/* * skeleton.c * * CS 3251 Programming Assignment 1, Client Skeleton * * $Id: skeleton.c,v 1.1 1999/10/29 17:46:57 ewz Exp $ */ #include #include #include #include #include /* for sockaddr_in */ #include #include /* for perror() */ #include /* for hostent and gethostbyname() */ /* structure of sockaddr_in, defined in netinet/in.h: struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; */ #define SRVPORT 13251 /* server port number */ #define SRVADDR "130.207.8.20" /* IP addr for flora.cc.gatech.edu */ #define ID "ewz@cc.gatech.edu" #define MSIZE 256 #define BUFSIZE 128 /* * Report error on system call and exit. */ void syserror(char *s) { perror(s); exit(1); } /* * Get current time and convert to human readable form. * (from KLC) */ char * getTime() { struct timeval myTime; struct tm *myTimP; gettimeofday(&myTime,(struct timezone *)NULL); myTimP = localtime((time_t *)&myTime.tv_sec); return asctime(myTimP); } /* Convert a struct sockaddr_in into - * string notation. Assumes addresses are in network byte order. * Don't forget to convert to host byte order! */ void sockAddrToString(char *string, struct sockaddr_in *ss) { /* your code goes here */ } /* * Main routine. */ int main(int argc, char *argv[]) { int cSocket; struct sockaddr_in sAddr, cAddr; int sizeofcAddr, sizeofsAddr; char sMsg[MSIZE],cMsg[MSIZE]; char caddrbuf[BUFSIZE], saddrbuf[BUFSIZE]; u_short port; /* CREATE A SOCKET */ /* your code goes here */ /* don't forget to check for errors in return code for all sys calls */ /* SET UP ADDRESS INFORMATION FOR SERVER IN SADDR */ /* your code goes here */ /* it's a good idea to zero out the structure first */ /* don't forget to convert from host to network byte order */ /* CONNECT TO SERVER USING SADDR INFORMATION */ /* your code goes here */ /* RECORD TIME OF SUCCESSFUL CONNECTION */ printf("Connected to server at time %s\n",getTime()); /* CONSTRUCT MESSAGE TO SEND TO SERVER */ /* FIRST contruct string for client IP address and port in cAddr */ /* your code goes here */ /* use getsockname() and sockAddrToString() */ /* NEXT contruct string for server IP address and port in sAddr */ /* your code goes here */ /* use getpeername() and sockAddrToString() */ /* note: this should be the same as SRVADDR-SRVPORT, letting you */ /* check whether sockAddrToString works correctly */ /* FILL IN CLIENT MESSAGE */ /* your code goes here */ /* it's a good idea to zero out cMsg before filling it in */ /* SEND MESSAGE */ /* your code goes here */ /* GET REPLY, FIRST GREETING THEN RANDOM NUMBER */ /* your code goes here */ /* CLOSE CONNECTION */ /* your code goes here */ } /* main */