home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / chat / hello.c < prev    next >
C/C++ Source or Header  |  1989-03-22  |  4KB  |  144 lines

  1. /******************************************************************************
  2.  *
  3.  *    Program Name:  TALK_TO.ME
  4.  *
  5.  *    Filename:  HELLO.C -- 10/25/88  4:00
  6.  *
  7.  *    Purpose:  Establishes an SPX connection and receive an incoming packet
  8.  *
  9.  ******************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <nit.h>
  13. #include "chat.h"
  14.  
  15. extern    void    SendMessage();
  16. extern    void    TearDownConnection();
  17. extern    int        PollForPacket();
  18. extern    void    SetUpReceiveECBs();
  19. extern     ECB        *SetUpInitialECB();
  20. extern     void    DrawScreen();
  21. extern    void    Dispatcher();
  22. extern     void    ClearScreen();
  23. extern    void     SetUpSendECB();
  24.  
  25. extern NETWORK_NODE     destNode;
  26. extern WORD       SPXConnectionNumber,         /* SPXEstablishConnection returns in DX */
  27.                   Socket;
  28. extern int        ACTIVE_CONNECTION;
  29.  
  30.  
  31. void main(argc,argv)
  32. int        argc;
  33. char    *argv[];
  34. {
  35.     int        ccode = 0;
  36.     char    key = 0,
  37.             networkNumber[4];
  38.     WORD    connection;
  39.  
  40.  /* Test for the correct number of arguments on command line */ 
  41.     if (argc != 2) 
  42.     {
  43.         printf ("HELLO.EXE v1.01\n");
  44.         printf ("by Novell Technical Documentation\n\n");
  45.         printf ("Format: Hello station_number(hexidecimal)\n");
  46.         printf ("Example: Hello 3A\n");
  47.         exit (); 
  48.     }
  49.  
  50.  /* Get the target station number */
  51.      sscanf (argv[1], "%d", &connection);
  52.     GetInternetAddress (connection, networkNumber, (char *)&destNode);
  53.  
  54.  /* Format the screen */
  55.      DrawScreen ();
  56.  
  57.  /* Establish a connection */
  58.     ccode = EstablishListeningSide ();
  59.     if (ccode)
  60.             Error ("No answer.  Maybe they really didn't want to chat with you.");
  61.  
  62.  /* While connection is active, send or poll for messages */
  63.      Update ("Connection is established.");
  64.  
  65.     while (ACTIVE_CONNECTION)
  66.     {
  67.          while ( !kbhit () )
  68.         {
  69.             if ( (ACTIVE_CONNECTION = PollForPacket()) == 0)
  70.             {
  71.                  Update ("Your partner hung up.  Press ESCAPE twice to close.");
  72.                 break;
  73.             }
  74.         }
  75.  
  76.      /* A key was hit */
  77.          if ( (key = getch()) == ESCAPE)
  78.         {
  79.             Update ("Press ESCAPE to hang up / ENTER to resume.");
  80.             if ( (key = getch()) == ESCAPE)
  81.             {
  82.                 Update ("Closing connection...");
  83.                 ACTIVE_CONNECTION = 0;
  84.                 break;
  85.             }
  86.             Update ("Connection is active.");
  87.         }
  88.         else
  89.             Dispatcher (key);        
  90.      }
  91.  
  92.     TearDownConnection ();
  93. }
  94.  
  95.  
  96.  
  97. int    EstablishListeningSide ()
  98. {
  99.     int        ccode;
  100.     BYTE    retryCount = 0x00, watchDog = 0x00, majRevNumber, minRevNumber;
  101.     WORD    tempIDNumber, maxConnections, maxConnAvailable;
  102.     ECB        *initialECB;
  103.     char    key;
  104.  
  105.  /* See if SPX is installed */
  106.        Update ("Initializing SPX.",0);
  107.     ccode =    SPXInitialize (&majRevNumber,&minRevNumber,&maxConnections,
  108.                 &maxConnAvailable);
  109.     if (ccode != SPX_INSTALLED)
  110.         Error ("SPX is not installed.");
  111.  
  112.  /* Open an IPX socket */
  113.     ccode = IPXOpenSocket ( (BYTE *)&Socket, (BYTE)0 );
  114.     if (ccode)
  115.         Error ("Unable to open a socket.");
  116.  
  117.  /* Set up an ECB for listening and some ECBs for receiving */
  118.     Update ("Preparing packets.",0);
  119.      SetUpReceiveECBs ();
  120.      initialECB = SetUpInitialECB ();
  121.  
  122.  /* Have SPX begin listening for a connection */
  123.      Update ("Attempting a connection.",0);
  124.     SPXListenForConnection ( (BYTE)retryCount, (BYTE)watchDog, initialECB );
  125.  
  126.  /* If connection is successful, set up some ECBs for sending */
  127.     Update ("Attempting a connection.  Press ESCAPE to hang up.");
  128.      while ( !kbhit() && initialECB->inUseFlag)
  129.     {
  130.         /* WAIT HERE */;
  131.     }
  132.  
  133.     if (initialECB->completionCode == 0x00)
  134.     {
  135.         memcpy (&SPXConnectionNumber, initialECB->IPXWorkspace, 2);
  136.         SetUpSendECB ();
  137.         ccode = initialECB->completionCode;
  138.     }
  139.     else if ( (key = getch()) == ESCAPE )
  140.         ccode = -1;
  141.  
  142.     return (ccode);
  143. }
  144.