home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / tcpiptk / socket / udpc.c < prev    next >
Text File  |  1996-11-19  |  3KB  |  82 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.  
  32. main(int argc, char *argv[])
  33. {
  34.  
  35.  
  36.    int s;
  37.    unsigned short port;
  38.    struct sockaddr_in server;
  39.    char buf[32];
  40.  
  41.    /* argv[1] is internet address of server argv[2] is port of server.
  42.     * Convert the port from ascii to integer and then from host byte
  43.     * order to network byte order.
  44.     */
  45.    if(argc != 3)
  46.    {
  47.       printf("Usage: %s <host address> <port> \n",argv[0]);
  48.       exit(1);
  49.    }
  50.   port = htons(atoi(argv[2]));
  51.  
  52.    /* Initialize with sockets */
  53.    sock_init();
  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.    /* Set up the server name */
  66.    server.sin_family      = AF_INET;            /* Internet Domain    */
  67.    server.sin_port        = port;               /* Server Port        */
  68.    server.sin_addr.s_addr = inet_addr(argv[1]); /* Server's Address   */
  69.  
  70.    strcpy(buf, "Hello");
  71.  
  72.    /* Send the message in buf to the server */
  73.    if (sendto(s, buf, (strlen(buf)+1), 0, (struct sockaddr *)&server, sizeof(server)) < 0)
  74.    {
  75.        psock_errno("sendto()");
  76.        exit(2);
  77.    }
  78.  
  79.    /* Deallocate the socket */
  80.    soclose(s);
  81. }
  82.