/* internet client client.c compile with cc client.c -o client start the server first run with client */ #include #include #include #include #include #include #include #include #include char *tidyup(char *str) { int i,j; for (i=0, j=0; str[i]; i++) { char c=str[i]; if (isupper(c)) c=tolower(c); if (iscntrl(c)) continue; str[j]=c; j++; } str[j]=0; return(str); } char *getline(FILE *f, char *s, int sz) { if (fgets(s,sz,f)==NULL) { s[0]=0; return(s); } tidyup(s); return(s); } void interact(FILE *reader, FILE *writer) { int n, command; char buffer[10250]; printf("saying hello to the server...\n"); fprintf(writer,"hello\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); printf("response from the server:\n \"%s\"\n",buffer); while (1) { printf("\n"); printf("Select: 1> Say hello to the server again\n"); printf(" 2> Ask the server what the time is\n"); printf(" 3> Ask the server how it is feeling\n"); printf(" 4> Tell the server to terminate\n"); printf(" 5> Disconnect from the server\n"); printf("command: "); n=scanf("%d",&command); if (n==0) continue; switch (command) { case 1: /* say hello again */ printf("saying hello to the server...\n"); fprintf(writer,"hello\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); printf("response from the server:\n \"%s\"\n",buffer); break; case 2: /* what time is it? */ printf("asking the server what time it is...\n"); fprintf(writer,"what time is it?\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); if (strstr(buffer,"it is ")==buffer) { buffer[30]=0; printf("The server said it is now %s.\n",buffer+6); } else printf("The server made an inappropriate response\n"); break; case 3: /* how are you */ printf("asking the server how it feels...\n"); fprintf(writer,"how are you?\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); printf("the server replied:\n \"%s\"\n",buffer); break; case 4: /* terminate server process */ printf("telling the server to stop...\n"); fprintf(writer,"drop dead\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); printf("the server replied:\n \"%s\"\n",buffer); break; case 5: /* disconect */ printf("saying goodbye to the server...\n"); fprintf(writer,"goodbye\n"); fflush(writer); getline(reader,buffer,sizeof(buffer)); printf("the server responded:\n \"%s\"\n",buffer); break; } if (command==5) break; } printf("End of interaction.\n"); } void main(int argc, char *argv[]) { char server_name[100]; int connection,n; int port_number; struct sockaddr_in server_internet_addr; void *sia_ptr=&server_internet_addr; int sia_len=sizeof(server_internet_addr); struct hostent* server_info_ptr; struct in_addr* coded_server_addr; FILE *r_file, *w_file; if (argc!=3) { fprintf(stderr,"usage: %s remote-host port-number\n",argv[0]); exit(1); } strcpy(server_name,argv[1]); n=sscanf(argv[2],"%d",&port_number); if (n!=1) { fprintf(stderr,"%s: \"%s\" is not a valid port number\n",argv[0],argv[2]); exit(1); } server_info_ptr=gethostbyname(server_name); if (server_info_ptr==NULL) { fprintf(stderr,"%s: (1)host \"%s\" not known\n", argv[0],server_name); exit(1); } coded_server_addr=(struct in_addr*)server_info_ptr->h_addr; if ((coded_server_addr->s_addr)==NULL) { fprintf(stderr,"%s: (2)host %s not known\n", argv[0],inet_ntoa(*coded_server_addr)); exit(1); } bzero(sia_ptr,sia_len); server_internet_addr.sin_family=AF_INET; server_internet_addr.sin_addr.s_addr=coded_server_addr->s_addr; server_internet_addr.sin_port=htons(port_number); connection=socket(AF_INET,SOCK_STREAM,0); if (connection<0) { perror("socket failure"); exit(1); } printf("\n[Connecting to %s (%s) on port %d]\n", server_name,inet_ntoa(*coded_server_addr),port_number); n=connect(connection,sia_ptr,sia_len); if (n<0) { perror("connect"); exit(1); } r_file=fdopen(connection,"r"); w_file=fdopen(connection,"w"); if (r_file==NULL || w_file==NULL) { fprintf(stderr,"%s: made a bad connection\n",argv[0]); exit(1); } interact(r_file, w_file); printf("%s: finished\n",argv[0]); }