home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / tcpiptk / socket / udps.c < prev    next >
Text File  |  1996-11-19  |  5KB  |  122 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 <stdio.h>
  26. #include <stdlib.h>
  27. #include <types.h>
  28. #include <netinet/in.h>
  29. #include <sys/socket.h>
  30.  
  31. main(void)
  32. {
  33.    int sockint,s, namelen, client_address_size;
  34.    struct sockaddr_in client, server;
  35.    char buf[32];
  36.  
  37.    /*
  38.     * Initialize with sockets.
  39.     */
  40.    if ((sockint = sock_init()) != 0)
  41.    {
  42.       printf(" INET.SYS probably is not running");
  43.       exit(1);
  44.    }
  45.  
  46.    /*
  47.     * Create a datagram socket in the internet domain and use the
  48.     * default protocol (UDP).
  49.     */
  50.    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  51.    {
  52.        psock_errno("socket()");
  53.        exit(1);
  54.    }
  55.  
  56.    /*
  57.     *
  58.     * Bind my name to this socket so that clients on the network can
  59.     * send me messages. (This allows the operating system to demultiplex
  60.     * messages and get them to the correct server)
  61.     *
  62.     * Set up the server name. The internet address is specified as the
  63.     * wildcard INADDR_ANY so that the server can get messages from any
  64.     * of the physical internet connections on this host. (Otherwise we
  65.     * would limit the server to messages from only one network interface)
  66.     */
  67.    server.sin_family      = AF_INET;   /* Server is in Internet Domain */
  68.    server.sin_port        = 0;         /* Use any available port       */
  69.    server.sin_addr.s_addr = INADDR_ANY;/* Server's Internet Address    */
  70.  
  71.    if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
  72.    {
  73.        psock_errno("bind()");
  74.        exit(2);
  75.    }
  76.  
  77.    /* Find out what port was really assigned and print it */
  78.    namelen = sizeof(server);
  79.    if (getsockname(s, (struct sockaddr *) &server, &namelen) < 0)
  80.    {
  81.        psock_errno("getsockname()");
  82.        exit(3);
  83.    }
  84.  
  85.    printf("Port assigned is %d\n", ntohs(server.sin_port));
  86.  
  87.    /*
  88.     * Receive a message on socket s in buf  of maximum size 32
  89.     * from a client. Because the last two paramters
  90.     * are not null, the name of the client will be placed into the
  91.     * client data structure and the size of the client address will
  92.     * be placed into client_address_size.
  93.     */
  94.    client_address_size = sizeof(client);
  95.  
  96.    if(recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &client,
  97.             &client_address_size) <0)
  98.    {
  99.        psock_errno("recvfrom()");
  100.        exit(4);
  101.    }
  102.    /*
  103.     * Print the message and the name of the client.
  104.     * The domain should be the internet domain (AF_INET).
  105.     * The port is received in network byte order, so we translate it to
  106.     * host byte order before printing it.
  107.     * The internet address is received as 32 bits in network byte order
  108.     * so we use a utility that converts it to a string printed in
  109.     * dotted decimal format for readability.
  110.     */
  111.    printf("Received message %s from domain %s port %d internet address %s\n",
  112.        buf,
  113.        (client.sin_family == AF_INET?"AF_INET":"UNKNOWN"),
  114.        ntohs(client.sin_port),
  115.        inet_ntoa(client.sin_addr));
  116.  
  117.    /*
  118.     * Deallocate the socket.
  119.     */
  120.    soclose(s);
  121. }
  122.