/********************************************* * File name: UDPquotecnls.cpp - This program is being * submitted as a part of program 1 home assignment by * his student bhavani prasad sangannagari (990 02 0777). * main, UDPquotecnls, Errexit Modified from connectionless UDP echo * in class notes of Bob Cotter and public examples directory. * The program takes commandline arguments for a destination * server plus port which will send any data entered from the * keyboard and recieves the quote from server. * It uses the winsock connectionless * communication functions (sendto and recvfrom) * It terminates with a ^Z (F6 EOF) from the keyboard. *********************************************************/ #include #include #include #define WSAver 0x102 #define LINELEN 100 void errexit(const char *, ...); void UDPquotecnls(const char *, const char *); /***************************************************** * main ( )- UDP client for quote service * This routine defines defaults for server host and * service, then takes command line parameters * as host and service. It then calls WSAstartup () * to select and initialize "winsock.dll", calls the * UDPquotecnls function, and finally closes out the dll. ******************************************************/ void main(int argc, char *argv[]) { char *service = argv[1]; char *host = argv[2]; WSAData wsadata; if(WSAStartup(WSAver, &wsadata)) errexit("WSAStartup failed\n"); UDPquotecnls(host, service); WSACleanup(); } /************************************************************** * UDPquotecnls ( ) This connectionless version embeds the host * identification process within this routine. It selects a * socket, and then goes into a loop, sequentially asking for * new input, sending it to the server (sendto ( )), looking for * a response (recvfrom( )), and printing the result, until an EOF * (^Z) (F6) is entered from the keyboard. ***********************************************************************/ void UDPquotecnls(const char *host, const char *service) { char buf[LINELEN+1]; /* buffer for one line of text */ SOCKET s; /* socket descriptor */ int nchars; /* read count*/ struct hostent *phe; /* pointer to host information entry */ struct servent *pse; /* pointer to service information entry */ struct protoent *ppe; /* pointer to protocol information entry */ struct sockaddr_in sin; /* an Internet endpoint address */ int type, status; /* socket descriptor and socket type */ char *transport = "udp"; /* transport service name for port */ memset(&sin,0,sizeof(sin)); // initialising sockadd_in structure with 0 sin.sin_family = AF_INET; // sin.sin_family = AF_INET; /* Map service name to port number */ if (pse = getservbyname(service,transport)) sin.sin_port = pse->s_port; else if ((sin.sin_port = htons((u_short)atoi(service))) == 0 ) errexit("can't get \"%s\" service 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("can't get \"%s\" host entry\n", host); /* Map protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) errexit("can't get \"%s\" protocol entry\n", transport); 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("can't create socket: %d\n", GetLastError()); /* Here user enters input which is sent to server and a quote is recieved from server */ while (fgets(buf,sizeof(buf),stdin)) { buf[LINELEN]= '\0'; nchars = strlen(buf); status = sendto(s, buf, nchars, 0,(struct sockaddr FAR *) &sin, sizeof(sin)); if (status == SOCKET_ERROR) errexit("Sendto failed: %d\n", GetLastError ()); if (recvfrom(s, buf, LINELEN, 0, NULL, NULL) < 0) errexit("recv failed: %d\n", GetLastError()); fputs(buf,stdout); } } /******************************************************************************* * Errexit takes variable number of arguments, passes on to vprintf for output * and finally calls WSACleanup to release system socket resources. * this follows the printf conventions for the formatted output. ********************************************************************************/ void errexit(const char *format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); WSACleanup(); exit(1); }