home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / echosamp / echoclnt / echoclnt.c < prev    next >
Text File  |  1999-05-11  |  7KB  |  194 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 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. /*--------------------------------------------------------------------------*/
  25.  
  26. /******************************************************************************
  27. *
  28. * An alternative version of the ECHO client
  29. * -----------------------------------------
  30. *
  31. * Written by Andre Asselin
  32. *
  33. * For use with "Introduction to sockets programming with IBM TCP/IP for OS/2"
  34. *
  35. *
  36. * Description
  37. * -----------
  38. *
  39. * This program implements the client side of the Echo protocol (RFC 862).  The
  40. * Echo protocol is very simple: whatever the client sends to the server, the
  41. * server will send right back to the client.  The complete RFC is reproduced
  42. * below:
  43. *
  44.  
  45. Network Working Group                                          J. Postel
  46. Request for Comments: 862                                            ISI
  47.                                                                 May 1983
  48.  
  49.                              Echo Protocol
  50.  
  51. This RFC specifies a standard for the ARPA Internet community.  Hosts on
  52. the ARPA Internet that choose to implement an Echo Protocol are expected
  53. to adopt and implement this standard.
  54.  
  55. A very useful debugging and measurement tool is an echo service.  An
  56. echo service simply sends back to the originating source any data it
  57. receives.
  58.  
  59. TCP Based Echo Service
  60.  
  61.    One echo service is defined as a connection based application on TCP.
  62.    A server listens for TCP connections on TCP port 7.  Once a
  63.    connection is established any data received is sent back.  This
  64.    continues until the calling user terminates the connection.
  65.  
  66. UDP Based Echo Service
  67.  
  68.    Another echo service is defined as a datagram based application on
  69.    UDP.  A server listens for UDP datagrams on UDP port 7.  When a
  70.    datagram is received, the data from it is sent back in an answering
  71.    datagram.
  72.  
  73. *
  74. ******************************************************************************/
  75.  
  76.  
  77. #include <stdlib.h>
  78. #include <stdio.h>
  79. #include <string.h>
  80.  
  81. #include <types.h>
  82. #include <netinet/in.h>
  83. #include <sys/socket.h>
  84. #include <netdb.h>
  85. #include <arpa\inet.h>
  86.  
  87. void main(int argc, char **argv) {
  88.    struct sockaddr_in server;
  89.    struct hostent *hp;
  90.    char buf[100];
  91.    struct servent *echoprot;
  92.    int sock, len, rc;
  93.  
  94.    // Initialize TCP/IP
  95.  
  96.    if (sock_init() != 0) {
  97.       printf("INET.SYS probably is not running");
  98.       exit(1);
  99.    }
  100.  
  101.    // Check to make sure the user passed the right number of arguments
  102.  
  103.    if (argc != 3  ||  argc > 1 && argv[1][0] == '?') {
  104.       printf("Usage:\n"
  105.              "──echoclnt──┬─ TCP ─┬─<host>──\n"
  106.              "              └─ UDP ─┘\n"
  107.              "\n"
  108.              "TCP    Specifies to use TCP as the connection protocol.\n"
  109.              "UDP    Specifies to use UDP as the connection protocol.\n"
  110.              "<host> Specifies the remote host on which the command is to be issued.\n");
  111.       exit(1);
  112.    }
  113.  
  114.    // Open the correct type of socket
  115.  
  116.    if (stricmp(argv[1], "TCP") == 0) {
  117.       sock = socket(AF_INET, SOCK_STREAM, 0);
  118.       if (sock == -1) {
  119.          psock_errno("socket()");
  120.          exit(1);
  121.       }
  122.    } else if (stricmp(argv[1], "UDP") == 0) {
  123.       sock = socket(AF_INET, SOCK_DGRAM, 0);
  124.       if (sock == -1) {
  125.          psock_errno("socket()");
  126.          exit(1);
  127.       }
  128.    } else {
  129.       printf("You must specify either 'tcp' or 'udp' as the connection protocol.\n");
  130.       exit(1);
  131.    }
  132.  
  133.    // Set the server address from argument 2 -- we accept both domain names
  134.    // and IP addresses
  135.  
  136.    server.sin_addr.s_addr = inet_addr(argv[2]);
  137.    if (server.sin_addr.s_addr == -1) {
  138.       // It wasn't a dotted decimal address -- assume it's a domain name,
  139.       // and use gethostbyname() to convert it
  140.  
  141.       hp = gethostbyname(argv[2]);
  142.       if (hp) {
  143.          server.sin_family = hp->h_addrtype;
  144.          memcpy(&server.sin_addr, hp->h_addr, sizeof(server.sin_addr));
  145.       } else {
  146.          printf("Unknown host %s\n", argv[2]);
  147.          exit(1);
  148.       }
  149.    }
  150.  
  151.    // Get the port for the echo protocol out of the etc\services file
  152.  
  153.    echoprot = getservbyname("echo", argv[1]);
  154.    if (echoprot == NULL) {
  155.       printf("The echo/%s protocol is not listed in the etc/services file\n", argv[1]);
  156.       exit(1);
  157.    }
  158.  
  159.    // Set the rest of the server address
  160.  
  161.    server.sin_family = AF_INET;
  162.    server.sin_port = echoprot->s_port;
  163.  
  164.    // Connect to the server
  165.  
  166.    rc = connect(sock, (struct sockaddr *)&server, sizeof(server));
  167.    if (rc == -1) {
  168.       psock_errno("connect()");
  169.       exit(1);
  170.    }
  171.  
  172.    while (1) {
  173.       if (gets(buf) == NULL) {
  174.          printf("gets() returned NULL\n");
  175.          exit(1);
  176.       }
  177.  
  178.       rc = send(sock, buf, strlen(buf), 0);
  179.       if (rc == -1) {
  180.          psock_errno("send()");
  181.          exit(1);
  182.       }
  183.  
  184.       len = recv(sock, buf, sizeof(buf), 0);
  185.       if (len == -1) {
  186.          psock_errno("recv()");
  187.          exit(1);
  188.       }
  189.  
  190.       buf[len] = '\0';
  191.       printf("%s\n", buf);
  192.    }
  193. }
  194.