home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / tcpiptk / socket / tcps.c < prev    next >
Text File  |  1996-11-19  |  4KB  |  134 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1990,1991.                              */
  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.  * Include Files.
  26.  */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <types.h>
  30. #include <netinet\in.h>
  31. #include <sys\socket.h>
  32.  
  33.  
  34. /*
  35.  * Server Main.
  36.  */
  37. main(int argc, char *argv[])
  38. {
  39.     unsigned short port;       /* port server binds to                  */
  40.     char buf[12];              /* buffer for sending and receiving data */
  41.     struct sockaddr_in client; /* client address information            */
  42.     struct sockaddr_in server; /* server address information            */
  43.     int s;                     /* socket for accepting connections      */
  44.     int ns;                    /* socket connected to client            */
  45.     int namelen;               /* length of client name                 */
  46.  
  47.     /*
  48.      * Check arguments. Should be only one: the port number to bind to.
  49.      */
  50.  
  51.     if (argc != 2)
  52.     {
  53.         fprintf(stderr, "Usage: %s port\n", argv[0]);
  54.         exit(1);
  55.     }
  56.  
  57.     /*
  58.      * Initialize with sockets.
  59.      */
  60.     sock_init();
  61.  
  62.     /*
  63.      * First argument should be the port.
  64.      */
  65.     port = (unsigned short) atoi(argv[1]);
  66.  
  67.     /*
  68.      * Get a socket for accepting connections.
  69.      */
  70.     if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  71.     {
  72.         psock_errno("Socket()");
  73.         exit(2);
  74.     }
  75.  
  76.     /*
  77.      * Bind the socket to the server address.
  78.      */
  79.     server.sin_family = AF_INET;
  80.     server.sin_port   = htons(port);
  81.     server.sin_addr.s_addr = INADDR_ANY;
  82.  
  83.     if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
  84.     {
  85.        psock_errno("Bind()");
  86.        exit(3);
  87.     }
  88.  
  89.     /*
  90.      * Listen for connections. Specify the backlog as 1.
  91.      */
  92.     if (listen(s, 1) != 0)
  93.     {
  94.         psock_errno("Listen()");
  95.         exit(4);
  96.     }
  97.  
  98.     /*
  99.      * Accept a connection.
  100.      */
  101.     namelen = sizeof(client);
  102.     if ((ns = accept(s, (struct sockaddr *)&client, &namelen)) == -1)
  103.     {
  104.         psock_errno("Accept()");
  105.         exit(5);
  106.     }
  107.  
  108.     /*
  109.      * Receive the message on the newly connected socket.
  110.      */
  111.     if (recv(ns, buf, sizeof(buf), 0) == -1)
  112.     {
  113.         psock_errno("Recv()");
  114.         exit(6);
  115.     }
  116.  
  117.     /*
  118.      * Send the message back to the client.
  119.      */
  120.     if (send(ns, buf, sizeof(buf), 0) < 0)
  121.     {
  122.         psock_errno("Send()");
  123.         exit(7);
  124.     }
  125.  
  126.     soclose(ns);
  127.     soclose(s);
  128.  
  129.     printf("Server ended successfully\n");
  130.     exit(0);
  131. }
  132.  
  133.  
  134.