home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / echosamp / echoclnt / echoc1st.c < prev    next >
Text File  |  1999-05-11  |  8KB  |  247 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. * ECHO client
  29. * -----------
  30. *
  31. * Written by Andre Asselin
  32. *
  33. *
  34. * Description
  35. * -----------
  36. *
  37. * This program implements the client side of the Echo protocol (RFC 862).  The
  38. * Echo protocol is very simple: whatever the client sends to the server, the
  39. * server will send right back to the client.  The complete RFC is reproduced
  40. * below:
  41. *
  42.  
  43. Network Working Group                                          J. Postel
  44. Request for Comments: 862                                            ISI
  45.                                                                 May 1983
  46.  
  47.                              Echo Protocol
  48.  
  49. This RFC specifies a standard for the ARPA Internet community.  Hosts on
  50. the ARPA Internet that choose to implement an Echo Protocol are expected
  51. to adopt and implement this standard.
  52.  
  53. A very useful debugging and measurement tool is an echo service.  An
  54. echo service simply sends back to the originating source any data it
  55. receives.
  56.  
  57. TCP Based Echo Service
  58.  
  59.    One echo service is defined as a connection based application on TCP.
  60.    A server listens for TCP connections on TCP port 7.  Once a
  61.    connection is established any data received is sent back.  This
  62.    continues until the calling user terminates the connection.
  63.  
  64. UDP Based Echo Service
  65.  
  66.    Another echo service is defined as a datagram based application on
  67.    UDP.  A server listens for UDP datagrams on UDP port 7.  When a
  68.    datagram is received, the data from it is sent back in an answering
  69.    datagram.
  70.  
  71. *
  72. ******************************************************************************/
  73.  
  74.  
  75. #include <stdlib.h>
  76. #include <stdio.h>
  77. #include <string.h>
  78.  
  79. #include <types.h>
  80. #include <netinet/in.h>
  81. #include <sys/socket.h>
  82. #include <netdb.h>
  83. #include <arpa\inet.h>
  84.  
  85. void TCPClient(struct sockaddr_in *server);
  86. void UDPClient(struct sockaddr_in *server);
  87.  
  88.  
  89. void main(int argc, char **argv) {
  90.    struct sockaddr_in server;
  91.    struct hostent *hp;
  92.  
  93.    // Initialize TCP/IP
  94.  
  95.    if (sock_init() != 0) {
  96.       printf("INET.SYS probably is not running");
  97.       exit(1);
  98.    }
  99.  
  100.    // Check to make sure the user passed the right number of arguments
  101.  
  102.    if (argc != 3  ||  argc > 1 && argv[1][0] == '?') {
  103.       printf("Usage:\n"
  104.              "──echoclnt──┬─ TCP ─┬─<host>──\n"
  105.              "              └─ UDP ─┘\n"
  106.              "\n"
  107.              "TCP    Specifies to use TCP as the connection protocol.\n"
  108.              "UDP    Specifies to use UDP as the connection protocol.\n"
  109.              "<host> Specifies the remote host on which the command is to be issued.\n");
  110.       exit(1);
  111.    }
  112.  
  113.    // Set the server address from argument 2 -- we accept both domain names
  114.    // and IP addresses
  115.  
  116.    server.sin_family = AF_INET;
  117.    server.sin_addr.s_addr = inet_addr(argv[2]);
  118.    if (server.sin_addr.s_addr == -1) {
  119.       // It wasn't a dotted decimal address -- assume it's a domain name,
  120.       // and use gethostbyname() to convert it
  121.  
  122.       hp = gethostbyname(argv[2]);
  123.       if (hp) {
  124.          server.sin_family = hp->h_addrtype;
  125.          memcpy(&server.sin_addr, hp->h_addr, sizeof(server.sin_addr));
  126.       } else {
  127.          printf("Unknown host %s\n", argv[2]);
  128.          exit(1);
  129.       }
  130.    }
  131.  
  132.    // Call the correct client routine
  133.  
  134.    if (stricmp(argv[1], "TCP") == 0) {
  135.       TCPClient(&server);
  136.    } else if (stricmp(argv[1], "UDP") == 0) {
  137.       UDPClient(&server);
  138.    } else {
  139.       printf("You must specify either 'tcp' or 'udp' as the connection protocol.\n");
  140.       exit(1);
  141.    }
  142. }
  143.  
  144.  
  145. void TCPClient(struct sockaddr_in *server) {
  146.    char buf[100];
  147.    struct servent *echoprot;
  148.    int sock, len, rc;
  149.  
  150.    // Open a TCP socket
  151.  
  152.    sock = socket(AF_INET, SOCK_STREAM, 0);
  153.    if (sock == -1) {
  154.       psock_errno("socket()");
  155.       exit(1);
  156.    }
  157.  
  158.    // Get the port for the echo protocol out of the etc\services file
  159.  
  160.    echoprot = getservbyname("echo", "tcp");
  161.    if (echoprot == NULL) {
  162.       printf("The echo/tcp protocol is not listed in the etc/services file\n");
  163.       exit(1);
  164.    }
  165.  
  166.    server->sin_port = echoprot->s_port;
  167.  
  168.    // Connect to the server
  169.  
  170.    rc = connect(sock, (struct sockaddr *)server, sizeof(*server));
  171.    if (rc == -1) {
  172.       psock_errno("connect()");
  173.       exit(1);
  174.    }
  175.  
  176.    while (1) {
  177.       if (gets(buf) == NULL) {
  178.          printf("gets() returned NULL\n");
  179.          exit(1);
  180.       }
  181.  
  182.       rc = send(sock, buf, strlen(buf), 0);
  183.       if (rc == -1) {
  184.          psock_errno("send()");
  185.          exit(1);
  186.       }
  187.  
  188.       len = recv(sock, buf, sizeof(buf), 0);
  189.       if (len == -1) {
  190.          psock_errno("recv()");
  191.          exit(1);
  192.       }
  193.  
  194.       buf[len] = '\0';
  195.       printf("%s\n", buf);
  196.    }
  197. }
  198.  
  199.  
  200. void UDPClient(struct sockaddr_in *server) {
  201.    struct sockaddr_in response;
  202.    char buf[100];
  203.    struct servent *echoprot;
  204.    int sock, len, rc, respsize;
  205.  
  206.    // Open a UDP socket
  207.  
  208.    sock = socket(AF_INET, SOCK_DGRAM, 0);
  209.    if (sock == -1) {
  210.       psock_errno("socket()");
  211.       exit(1);
  212.    }
  213.  
  214.    // Get the port for the echo protocol out of the etc\services file
  215.  
  216.    echoprot = getservbyname("echo", "udp");
  217.    if (echoprot == NULL) {
  218.       printf("The echo/udp protocol is not listed in the etc/services file\n");
  219.       exit(1);
  220.    }
  221.  
  222.    server->sin_port = echoprot->s_port;
  223.  
  224.    while (1) {
  225.       if (gets(buf) == NULL) {
  226.          printf("gets() returned NULL\n");
  227.          exit(1);
  228.       }
  229.  
  230.       rc = sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)server, sizeof(*server));
  231.       if (rc == -1) {
  232.          psock_errno("send()");
  233.          exit(1);
  234.       }
  235.  
  236.       respsize = sizeof(response);
  237.       len = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&response, &respsize);
  238.       if (len == -1) {
  239.          psock_errno("recv()");
  240.          exit(1);
  241.       }
  242.  
  243.       buf[len] = '\0';
  244.       printf("%s\n", buf);
  245.    }
  246. }
  247.