SDDSlib
Loading...
Searching...
No Matches
sddslogclient.c File Reference

A simple TCP client for sending commands to a server. More...

#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.

Macros

#define BUFLEN   16384
 

Functions

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

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> ...]
  • hostname: The server's hostname or IP address.
  • port: The port number on which the server is listening.
  • <command>: (Optional) One or more commands to send to the server initially.

If no commands are provided as arguments, the client enters an interactive mode where the user can input commands manually.

Functions

  • void error(const char *msg): Prints an error message and exits the program.
  • int processCommand(char *buffer, int sockfd): Sends a command to the server and processes the response.
  • int main(int argc, char *argv[]): The main function that handles argument parsing, socket creation, connection, and command processing.

Example

To connect to a server at localhost on port 8080 and send the commands HELLO and STATUS:

./sddslogclient localhost 8080 HELLO STATUS

After sending the initial commands, the client will enter interactive mode where additional commands can be entered until disconnect is typed.

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.

Macro Definition Documentation

◆ BUFLEN

#define BUFLEN   16384

Definition at line 64 of file sddslogclient.c.

Function Documentation

◆ error_exit()

void error_exit ( const char * msg)

Definition at line 67 of file sddslogclient.c.

67 {
68 perror(msg);
69 exit(EXIT_FAILURE);
70}

◆ main()

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

Definition at line 90 of file sddslogclient.c.

90 {
91 int sockfd, portno, arg_index;
92 struct sockaddr_in serv_addr;
93 struct hostent *server;
94 int persist = 1;
95
96 char buffer[BUFLEN];
97
98 if (argc < 3) {
99 fprintf(stderr, "Usage: %s hostname port [<command> [<command> ...]]\n", argv[0]);
100 exit(EXIT_FAILURE);
101 }
102
103 portno = atoi(argv[2]);
104 sockfd = socket(AF_INET, SOCK_STREAM, 0);
105 if (sockfd < 0)
106 error_exit("ERROR opening socket");
107
108 server = gethostbyname(argv[1]);
109 if (server == NULL) {
110 fprintf(stderr, "ERROR, no such host\n");
111 exit(EXIT_FAILURE);
112 }
113
114 memset(&serv_addr, 0, sizeof(serv_addr));
115 serv_addr.sin_family = AF_INET;
116 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
117 serv_addr.sin_port = htons(portno);
118
119 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
120 error_exit("ERROR connecting");
121
122 for (arg_index = 3; persist && arg_index < argc; arg_index++) {
123 if (snprintf(buffer, BUFLEN, "%s\n", argv[arg_index]) >= BUFLEN) {
124 fprintf(stderr, "ERROR: Command too long\n");
125 continue;
126 }
127
128 if (strcmp(buffer, "disconnect\n") == 0)
129 persist = 0;
130
131 printf("Processing command: %s", buffer);
132 fflush(stdout);
133
134 process_command(buffer, sockfd);
135 }
136
137 while (persist) {
138 printf("Please enter the message: ");
139 fflush(stdout);
140
141 memset(buffer, 0, BUFLEN);
142 if (fgets(buffer, BUFLEN - 1, stdin) == NULL)
143 break;
144
145 if (strcmp(buffer, "disconnect\n") == 0)
146 persist = 0;
147
148 process_command(buffer, sockfd);
149 }
150
151 close(sockfd);
152 return EXIT_SUCCESS;
153}

◆ process_command()

int process_command ( char * buffer,
int sockfd )

Definition at line 73 of file sddslogclient.c.

73 {
74 ssize_t n;
75
76 n = write(sockfd, buffer, strlen(buffer));
77 if (n < 0)
78 error_exit("ERROR writing to socket");
79
80 memset(buffer, 0, BUFLEN);
81 n = read(sockfd, buffer, BUFLEN - 1);
82 if (n < 0)
83 error_exit("ERROR reading from socket");
84
85 printf("%s\n", buffer);
86 return n;
87}