home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / socket / msgs.c < prev    next >
Text File  |  1999-05-11  |  5KB  |  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, 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. /*   Modification History:                                                  */
  25. /*   Date:     By:   Tag:  Desc:                                            */
  26. /*   09.30.96  DRC         removed unreferenced variables                   */
  27. /****************************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <types.h>
  32. #include <netinet/in.h>
  33. #include <sys/socket.h>
  34. #include <sys/time.h>
  35. #include <unistd.h>
  36. #include <sys/uio.h>
  37.  
  38. main(void)
  39. {
  40.    int byterecv,sockint,s, namelen;
  41.    struct sockaddr_in client, server;
  42.    struct msghdr msg;
  43.    struct iovec iov[5];
  44.    char buf[64],buf1[64],buf2[64],buf3[64],buf4[64];
  45.  
  46.    /*
  47.     * Initialize with sockets.
  48.     */
  49.    if ((sockint = sock_init()) != 0)
  50.    {
  51.       printf(" INET.SYS probably is not running");
  52.       exit(1);
  53.    }
  54.  
  55.    /*
  56.     * Create a datagram socket in the internet domain and use the
  57.     * default protocol (UDP).
  58.     */
  59.    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  60.    {
  61.        psock_errno("socket()");
  62.        exit(1);
  63.    }
  64.  
  65.    /*
  66.     *
  67.     * Bind my name to this socket so that clients on the network can
  68.     * send me messages. (This allows the operating system to demultiplex
  69.     * messages and get them to the correct server)
  70.     *
  71.     * Set up the server name. The internet address is specified as the
  72.     * wildcard INADDR_ANY so that the server can get messages from any
  73.     * of the physical internet connections on this host. (Otherwise we
  74.     * would limit the server to messages from only one network interface)
  75.     */
  76.    server.sin_family      = AF_INET;   /* Server is in Internet Domain */
  77.    server.sin_port        = 0;         /* Use any available port       */
  78.    server.sin_addr.s_addr = INADDR_ANY;/* Server's Internet Address    */
  79.  
  80.    if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
  81.    {
  82.        psock_errno("bind()");
  83.        exit(2);
  84.    }
  85.  
  86.    /* Find out what port was really assigned and print it */
  87.    namelen = sizeof(server);
  88.    if (getsockname(s, (struct sockaddr *) &server, &namelen) < 0)
  89.    {
  90.        psock_errno("getsockname()");
  91.        exit(3);
  92.    }
  93.  
  94.    printf("Port assigned is %d\n", ntohs(server.sin_port));
  95.  
  96.  
  97.    iov[0].iov_base = &buf[0];
  98.    iov[0].iov_len = sizeof(buf);
  99.    iov[1].iov_base = &buf1[0];
  100.    iov[1].iov_len = sizeof(buf1);
  101.    iov[2].iov_base = &buf2[0];
  102.    iov[2].iov_len = sizeof(buf2);
  103.    iov[3].iov_base = &buf3[0];
  104.    iov[3].iov_len = sizeof(buf3);
  105.    iov[4].iov_base = &buf4[0];
  106.    iov[4].iov_len = sizeof(buf4);
  107.  
  108.    msg.msg_name = (caddr_t)&client;
  109.    msg.msg_namelen = sizeof(client);
  110.  
  111.    msg.msg_iov = &iov[0];
  112.    msg.msg_iovlen = 5;
  113.  
  114.    if((byterecv=recvmsg(s, &msg, 0))<0)
  115.    {
  116.        psock_errno("recvmsg()");
  117.        exit(4);
  118.    }
  119.    /*
  120.     * Print the message received.
  121.     */
  122.    printf("%s \n", buf);
  123.    printf("%s \n", buf1);
  124.    printf("%s \n", buf2);
  125.    printf("%s \n", buf3);
  126.    printf("%s \n", buf4);
  127.  
  128.    /*
  129.     * Deallocate the socket.
  130.     */
  131.    soclose(s);
  132. }
  133.  
  134.