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