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