home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / iapp300.zip / SAMPLES / TCPECHO.C < prev    next >
C/C++ Source or Header  |  1995-03-01  |  6KB  |  182 lines

  1. /************************************************************************/
  2. /*                    TCP/IP Socket Sample Program                                         */
  3. /************************************************************************/
  4. /* Function        Client / Server sample program using TCP/IP socket calls.*/
  5. /* Author        (c) Copyright Infoline AG 1995                                    */
  6. /*                    Schaffhauserstrasse 121                                                */
  7. /*                    CH-8302 Kloten - Switzerland                                        */
  8. /*                    Phone: +41 1 803 07 06 / Fax: +41 1 881 17 55                */
  9. /* History        V3.00 01/Mar/1995 Andy Brunner    Initial program version    */
  10. /************************************************************************/
  11.  
  12. /************************************************************************/
  13. /*                    Do not forget to add the service entry in \ETC\SERVICES    */
  14. /*                    e.g. iappecho 5000/tcp                                                */
  15. /************************************************************************/
  16.  
  17. #define    INCL_BASE
  18. #define    USER_OS2
  19.  
  20. #include "user.h"                                /* Include necessary files            */
  21. #include "iapp.h"                                /* Include necessary files            */
  22.  
  23. /*----------------------------------------------------------------------*/
  24. /*    Function prototypes                                                                    */
  25. /*----------------------------------------------------------------------*/
  26.  
  27. VOID    ErrorHandler(P_IAPP_SOCKET pxControl);
  28.  
  29. /*======================================================================*/
  30. /*    Function: main                                                                             */ 
  31. /*======================================================================*/
  32.  
  33. SHORT main(SHORT sArgCounter, PUCHAR *pszArgVariables)
  34. {
  35.     /*-------------------------------------------------------------------*/
  36.     /* Automatic variables                                                                 */ 
  37.     /*-------------------------------------------------------------------*/
  38.  
  39.     P_IAPP_SOCKET        pxControl                            = NULL;
  40.  
  41.     UCHAR                    Buffer[100];
  42.  
  43.     PUCHAR                pszMessage                            = "Test Message";
  44.  
  45.     USHORT                usCounter                            = 0;
  46.     USHORT                usBufferSize                        = 0;
  47.  
  48.     /*-------------------------------------------------------------------*/
  49.     /* Display program title                                                            */
  50.     /*-------------------------------------------------------------------*/
  51.  
  52.     printf("TCP/IP Socket Sample Program Version 3.00 - (c) Copyright Infoline AG 1995\n");
  53.     printf("──────────────────────────────────────────────────────────────────────────\n");
  54.     printf("\n");
  55.  
  56.     /*-------------------------------------------------------------------*/
  57.     /* Check program arguments                                                            */
  58.     /*-------------------------------------------------------------------*/
  59.  
  60.     if ((sArgCounter == 1) || (sArgCounter > 3))
  61.     {
  62.         printf("Usage: TCPEcho ServiceName           (for Server)\n");
  63.         printf("       TCPEcho ServiceName HostName  (for Client)\n");
  64.         exit(4);
  65.     }
  66.  
  67.     /*-------------------------------------------------------------------*/
  68.     /* Initialize TCP/IP socket connection                                            */
  69.     /*-------------------------------------------------------------------*/
  70.  
  71.     if (sArgCounter == 2)
  72.     {
  73.         /*----------------------------------------------------------------*/
  74.         /* Initialize server                                                               */
  75.         /*----------------------------------------------------------------*/
  76.  
  77.         printf("Server: Waiting for client to connect.\n");
  78.  
  79.         if (!IAppSocketOpen(&pxControl, NULL, pszArgVariables[1]))
  80.             ErrorHandler(pxControl);
  81.         
  82.         printf("Server: Connected with client.\n");
  83.     }
  84.     else
  85.     {
  86.         /*----------------------------------------------------------------*/
  87.         /* Initialize client                                                               */
  88.         /*----------------------------------------------------------------*/
  89.  
  90.         printf("Client: Waiting for server to answer.\n");
  91.  
  92.         if (!IAppSocketOpen(&pxControl, pszArgVariables[2], pszArgVariables[1]))
  93.             ErrorHandler(pxControl);
  94.         
  95.         printf("Client: Connected with server.\n");
  96.     }
  97.  
  98.     /*-------------------------------------------------------------------*/
  99.     /* Perform data transfer                                                            */
  100.     /*-------------------------------------------------------------------*/
  101.  
  102.     if (sArgCounter == 3)
  103.     {
  104.         /*----------------------------------------------------------------*/
  105.         /*    Client: Send data to server and read answer                            */
  106.         /*----------------------------------------------------------------*/
  107.  
  108.         while (++usCounter < 4)
  109.         {
  110.             if (!IAppSocketWrite(pxControl, pszMessage, strlen(pszMessage)))
  111.                 ErrorHandler(pxControl);
  112.             
  113.             printf("Client: Sent to server <%s>.\n", pszMessage);
  114.  
  115.             Buffer[0] = '\0';
  116.             usBufferSize = 100;
  117.  
  118.             if (!IAppSocketRead(pxControl, Buffer, &usBufferSize))
  119.                 ErrorHandler(pxControl);
  120.  
  121.             Buffer[usBufferSize] = '\0';
  122.             
  123.             printf("Client: Received from server <%s>.\n", Buffer);
  124.         }
  125.     }
  126.     else
  127.     {
  128.         /*----------------------------------------------------------------*/
  129.         /*    Server: Read client data and echo back to client as uppercase    */
  130.         /*----------------------------------------------------------------*/
  131.  
  132.         while (TRUE)
  133.         {
  134.             Buffer[0] = '\0';
  135.             usBufferSize = 100;
  136.  
  137.             if (!IAppSocketRead(pxControl, Buffer, &usBufferSize))
  138.                 ErrorHandler(pxControl);
  139.  
  140.             Buffer[usBufferSize] = '\0';
  141.  
  142.             printf("Server: Received from client <%s>.\n", Buffer);
  143.  
  144.             IAppUpperCase(Buffer);
  145.  
  146.             if (!IAppSocketWrite(pxControl, Buffer, strlen(Buffer)))
  147.                 ErrorHandler(pxControl);
  148.  
  149.             printf("Server: Sent to client <%s>.\n", Buffer);
  150.         }
  151.     }
  152.  
  153.     /*-------------------------------------------------------------------*/
  154.     /* Terminate TCP/IP socket connection                                            */
  155.     /*-------------------------------------------------------------------*/
  156.  
  157.     if (!IAppSocketClose(pxControl))
  158.         ErrorHandler(pxControl);
  159.  
  160.     /*-------------------------------------------------------------------*/
  161.     /*    Return to the caller                                                                  */
  162.     /*-------------------------------------------------------------------*/
  163.  
  164.     return (0);
  165. }
  166.  
  167. /*======================================================================*/
  168. /*    Function: ErrorHandler                                                                 */ 
  169. /*======================================================================*/
  170.  
  171. VOID ErrorHandler(P_IAPP_SOCKET pxControl)
  172. {
  173.     printf("\n");
  174.     printf("%s\n", pxControl->szErrorMessage);
  175.  
  176.     IAppBeep();
  177.  
  178.     exit(4);
  179. }
  180.  
  181.  
  182.