SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
sddslogclient.c File Reference

Detailed Description

A simple TCP client for sending commands to a server.

This program establishes a TCP connection to a specified server and port, sends commands either from the command-line arguments or interactively from the user, and processes the server's responses. The client supports persistent connections, allowing multiple commands to be sent over a single socket until a "disconnect" command is issued.

Usage

sddslogclient <hostname> <port> [<command> ...]

Options

Required Description
hostname The server's hostname or IP address.
port The port number on which the server is listening.
Optional Description
<command> One or more commands to send to the server initially.
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, R. Soliday

Definition in file sddslogclient.c.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

Go to the source code of this file.

Functions

void error_exit (const char *msg)
 
int process_command (char *buffer, int sockfd)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ error_exit()

void error_exit ( const char * msg)

Definition at line 51 of file sddslogclient.c.

51 {
52 perror(msg);
53 exit(EXIT_FAILURE);
54}

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 74 of file sddslogclient.c.

74 {
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}

◆ process_command()

int process_command ( char * buffer,
int sockfd )

Definition at line 57 of file sddslogclient.c.

57 {
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}
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