home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / socket / udpc.c < prev    next >
Text File  |  1999-05-11  |  3KB  |  85 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 <string.h>
  28. #include <types.h>
  29. #include <netinet/in.h>
  30. #include <sys/socket.h>
  31. #include <arpa/inet.h>
  32. #include <sys/time.h>
  33. #include <unistd.h>
  34.  
  35. main(int argc, char *argv[])
  36. {
  37.  
  38.  
  39.    int s;
  40.    unsigned short port;
  41.    struct sockaddr_in server;
  42.    char buf[32];
  43.  
  44.    /* argv[1] is internet address of server argv[2] is port of server.
  45.     * Convert the port from ascii to integer and then from host byte
  46.     * order to network byte order.
  47.     */
  48.    if(argc != 3)
  49.    {
  50.       printf("Usage: %s <host address> <port> \n",argv[0]);
  51.       exit(1);
  52.    }
  53.   port = htons(atoi(argv[2]));
  54.  
  55.    /* Initialize with sockets */
  56.    sock_init();
  57.  
  58.  
  59.    /* Create a datagram socket in the internet domain and use the
  60.     * default protocol (UDP).
  61.     */
  62.    if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  63.    {
  64.        psock_errno("socket()");
  65.        exit(1);
  66.    }
  67.  
  68.    /* Set up the server name */
  69.    server.sin_family      = AF_INET;            /* Internet Domain    */
  70.    server.sin_port        = port;               /* Server Port        */
  71.    server.sin_addr.s_addr = inet_addr(argv[1]); /* Server's Address   */
  72.  
  73.    strcpy(buf, "Hello");
  74.  
  75.    /* Send the message in buf to the server */
  76.    if (sendto(s, buf, (strlen(buf)+1), 0, (struct sockaddr *)&server, sizeof(server)) < 0)
  77.    {
  78.        psock_errno("sendto()");
  79.        exit(2);
  80.    }
  81.  
  82.    /* Deallocate the socket */
  83.    soclose(s);
  84. }
  85.