/****************************************************************************** * this program is developed by bhavani prasad sangannagari * * standard routines for socket interface are copied from comer * * 1. allows user to modify default port and host * 2. client repeats the main menu after each response until user selects quit.* * * * ****************************************************************************/ /**************************************************************************** * main threads () * * implements comment 1 and passes to TCPlib routine * *********************************************************************************/ #include #include #include #include #include #include #include #define BUFSIZE 100 #define LINELEN 128 #define WSVERS MAKEWORD(2, 0) void TCPlib(const char *, const char *); SOCKET connectTCP(const char *, const char *); SOCKET connectsock(const char *, const char *, const char *); void errexit(const char *format, ...); void list(SOCKET); // to list out all the books void search(SOCKET); // to search the book void checkout(SOCKET);// to check out a book void quit(SOCKET); // to quit the client void readip(SOCKET); // to read the data from the server void main(int argc, char *argv[]) { const char *host = "localhost"; const char *service = "2003"; WSADATA wsadata; switch (argc) { case 1: host = "localhost"; break; case 3: service = argv[2]; case 2: host = argv[1]; break; default: fprintf(stderr, "usage: TCP [host [port]]\n"); exit(1); } if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); TCPlib(host,service); WSACleanup(); exit(0); } /**************************************************************************** * TCPlib() * * performs sorts the users requests to different functions and repeats the main * * menu * *********************************************************************************/ void TCPlib(const char *host, const char *service) { char c; SOCKET s; // int cc, outchars, inchars; s = connectTCP(host, service); while(1) { printf("\n**************************************************"); printf("\n***************** MEMORIAL LIBRARY *****************"); printf("\n**************************************************"); printf("\n ENTER 1: LIST OF ALL THE BOOKS AVAILABLE"); printf("\n 2: TO SEARCH FOR A BOOK "); printf("\n 3: TO CHECK OUT A BOOK "); printf("\n 4: QUIT "); printf("\n Your Choice : "); switch(c = _getche()) { case '1': list(s); break; case '2': search(s); break; case '3': checkout(s); break; case '4': quit(s); return; default: printf("\n INVALID OPTION SELECTED : PLEASE SELECT AGAIN"); } } closesocket(s); } /**************************************************************************** * list() * intimates the server of the particular user requests and passes control to * * readip() to read server response * *********************************************************************************/ void list(SOCKET s) { int rc; rc = send(s, "1", 1,0); if (rc < 0) { fprintf(stderr, "Error sending message to server %d\n", GetLastError()); closesocket(s); exit(1); } readip(s); return; } /**************************************************************************** * search() * intimates the server of the particular user requests to search the book by * * ISBN or booktitle * * readip() to read server response * ******************************************************************************/ void search(SOCKET s) { int rc; char c,sendbuf[BUFSIZE],temp[20]; memset(sendbuf,0,sizeof sendbuf); memset(temp,0,sizeof temp); while(1) { printf("\n ENTER 1: TO SEARCH BOOK BY ISBN"); printf("\n 2: TO SEARCH BOOK BY BOOK TITLE"); printf("\n 3: TO QUIT SEARCH BOOK"); printf("\n Your Choice : "); switch(c = _getche()) { case '1': memset(sendbuf,0,sizeof sendbuf); printf("\n ENTER ISBN "); scanf("%s",temp); flushall(); strcat(sendbuf,"21"); strcat(sendbuf,temp); strcat(sendbuf,"#"); rc = send(s, sendbuf,sizeof sendbuf,0); if (rc < 0) { fprintf(stderr, "Error sending message to server %d\n", GetLastError()); closesocket(s); exit(1); } readip(s); break; case '2': memset(sendbuf,0,sizeof sendbuf); printf("\n ENTER THE BOOK TITLE"); gets(temp); strcat(sendbuf,"22"); strcat(sendbuf,temp); strcat(sendbuf,"#"); rc = send(s, sendbuf,sizeof sendbuf,0); if (rc < 0) { fprintf(stderr, "Error sending message to server %d\n", GetLastError()); closesocket(s); exit(1); } readip(s); break; case '3': return; default: printf("\nenter proper value"); } // end switch } //end while } /**************************************************************************** * checkout() * takes the particular ISBN from user and sends to server then calls readip() * * for reading input * *******************************************************************************/ void checkout(SOCKET s) { int rc; char c,sendbuf[BUFSIZE],temp[20]; memset(sendbuf,0,sizeof sendbuf); memset(temp,0,sizeof temp); while(1) { printf("\n ENTER 1: TO CHECKOUT A BOOK BY ISBN"); printf("\n 2: TO QUIT CHECKOUT"); printf("\n Your Choice : "); switch(c = _getche()) { case '1':memset(sendbuf,0,sizeof sendbuf); printf("\n ENTER ISBN "); scanf("%s",temp); flushall(); strcat(sendbuf,"31"); strcat(sendbuf,temp); strcat(sendbuf,"#"); rc = send(s, sendbuf,sizeof sendbuf,0); if (rc < 0) { fprintf(stderr, "Error sending message to server %d\n", GetLastError()); closesocket(s); exit(1); } printf("\n See your server window and wait for a while"); readip(s); break; case '2': return; default: printf("\nenter proper value"); }//end switch } // end while } /**************************************************************************** * quit() * * intimates the server that this client is quiting before quiting * ******************************************************************************/ void quit(SOCKET s) { int rc; rc = send(s, "4", 1,0); if (rc < 0) { fprintf(stderr, "Error sending message to server %d\n", GetLastError()); closesocket(s); exit(1); } closesocket(s); return; } /**************************************************************************** * readip() * * reads input from the server until server sends 1 to indicate end of data * ******************************************************************************/ void readip(SOCKET s) { char recvbuf[BUFSIZE]; int rc; memset(&recvbuf, 0, sizeof(recvbuf)); printf("\nBook Tit Author L Name Author F Name,ISBN ,Copy Avail,Copies Checked Out, copies still avail\n"); while(1) { rc = recv(s,recvbuf,BUFSIZE,0);// receive message from server if (strlen(recvbuf)== 1) break; if(rc<0) { fprintf(stderr, "Error in recieving from server: %d\n",GetLastError()); closesocket(s); exit(1); } printf("%s",recvbuf); memset(recvbuf,0,sizeof recvbuf); } return; } SOCKET connectsock(const char *host, const char *service, const char *transport) { struct hostent *phe; struct servent *pse; struct protoent *ppe; struct sockaddr_in sin; int s,type; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; /*map service name to prot number*/ if ( pse = getservbyname(service,transport)) sin.sin_port = pse->s_port; else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) errexit("cant get \"%s\" host entry\n", service); /*map host name to IP address , allowing for dotted decimal */ if( phe = gethostbyname(host)) memcpy(&sin.sin_addr,phe->h_addr,phe->h_length); else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) errexit("cant get \"%s\" host entry\n", host); /*map protocol name to protocol number */ if ((ppe = getprotobyname(transport)) == 0) errexit("cant get \"%s\" protocol entry \n", transport); /*use protocol to choose a sockettype */ if (strcmp(transport, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM; /*allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) errexit("cant create socket: %d\n", GetLastError()); /* connect the socket */ if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) errexit("cant connect to %s.%s: %d\n", host, service,GetLastError()); return s; } SOCKET connectTCP(const char *host, const char *service) { return connectsock(host,service,"tcp"); } void errexit(const char *format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); WSACleanup(); exit(1); }