home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / socket / msgc.c next >
Text File  |  1999-05-11  |  4KB  |  108 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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <types.h>
  28. #include <netinet/in.h>
  29. #include <sys/socket.h>
  30. #include <arpa/inet.h>
  31. #include <sys/uio.h>
  32. #include <sys/time.h>
  33. #include <unistd.h>
  34.  
  35. main(int argc, char *argv[])
  36. {
  37.  
  38.  
  39.    int s,bytesend;
  40.    unsigned short port;
  41.    struct sockaddr_in server;
  42.    struct msghdr msg;
  43.    struct iovec iov[5];
  44.    char buf[64],buf1[64],buf2[64],buf3[64],buf4[64];
  45.  
  46.    /* argv[1] is internet address of server argv[2] is port of server.
  47.     * Convert the port from ascii to integer and then from host byte
  48.     * order to network byte order.
  49.     */
  50.    if(argc != 3)
  51.    {
  52.       printf("Usage: %s <host address> <port> \n",argv[0]);
  53.       exit(1);
  54.    }
  55.    port = htons(atoi(argv[2]));
  56.  
  57.    /* Initialize with sockets */
  58.    sock_init();
  59.  
  60.    /* Create a datagram socket in the internet domain and use the
  61.     * default protocol (UDP).
  62.     */
  63.    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  64.    {
  65.        psock_errno("socket()");
  66.        exit(1);
  67.    }
  68.  
  69.    /* Set up the server name */
  70.    server.sin_family      = AF_INET;            /* Internet Domain    */
  71.    server.sin_port        = port;               /* Server Port        */
  72.    server.sin_addr.s_addr = inet_addr(argv[1]); /* Server's Address   */
  73.  
  74.    strcpy(buf, "Hello, I am Gaurang Shah");
  75.    strcpy(buf1, "Hello *1*, I am William R. SNOW");
  76.    strcpy(buf2, "Hello *2*, I am James Willam");
  77.    strcpy(buf3, "Hello *3*, I am GLENN STUMP");
  78.    strcpy(buf4, "Hello *4*, I am paul seifert");
  79.  
  80.    iov[0].iov_base = &buf[0];
  81.    iov[0].iov_len = sizeof(buf);
  82.    iov[1].iov_base = &buf1[0];
  83.    iov[1].iov_len = sizeof(buf1);
  84.    iov[2].iov_base = &buf2[0];
  85.    iov[2].iov_len = sizeof(buf2);
  86.    iov[3].iov_base = &buf3[0];
  87.    iov[3].iov_len = sizeof(buf3);
  88.    iov[4].iov_base = &buf4[0];
  89.    iov[4].iov_len = sizeof(buf4);
  90.  
  91.    msg.msg_name = (caddr_t) &server;
  92.    msg.msg_namelen = sizeof(server);
  93.  
  94.    msg.msg_iov = &iov[0];
  95.    msg.msg_iovlen = 5;
  96.  
  97.    /* Send the message in buf to the server */
  98.  
  99.    if ((bytesend=sendmsg(s, &msg, 0)) < 0)
  100.     {
  101.         psock_errno("sendmsg()");
  102.         exit(2);
  103.     }
  104.  
  105.    /* Deallocate the socket */
  106.    soclose(s);
  107. }
  108.