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

VxWorks

开发平台:

C/C++

  1. /* client.c - client task for simple client/server network demo */
  2. /* Copyright 1984-1993 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01j,15feb93,jcf  added mixed endian support.
  8. 01i,13feb93,jcf  cleaned.
  9. 01h,31oct91,rrr  passed through the ansification filter
  10.   -changed copyright notice
  11. 01g,07jun89,gae  used ntohs on SERVER_NUM; changed SOCKADDR to struct sockaddr.
  12. 01f,29aug88,gae  fixed printStatus, documentation, allowed inet addresses.
  13. 01e,30may88,dnw  changed to v4 names.
  14. 01d,28may88,dnw  changed call to fioStdIn to STD_IN.
  15. 01c,06apr87,gae  caught some lint.  Got rid of excess includes.
  16. 01b,08jan87,jlf  minor rearrangement, to meet coding conventions.
  17.  changed <> to " in includes.
  18. 01a,17sep86,llk  written.
  19. */
  20. /*
  21. DESCRIPTION
  22. This is a simple demonstration of the server-client relationship.
  23. This is the client. The other half of the demonstration is in server.c
  24. EXAMPLE
  25. On the UNIX host "wrs":
  26.     % server
  27. On the VxWorks target:
  28.     -> ld < client.o
  29.     -> client "wrs"
  30. When you type characters into VxWorks, they will be echoed by the server
  31. on UNIX.  Type control-D to terminate.
  32. */
  33. #include "vxWorks.h"
  34. #include "fioLib.h"
  35. #include "stdio.h"
  36. #include "unistd.h"
  37. #include "string.h"
  38. #include "usrLib.h"
  39. #include "errnoLib.h"
  40. #include "hostLib.h"
  41. #include "sockLib.h"
  42. #include "socket.h"
  43. #include "inetLib.h"
  44. #include "in.h"
  45. #include "serverDemo.h"
  46. IMPORT char sysBootHost []; /* VxWorks saves name of host booted from */
  47. /* make 'clientSock' a global variable so that if something goes wrong
  48.  * when the client runs on VxWorks, the socket can be closed.
  49.  */
  50. int clientSock; /* socket opened to server */
  51. /*******************************************************************************
  52. *
  53. * client - client task
  54. *
  55. * This is a simple client program which connects to the server
  56. * via a socket.  It reads characters from standard input and sends
  57. * them onto the server through the socket.  It stops when it reads
  58. * 0 characters (receives an EOF).
  59. *
  60. * The server runs on UNIX, client runs on VxWorks.
  61. *
  62. * RETURNS: OK or ERROR
  63. */
  64. STATUS client (hostName)
  65.     char *hostName; /* name of host running server, 0=boot host */
  66.     {
  67.     struct sockaddr_in serverAddr; /* server's address */
  68.     struct sockaddr_in clientAddr; /* client's address */
  69.     int nBytes; /* number of bytes read from stdin */
  70.     char c;
  71.     if (hostName == NULL)
  72. hostName = sysBootHost;
  73.     /* Zero out the sock_addr structures.
  74.      * This MUST be done before the socket call.
  75.      */
  76.     bzero ((char *) &serverAddr, sizeof (serverAddr));
  77.     bzero ((char *) &clientAddr, sizeof (clientAddr));
  78.     /* Open the socket.
  79.      * Use ARPA Internet address format and stream sockets.
  80.      * Format described in "socket.h".
  81.      */
  82.     clientSock = socket (AF_INET, SOCK_STREAM, 0);
  83.     if (clientSock == ERROR)
  84. return (ERROR);
  85.     serverAddr.sin_family = AF_INET;
  86.     serverAddr.sin_port   = htons(SERVER_NUM);
  87.     /* get server's Internet address */
  88.     if ((serverAddr.sin_addr.s_addr = inet_addr (hostName)) == ERROR &&
  89. (serverAddr.sin_addr.s_addr = hostGetByName (hostName)) == ERROR)
  90. {
  91. printf ("Invalid host: "%s"n", hostName);
  92. printErrno (errnoGet ());
  93. close (clientSock);
  94. return (ERROR);
  95. }
  96.     printf ("Server's address is %x:n", htonl (serverAddr.sin_addr.s_addr));
  97.     if (connect (clientSock, (struct sockaddr *)&serverAddr,
  98.     sizeof (serverAddr)) == ERROR)
  99. {
  100. printf ("Connect failed:n");
  101. printErrno (errnoGet ());
  102. close (clientSock);
  103. return (ERROR);
  104. }
  105.     printf ("Connected...n");
  106.     /* repeatedly read from standard input and send to socket until EOF */
  107.     while (TRUE)
  108. {
  109. /* read a character from standard input */
  110. if ((nBytes = read (STD_IN, &c, 1)) == 0)
  111.     {
  112.     /* client read an EOF; exit the loop. */
  113.     break;
  114.     }
  115. else if (nBytes != 1)
  116.     {
  117.     printf ("CLIENT read error, %d bytes readn", nBytes);
  118.     printErrno (errnoGet ());
  119.     break;
  120.     }
  121. /* send byte to server */
  122. if (send (clientSock, &c, 1, 0) != 1)
  123.     {
  124.     printf ("CLIENT write error:n");
  125.     printErrno (errnoGet ());
  126.     }
  127. }
  128.     /* close socket from the VxWorks side */
  129.     close (clientSock);
  130.     printf ("n...goodbyen");
  131.     return (OK);
  132.     }