home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / echosamp / echosrv / echostcp.c < prev    next >
Text File  |  1999-05-11  |  6KB  |  188 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1996.                                   */
  6. /*                                                                          */
  7. /*   All rights reserved.                                                   */
  8. /*                                                                          */
  9. /*   US Government Users Restricted Rights -                                */
  10. /*   Use, duplication or disclosure restricted by GSA ADP Schedule          */
  11. /*   Contract with IBM Corp.                                                */
  12. /*                                                                          */
  13. /*--------------------------------------------------------------------------*/
  14. /*                                                                          */
  15. /*  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is             */
  16. /*  sample code created by IBM Corporation. This sample code is not         */
  17. /*  part of any standard or IBM product and is provided to you solely       */
  18. /*  for  the purpose of assisting you in the development of your            */
  19. /*  applications.  The code is provided "AS IS", without                    */
  20. /*  warranty of any kind.  IBM shall not be liable for any damages          */
  21. /*  arising out of your use of the sample code, even if they have been      */
  22. /*  advised of the possibility of such damages.                             */
  23. /*                                                                          */
  24. /*****************************************************************************
  25. *
  26. * Single client TCP ECHO server
  27. * -----------------------------
  28. *
  29. * Written by Andre Asselin
  30. *
  31. *
  32. * Description
  33. * -----------
  34. *
  35. * This program implements the server side of the Echo protocol (RFC 862).  The
  36. * Echo protocol is very simple: whatever the client sends to the server, the
  37. * server will send right back to the client.
  38. *
  39. * This version of the server services only 1 TCP echo client at a time.
  40. *
  41. * The complete RFC is reproduced below:
  42. *
  43.  
  44. Network Working Group                                          J. Postel
  45. Request for Comments: 862                                            ISI
  46.                                                                 May 1983
  47.  
  48.                              Echo Protocol
  49.  
  50. This RFC specifies a standard for the ARPA Internet community.  Hosts on
  51. the ARPA Internet that choose to implement an Echo Protocol are expected
  52. to adopt and implement this standard.
  53.  
  54. A very useful debugging and measurement tool is an echo service.  An
  55. echo service simply sends back to the originating source any data it
  56. receives.
  57.  
  58. TCP Based Echo Service
  59.  
  60.    One echo service is defined as a connection based application on TCP.
  61.    A server listens for TCP connections on TCP port 7.  Once a
  62.    connection is established any data received is sent back.  This
  63.    continues until the calling user terminates the connection.
  64.  
  65. UDP Based Echo Service
  66.  
  67.    Another echo service is defined as a datagram based application on
  68.    UDP.  A server listens for UDP datagrams on UDP port 7.  When a
  69.    datagram is received, the data from it is sent back in an answering
  70.    datagram.
  71.  
  72. **************************************************************************/
  73.  
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76.  
  77. #include <types.h>
  78. #include <netinet\in.h>
  79. #include <sys\socket.h>
  80. #include <netdb.h>
  81. #include <sys\ioctl.h>
  82. #include <sys\time.h>
  83. #include <unistd.h>
  84. #include <arpa\inet.h>
  85. #include <nerrno.h>
  86.  
  87.  
  88. void main(int argc, char **argv) {
  89.    int listen_sock, newsock;
  90.    struct servent *echoprot;
  91.    struct sockaddr_in server, client;
  92.    int rc, len;
  93.    int asize, i;
  94.    char buf[1024];
  95.  
  96.    // Initialize TCP/IP
  97.  
  98.    if (sock_init() != 0) {
  99.       printf("INET.SYS probably is not running");
  100.       exit(1);
  101.    }
  102.  
  103. //------ Create the TCP server socket
  104.  
  105.    // Create a TCP socket to accept incoming connections
  106.  
  107.    listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  108.    if (listen_sock == -1) {
  109.        psock_errno("tcp socket()");
  110.        exit(1);
  111.    }
  112.  
  113.    printf("TCP socket assigned is %d\n", listen_sock);
  114.  
  115. //------ Bind the TCP socket to the echo port
  116.  
  117.    // Get the port for the echo protocol out of the etc\services file
  118.  
  119.    echoprot = getservbyname("echo", "tcp");
  120.    if (echoprot == NULL) {
  121.       printf("The echo/tcp protocol is not listed in the etc/services file\n");
  122.       exit(1);
  123.    }
  124.  
  125.    server.sin_family = AF_INET;
  126.    server.sin_port = echoprot->s_port;
  127.    server.sin_addr.s_addr = INADDR_ANY;
  128.  
  129.    rc = bind(listen_sock, (struct sockaddr *)&server, sizeof(server));
  130.    if (rc == -1) {
  131.        psock_errno("tcp bind()");
  132.        exit(1);
  133.    }
  134.  
  135. //------ Put the socket into listen mode
  136.  
  137.    rc = listen(listen_sock, SOMAXCONN);
  138.    if (rc == -1) {
  139.        psock_errno("listen()");
  140.        exit(1);
  141.    }
  142.  
  143.  
  144.    while (1) {
  145.  
  146. //------ Wait for a connection on the TCP socket
  147.  
  148.       asize = sizeof(client);
  149.       newsock = accept(listen_sock, (struct sockaddr *)&client, &asize);
  150.       if (newsock == -1) {
  151.          psock_errno("accept()");
  152.          exit(1);
  153.       }
  154.  
  155.       printf("Received a TCP connection on socket %d from %s port %d\n",
  156.              newsock, inet_ntoa(client.sin_addr), ntohs(client.sin_port));
  157.  
  158.       while (1) {
  159.  
  160. //------ Read the data
  161.  
  162.          len = recv(newsock, buf, sizeof(buf), 0);
  163.          if (len == -1) {
  164.             psock_errno("recv()");
  165.             exit(1);
  166.          }
  167.  
  168.          if (len == 0) {
  169.             printf("The client broke the connection\n");
  170.             soclose(newsock);
  171.             break;
  172.          }
  173.  
  174.          printf("Received %d bytes\n", len);
  175.  
  176. //------ Echo it back
  177.  
  178.          rc = send(newsock, buf, len, 0);
  179.          if (rc == -1) {
  180.             psock_errno("send()");
  181.             exit(1);
  182.          }
  183.       }
  184.  
  185.    }
  186.  
  187. }
  188.