www.pudn.com > vxWorks_Lab.rar > testServer.c
/* testServer - prints a client's messages and sends user-specified replies */ /* Copyright 1984-1993 Wind River Systems, Inc. */ /* modification history -------------------- 01b,06dec94,bss cleaned up. 01a,???????,??? written. */ /* DESCRIPTION testServer sets up a TCP socket and then processes a client's requests sequentially. When a client requests service, testServer will print the message sent by the client, prompt the user for a reply to send, and then send the reply. testServer will continue servicing the client until it is done. Then testServer will exit. Typically, a server would sit in an infinite loop processing requests from multiple clients. */ #include#include #include #include #include #include /* defines */ #define MAX_MSG_SIZE 80 #define FOREVER for (;;) /* for WRS coding conventions */ #define LOCAL static /* typedefs */ typedef int SOCK_FD; /* forward declarations */ LOCAL void doRequest (); LOCAL void error (); /******************************************************************************* * * main - entry point of testServer * * This routine sets up a TCP socket and then sits in an infinite * loop accepting clients' requests. When a request is made, * doRequest () is called to process the request. */ main (argc, argv) int argc; char **argv; { u_short port; int clientAddrLength; SOCK_FD sockFd; SOCK_FD newSockFd; struct sockaddr_in clientAddr; struct sockaddr_in srvAddr; clientAddrLength = sizeof (clientAddr); /* Get the server port number */ if (argc != 2) { fprintf (stderr, "Usage: %s portNumber\n", argv[0]); exit (1); } port = (u_short) atoi(argv[1]); /* Create a socket */ if ( (sockFd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) error ("Socket failed"); /* * Bind to a well known address. INADDR_ANY says any network * interface will do. hton?() routines put things in network * byte order */ bzero ((char *)&srvAddr, sizeof (srvAddr)); srvAddr.sin_family = AF_INET; srvAddr.sin_port = htons (port); srvAddr.sin_addr.s_addr = INADDR_ANY; if (bind (sockFd, &srvAddr, sizeof(srvAddr)) < 0) { close (sockFd); error ("Bind failed"); } /* Queue only one request */ if (listen (sockFd, 1) < 0) { close (sockFd); error ("Listen failed"); } /* * Service request. Typically, the server would be in an infinite loop, * granting requests to many clients. In this example, we service just * a single client and then exit. */ FOREVER { newSockFd = accept (sockFd, &clientAddr, &clientAddrLength); if (newSockFd < 0) { close (sockFd); error ("Accept failed"); } doRequest (newSockFd, &clientAddr); printf ("\nServer socket closing\n"); /* These three lines would not */ close (sockFd); /* be here in a more typical */ exit (0); /* application. */ } } /************************************************************************ * * doRequest - read the clients requests from the socket and honor them. * * In a more typical application, this routine would fork of anouther * process to handle the request. * */ LOCAL void doRequest (sock, pClientAddr) SOCK_FD sock; struct sockaddr_in * pClientAddr; { char buf [MAX_MSG_SIZE]; int msgSize; char * pClientInet; u_short clientPort; pClientInet = inet_ntoa(pClientAddr->sin_addr); clientPort = ntohs(pClientAddr->sin_port); printf ("Client connected from inet %s, port %d\n\n", pClientInet, clientPort ); FOREVER { msgSize = read (sock, buf, MAX_MSG_SIZE - 1); /* Read error ? */ if (msgSize < 0) { close (sock); error ("Read failed"); } /* Connection closed ? */ else if (msgSize == 0) { close (sock); break; } /* Print client message and send reply */ else { printf ("Here is your %d byte message: \n%s\n\n", msgSize, buf); printf ("Enter message to send back: "); fflush (stdout); msgSize = read (STDIN_FILENO, buf, MAX_MSG_SIZE); buf [msgSize] = '\0'; /* must null terminate string */ if (write (sock, buf, msgSize+1) < 0) { close (sock); error ("Write failed."); } } } } /************************************************************************ * * error - print an error message and exit. * */ LOCAL void error (pStr) char * pStr; { perror (pStr); exit (1); }