server.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:3k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* server.c - server process for simple client/server network demo */
  2. static char *copyright = "Copyright 1986-1989, Wind River Systems, Inc.";
  3. /*
  4. modification history
  5. --------------------
  6. 01i,31oct91,rrr  passed through the ansification filter
  7.   -changed copyright notice
  8. 01h,25oct90,lpf  put back the HP version's bcopy.
  9. 01g,07jun89,gae  used ntohs on SERVER_NUM; changed SOCKADDR to struct sockaddr.
  10. 01c,06apr87,gae  caught some lint.  Got rid of excess includes.
  11. 01b,12jan87,jlf  minor rearrangement, to meet WRS coding conventions.
  12.  changed <> to " in includes.
  13. 01a,17sep86,llk  written.
  14. */
  15. /*
  16. DESCRIPTION
  17. This is a simple demonstration of the server-client relationship.
  18. This is the server. The other half of the demonstration is in client.c
  19. */
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include "serverDemo.h"
  24. #if     defined(HOST_HP)
  25. void bzero (s, n)
  26.     char *s;
  27.     int n;
  28.     {
  29.     memset (s, '', n);
  30.     }
  31. #endif
  32. /*******************************************************************************
  33. *
  34. * main - server process
  35. *
  36. * This is a simple server program which communicates with a client
  37. * through a socket.  It reads (recv's) characters, one at a time,
  38. * from the client and echos them to standard output.  When the client
  39. * goes away, the server also goes away (the server reads 0 characters
  40. * from the socket).
  41. *
  42. * The server runs on UNIX, client runs on VxWorks.
  43. *
  44. * See the manual page on sockets for more information.
  45. */
  46. main ()
  47.     {
  48.     int sock, snew; /* socket fd's */
  49.     struct sockaddr_in serverAddr; /* server's address */
  50.     struct sockaddr_in  clientAddr; /* client's address */
  51.     int client_len; /* length of clientAddr */
  52.     char c;
  53.     extern int errno; /* for UNIX error referencing */
  54.     /* Zero out the sock_addr structures.
  55.      * This MUST be done before the socket calls.
  56.      */
  57.     bzero (&serverAddr, sizeof (serverAddr));
  58.     bzero (&clientAddr, sizeof (clientAddr));
  59.     /* Open the socket.
  60.      * Use ARPA Internet address format and stream sockets.
  61.      * Format described in "socket.h".
  62.      */
  63.     sock = socket (AF_INET, SOCK_STREAM, 0);
  64.     if (sock == -1)
  65. exit (1);
  66.     /* Set up our internet address, and bind it so the client can connect. */
  67.     serverAddr.sin_family = AF_INET;
  68.     serverAddr.sin_port = htons(SERVER_NUM);
  69.     printf ("nBinding SERVERn", serverAddr.sin_port);
  70.     if (bind (sock, (struct sockaddr *)&serverAddr, sizeof (serverAddr))
  71. == -1)
  72. {
  73. printf ("bind failed, errno = %dn", errno);
  74. close (sock);
  75. exit (1);
  76. }
  77.     /* Listen, for the client to connect to us. */
  78.     printf ("Listening to clientn");
  79.     if (listen (sock, 2) == -1)
  80. {
  81. printf ("listen failedn");
  82. close (sock);
  83. exit (1);
  84. }
  85.     /* The client has connected.  Accept, and receive chars */
  86.     printf ("Accepting CLIENTn");
  87.     client_len = sizeof (clientAddr);
  88.     snew = accept (sock, (struct sockaddr *)&clientAddr, &client_len);
  89.     if (snew == -1)
  90. {
  91. printf ("accept failedn");
  92. close (sock);
  93. exit (1);
  94. }
  95.     printf ("CLIENT: port = %d: family = %d: addr = %lx:n",
  96. ntohs(clientAddr.sin_port), clientAddr.sin_family,
  97. ntohl(clientAddr.sin_addr.s_addr));
  98.     /* repeatedly recieve characters from client and put on stdout */
  99.     for (;;)
  100. {
  101. if (recv (snew, &c, 1, 0) == 0)
  102.     {
  103.     /* client has disappeared */
  104.     break;
  105.     }
  106. putchar (c);
  107. }
  108.     /* close the socket from the UNIX side */
  109.     close (sock);
  110.     close (snew);
  111.     printf ("n...goodbyen");
  112.     }