/* Single-threaded command line client. */ /* WINDOWS SOCKETS VERSION. */ /* Reads a sequence of commands to send to a server process */ /* over a socket connection. Wait for and display response. */
#include "Everything.h" #include "ClientServer.h"/* Defines request and response records. */
static BOOL SendRequestMessage (REQUEST *, SOCKET); static BOOL ReceiveResponseMessage (RESPONSE *, SOCKET);
struct sockaddr_in clientSAddr;/* Client Socket address structure */
int main (int argc, LPSTR argv[]) { SOCKET clientSock = INVALID_SOCKET; REQUEST request;/* See clntcrvr.h */ RESPONSE response;/* See clntcrvr.h */ WSADATA WSStartData; /* Socket library data structure */ BOOL quit = FALSE; DWORD conVal;
/* Initialize the WS library. Ver 2.0 */ WSAStartup (MAKEWORD (2, 0), &WSStartData); /* Connect to the server */ /* Follow the standard client socket/connect sequence */ clientSock = socket(AF_INET, SOCK_STREAM, 0); memset (&clientSAddr, 0, sizeof(clientSAddr)); clientSAddr.sin_family = AF_INET; if (argc >= 2) clientSAddr.sin_addr.s_addr = inet_addr (argv[1]); else clientSAddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
clientSAddr.sin_port = htons(SERVER_PORT); conVal = connect (clientSock, (struct sockaddr *)&clientSAddr, sizeof(clientSAddr)); /* Main loop to prompt user, send request, receive response */ while (!quit) { _tprintf (_T("%s"), _T("\nEnter Command: ")); fgets (request.record, MAX_RQRS_LEN, stdin); /* Get rid of the new line at the end */ request.record[strlen(request.record)-1] = '\0'; if (strcmp (request.record, "$Quit") == 0) quit = TRUE; SendRequestMessage (&request, clientSock); if (!quit) ReceiveResponseMessage (&response, clientSock); }
shutdown (clientSock, SD_BOTH); /* Disallow sends and receives */ closesocket (clientSock); WSACleanup(); _tprintf (_T("\n****Leaving client\n")); return 0; }
|