/* *********************************************************** *
* *
* writer.c (client.c) *
* *
* This program is for the client process in the server *
* /client model, using the socket. This program is designed *
* to run on collins.eng.usf.edu The correspondent client *
* process is offered by server.c , which is designed to work *
* on armstrong.eng.usf.edu This server is going to listen *
* to port #6700 and this value must be agreed upon between *
* server and client. *
* *
* When you compile this file, please make sure a library *
* file, lxnet. That means you need to provide library *
* option when you compile it, like: *
* *
* cc writer.c -o writer -lxnet *
* *
* Code designed and created by H. Fujinoki *
* Code modified on: March 12, 1997 at USF Tampa. FL USA *
*
* code modified by: john egan
* code modified on: spring 1999
* compile: cc sockets_client.c -lxnet
*
* *********************************************************** */
#include < stdio.h >
#include < string.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < netdb.h >
#define TRUE 1 /* for infinite loop */
#define PORTNUM 6779 /* the port # the server is listening to */
#define DEFAULT_PROTOCOL 0 /* constant for default protocol */
/* Main ============================================================ */
void main(argc, argv)
int argc;
char *argv[];
{
int port = PORTNUM; /* the port number agrred upon server & writer */
int socketid; /* the socket ID */
int status; /* error status holder */
int len; /* character length in the char array */
int i; /* a general loop counter */
int server_len; /* length for sockaddr for the server */
int client_len; /* length for sockaddr for the client */
char buffer[8]; /* the message buffer */
/* --- socket address in the Internet --- */
struct sockaddr_in serverINETaddress;
struct hostent *myhostent;
unsigned long inetAddress;
/* --- Check the argument --- */
if (argc != 2) /* there must be two arguments */
{
printf("Wrong Argument..");
printf("Please put 'writerX'..X is one of 1, 2, 3.as an argument.\n");
exit(-1);
}
/* --- Get message --- */
len = strlen(argv[1]);
for (i = 0; i <= len; i++) buffer[i] = argv[1][i];
/* --- Display the message --- */
printf("Message: %s.\n", buffer);
/* --- Get Internet address --- */
myhostent = gethostbyname("rivest");
server_len = sizeof(serverINETaddress);
bzero((char *)&serverINETaddress, server_len);
serverINETaddress.sin_family = AF_INET;
bcopy((char *)myhostent->h_addr, (char *)&serverINETaddress.sin_addr,
myhostent->h_length);
serverINETaddress.sin_port = htons(port);
/* ====================== the Main Loop ========================== */
while (TRUE)
{
/* --- Create a socket --- */
socketid = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL);
if (socketid < 0)
{ printf("Error in creating a socket.\n"); exit(-1); }
/* --- Connecting the socket --- */
status = connect(socketid, (struct sockaddr *)&serverINETaddress, server_len);
if (status < 0)
{
sleep(1); /* insert a delay */
status = connect(socketid, (struct sockaddr *)&serverINETaddress, server_len);
}
/* --- Get message information --- */
len = strlen(buffer);
printf("buffer: %s.\n", buffer);
printf("length: %d.\n", len);
/* --- Seend the message --- */
status = write(socketid, buffer, len);
if (status < 0)
{ printf("Error in write.\n"); exit(-1); }
/* --- Receive the acknowledgement --- */
status = read(socketid, buffer, len);
if (status < 0)
{ printf("Error in receiving a message.\n"); exit(-1); }
/* --- Display the echo message --- */
printf("Echo: %s.\n", buffer);
/* --- Insert delay --- */
sleep(2);
/* --- Cut the socket --- */
close(socketid);
}
}
/* ======================= END OF THE CODE ======================= */