Code:
/*
* File: wl.c
* The start of a "Web Load" client program.
*/
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "wl.h"
int main(int argc, char *argv[]) {
char *name; /* domain name of server */
int port; /* port number to connect to, on server */
struct hostent *phostent; /* for lookup of server ip address */
/* struct sockaddr_in sin; socket endpoint */
int s; /* socket descriptor for connection */
int pid; /* clinet (this process's) pid */
char spid[8]; /* string version of pid */
char mess_out[256] = "\0this is a lower case message. from pid = ";
int mess_out_len;
char mess_in[256];
int nw,nr; /* number read and number written */
int nrsf; /* number read so far, for multiple reads */
struct sockaddr_in sin; /* socket endpoint */
pid=getpid();
if(argc==2) {
name = argv[1];
port = WL_PORT;
} else if(argc==3) {
name = argv[1];
port = atoi(argv[2]); // No check for a valid arguement!
} else {
fprintf(stderr,"ERROR: useage: %s <name> [<port>]\n", argv[0]);
exit(1);
}
#ifdef DEBUG
fprintf(stderr,"DEBUG: %d %s connecting to %s port %d\n",
pid, argv[0], name, port);
#endif
/* Get the ip address from the domain name. */
if((phostent = gethostbyname(name))==0) {
fprintf(stderr,"ERROR: %s can not get ip address for %s\n", argv[0], name);
exit(1);
}
#ifdef DEBUG
fprintf(stderr,"DEBUG: %d %s hostent.h_addr=%u.%u.%u.%u hostent.h_length=%d\n", pid, argv[0],
(unsigned char)phostent->h_addr[0],
(unsigned char)phostent->h_addr[1],
(unsigned char)phostent->h_addr[2],
(unsigned char)phostent->h_addr[3],
phostent->h_length);
#endif
/* Get a TCP based socket. */
// if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
if((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr,"ERROR: %s Can't get a socket.\n", argv[0]);
exit(1);
}
/* Prepare the endpoint. (Only specify the remote ip address/port) */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, phostent->h_addr, phostent->h_length);
sin.sin_port = htons((u_short)port);
/* Make the connection */
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
fprintf(stderr,"ERROR: %s Can't connect to %s port %d\n", argv[0], name, port);
exit(1);
}
/*
* Now build and write a message to the server, including the '\0'
* messages consist of 1 to 255 bytes.
* the first byte msize = mess[0] is the size of the message,
* including the fisrt byte.
* the bytes mess[1]. mess[2]. ... mess[msize-1] comprise the data
* in the message.
*/
mess_out_len = strlen(mess_out+1) + 2;
sprintf(spid, "%d\0", pid);
strncat(mess_out+1, spid, 256-mess_out_len);
mess_out_len = strlen(mess_out+1) + 2;
mess_out[0] = mess_out_len;
#ifdef DEBUG
fprintf(stderr,"DEBUG: %d %s write mess_out= %d \"%s\" to socket\n", pid, argv[0], mess_out[0], mess_out+1);
#endif
if((nw=write(s, mess_out, mess_out_len)) != mess_out_len) {
fprintf(stderr,"ERROR: %s wrote %d out of %d.\n", argv[0], nw, mess_out_len);
exit(1);
}
shutdown(s,1); /* optional */
/* Now read a message of the same size back */
for(nrsf=0; nrsf < mess_out_len; nrsf+=nr) {
if((nr=read(s, mess_in+nrsf, mess_out_len-nrsf))<=0) {
break;
}
#ifdef DEBUG
fprintf(stderr,"DEBUG: %d %s read nr = %d nrsf = %d\n", pid, argv[0], nr, nrsf);
#endif
}
#ifdef DEBUG
fprintf(stderr,"DEBUG: %d %s final nr = %d nrsf = %d\n", pid, argv[0], nr, nrsf);
fprintf(stderr,"DEBUG: %d %s read mess_in = %d \"%s\" from socket\n", pid, argv[0], mess_in[0], mess_in+1);
#endif
/*
* Note the next read should return an EOF, if testing with echo (port=7)
* because of the "shutdown(s,1)"
*/
exit(0);
}