SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddslogclient.c
Go to the documentation of this file.
1/**
2 * @file sddslogclient.c
3 * @brief A simple TCP client for sending commands to a server.
4 *
5 * @details
6 * This program establishes a TCP connection to a specified server and port,
7 * sends commands either from the command-line arguments or interactively
8 * from the user, and processes the server's responses. The client supports
9 * persistent connections, allowing multiple commands to be sent over a single
10 * socket until a "disconnect" command is issued.
11 *
12 * @section Usage
13 * ```
14 * sddslogclient <hostname> <port> [<command> ...]
15 * ```
16 *
17 * @section Options
18 * | Required | Description |
19 * |------------|-----------------------------------------------------------------|
20 * | `hostname` | The server's hostname or IP address. |
21 * | `port` | The port number on which the server is listening. |
22 *
23 * | Optional | Description |
24 * |------------|-----------------------------------------------------------------|
25 * | `<command>`| One or more commands to send to the server initially. |
26 *
27 * @copyright
28 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
29 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
30 *
31 * @license
32 * This file is distributed under the terms of the Software License Agreement
33 * found in the file LICENSE included with this distribution.
34 *
35 * @author
36 * M. Borland, R. Soliday
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#include <sys/types.h>
44#include <sys/socket.h>
45#include <netinet/in.h>
46#include <netdb.h>
47
48#define BUFLEN 16384
49
50/* Error handling function */
51void error_exit(const char *msg) {
52 perror(msg);
53 exit(EXIT_FAILURE);
54}
55
56/* Process a single command */
57int process_command(char *buffer, int sockfd) {
58 ssize_t n;
59
60 n = write(sockfd, buffer, strlen(buffer));
61 if (n < 0)
62 error_exit("ERROR writing to socket");
63
64 memset(buffer, 0, BUFLEN);
65 n = read(sockfd, buffer, BUFLEN - 1);
66 if (n < 0)
67 error_exit("ERROR reading from socket");
68
69 printf("%s\n", buffer);
70 return n;
71}
72
73/* Main function */
74int main(int argc, char *argv[]) {
75 int sockfd, portno, arg_index;
76 struct sockaddr_in serv_addr;
77 struct hostent *server;
78 int persist = 1;
79
80 char buffer[BUFLEN];
81
82 if (argc < 3) {
83 fprintf(stderr, "Usage: %s <hostname> <port> [<command> [<command> ...]]\n", argv[0]);
84 exit(EXIT_FAILURE);
85 }
86
87 portno = atoi(argv[2]);
88 sockfd = socket(AF_INET, SOCK_STREAM, 0);
89 if (sockfd < 0)
90 error_exit("ERROR opening socket");
91
92 server = gethostbyname(argv[1]);
93 if (server == NULL) {
94 fprintf(stderr, "ERROR, no such host\n");
95 exit(EXIT_FAILURE);
96 }
97
98 memset(&serv_addr, 0, sizeof(serv_addr));
99 serv_addr.sin_family = AF_INET;
100 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
101 serv_addr.sin_port = htons(portno);
102
103 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
104 error_exit("ERROR connecting");
105
106 for (arg_index = 3; persist && arg_index < argc; arg_index++) {
107 if (snprintf(buffer, BUFLEN, "%s\n", argv[arg_index]) >= BUFLEN) {
108 fprintf(stderr, "ERROR: Command too long\n");
109 continue;
110 }
111
112 if (strcmp(buffer, "disconnect\n") == 0)
113 persist = 0;
114
115 printf("Processing command: %s", buffer);
116 fflush(stdout);
117
118 process_command(buffer, sockfd);
119 }
120
121 while (persist) {
122 printf("Please enter the message: ");
123 fflush(stdout);
124
125 memset(buffer, 0, BUFLEN);
126 if (fgets(buffer, BUFLEN - 1, stdin) == NULL)
127 break;
128
129 if (strcmp(buffer, "disconnect\n") == 0)
130 persist = 0;
131
132 process_command(buffer, sockfd);
133 }
134
135 close(sockfd);
136 return EXIT_SUCCESS;
137}
write(SddsFile sdds_file, output_file)
Mostly backward compatible with the PyLHC sdds module write() function.
Definition sdds.py:1967
SddsFile read(input_file)
Mostly backward compatible with the PyLHC sdds module read() function.
Definition sdds.py:1870