////////////////////////////////////////////////////////////////////////////// // // File Name: client.cxx // // Overview: TBD // // Approach : TBD // // Notes: By J. Kyle Glowacki // ////////////////////////////////////////////////////////////////////////////// #define closesocket close #include #include #include #include #include #include #include #include #include #include #include #include extern int errno; char localhost[] = "localhost"; /* default host name */ /*------------------------------------------------------------------------ * Program: client * * Purpose: allocate a socket, connect to a server, and print all output * * Syntax: client [ host [port] ] * * host - name of a computer on which server is executing * port - protocol port number server is using * * Note: Both arguments are optional. If no host name is specified, * the client uses "localhost"; if no protocol port is * specified, the client uses the default given by PROTOPORT. * *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad; /* structure to hold an IP address */ int sd; /* socket descriptor */ int port; /* protocol port number */ char *host; /* pointer to host name */ int n; /* number of characters read */ char buf[1001]; /* buffer for data from the server */ // struct passwd *ppwd; memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ /* Check command-line argument for protocol port and extract */ /* port number if one is specified. Otherwise, use the default */ /* port value given by constant PROTOPORT */ if (argc < 4) { cerr << "Usage :" << endl << " cli port " << endl; exit(-1); } //filename = argv[3]; // no need to copy, use it directly port = atoi(argv[2]); if (port > 0) /* test for legal value */ sad.sin_port = htons((u_short)port); else { /* print error message and exit */ cerr << "Bad port number " << argv[2] << endl; exit(1); } host = argv[1]; // Convert host name to equivalent IP address and copy to sad. ptrh = gethostbyname(host); if (((char *)ptrh) == NULL ) { cerr << "Invalid Host : " << host << endl; exit(1); } memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); // Map TCP transport protocol name to protocol number. if (((int)(ptrp = getprotobyname("tcp"))) == 0) { cerr << "Can not map 'tcp' to protocol number" << endl; exit(1); } // Create a socket. sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { cerr << "Socket Creation Failed" << endl; exit(1); } // Connect the socket to the specified server. if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { cerr << "Connect Failed" << endl; exit(1); } // construct a string with the request to send. sprintf(buf, "GET %s HTTP/1.0\r\n\r\n\0", argv[3]); // send the string send(sd, buf, strlen(buf)+1, 0); // reset the buffer to 0 // ensure that the final byte is zero memset((char *)&buf,0,1001); n = recv(sd, buf, 1000, 0); while (n > 0) { //write(1,buf,n); cout << buf; memset((char *)&buf,0,1000); n = recv(sd, buf, sizeof(buf), 0); } // Close the socket. closesocket(sd); // Terminate the client program gracefully. exit(0); }