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

  1. /******************************************************************************
  2.  *
  3.  *    Program Name:    TALK_TO.ME 
  4.  *
  5.  *    Filename:    CHAT.C -- 10/25/88  4:00
  6.  *
  7.  *    Purpose:  Establishes an SPX connection and sends a message
  8.  *
  9.  ******************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <nit.h>
  14. #include "chat.h"
  15.  
  16.  
  17. extern    void    SendMessage();
  18. extern    void    TearDownConnection();
  19. extern    int     PollForPacket();
  20. extern    void    SetUpSendECBs();
  21. extern    void    SetUpReceiveECBs();
  22. extern     ECB       *SetUpInitialECB();
  23. extern     void    DrawScreen();
  24. extern    void    Dispatcher();
  25. extern     void    ClearScreen();
  26.  
  27. extern WORD        SPXConnectionNumber, Socket, connectionID,
  28.                 listeningConnection, preferredConnectionID;
  29. extern int        ACTIVE_CONNECTION;
  30.  
  31. char    hangUp[] = "I hung up.  Call me back when you get a chance.";
  32.  
  33.  
  34. void main(argc,argv)
  35. int        argc;
  36. char    *argv[];
  37. {
  38.     int           ccode;
  39.     char    key, userName[48], callerName[48], currentServer[48],
  40.             preferredServer[48], *command;
  41.     BYTE    result[2];
  42.             
  43.  /* Test for the correct number of arguments on command line */ 
  44.     if (argc != 2) 
  45.     {
  46.         printf ("CHAT.EXE v1.01\n");
  47.         printf ("by Novell Technical Documentation\n\n");
  48.         printf ("Format: Chat fileserver/userName\n");
  49.         printf ("Example: Chat Server_1/Rasputin\n");
  50.         exit (); 
  51.     }
  52.  
  53.  
  54.     command = argv[1];
  55.     ParseDestination (preferredServer, userName, command);
  56.  
  57.     CheckServer (currentServer, preferredServer);
  58.  
  59.     GetConnectionAndNode (preferredServer, userName, &listeningConnection);
  60.  
  61.     ccode = SayHello (&listeningConnection);
  62.     if (ccode)
  63.         Error ("Sorry, there's no answer.");
  64.  
  65.  /* Format the screen */
  66.     DrawScreen ();
  67.  
  68.  /* Establish a connection */
  69.     ccode =    EstablishCallingSide ();
  70.     if (ccode)
  71.     {
  72.         SendBroadcastMessage (hangUp, &listeningConnection, result, 1);
  73.         Error ("So much for that.");
  74.     }
  75.  
  76.  /* While connection is active, poll for and send messages */
  77.      Update ("Connection is established.");
  78.     while (ACTIVE_CONNECTION)
  79.     {
  80.          while (!kbhit())
  81.         {
  82.             if ( (ACTIVE_CONNECTION = PollForPacket()) == 0)
  83.             {
  84.                 Update ("Your partner hung up.  Press ESCAPE twice to close.");
  85.                 break;
  86.             }
  87.         }
  88.  
  89.      /* A key was hit */
  90.          if ( (key = getch()) == ESCAPE)
  91.         {
  92.             Update ("Press ESCAPE to hang up / ENTER to resume.");
  93.             if ( (key = getch()) == ESCAPE)
  94.             {
  95.                 Update ("Closing connection...");
  96.                 ACTIVE_CONNECTION = 0;
  97.                 break;
  98.             }
  99.             Update ("Connection is active.");
  100.         }
  101.         else
  102.             Dispatcher (key);        
  103.      }
  104.  
  105.     TearDownConnection ();
  106.  
  107.     if (preferredConnectionID);
  108.         SetPreferredConnectionID (connectionID);
  109. }
  110.  
  111.  
  112. EstablishCallingSide ()
  113. {
  114.     int        ccode, returnCode = 0, keepTrying = 1;
  115.     char    key;
  116.     BYTE    retryCount = 0x00, watchDog = 0x00, majRevNumber, minRevNumber;
  117.     WORD    tempIDNumber, maxConnections, maxConnAvailable;
  118.     ECB        *initialECB;
  119.  
  120.  /* See if SPX is installed */
  121.     ccode =    SPXInitialize (&majRevNumber,&minRevNumber,&maxConnections,
  122.                 &maxConnAvailable);
  123.     if (ccode != SPX_INSTALLED)
  124.         Error ("SPX is not installed on this machine.");
  125.  
  126.  /* Open an IPX socket */
  127.     ccode = IPXOpenSocket ( (BYTE *)&Socket, (BYTE)0 );
  128.     if (ccode)
  129.         Error ("Unable to open a socket.");
  130.  
  131.  /* Set up an initial ECB for listening and some ECBs for receiving */
  132.      SetUpReceiveECBs ();
  133.     initialECB = SetUpInitialECB ();
  134.  
  135.  /* Try to establish a connection */
  136.     while (keepTrying)
  137.     {
  138.         Update ("Waiting for someone to answer...");
  139.         ccode = SPXEstablishConnection (  (BYTE)retryCount, (BYTE)watchDog,
  140.             &SPXConnectionNumber, initialECB );
  141.         if (ccode)
  142.             Error ("Connection error.  Unable to continue.");
  143.  
  144.         while (initialECB->inUseFlag)
  145.         {
  146.             IPXRelinquishControl ();
  147.         }
  148.  
  149.          keepTrying = initialECB->completionCode;
  150.         if (keepTrying)
  151.         {
  152.             Update ("No answer.  Press ESCAPE to hang up / ENTER to keep trying.");
  153.             if ( (key = getch()) == ESCAPE)
  154.             {
  155.                 keepTrying = 0;
  156.                 returnCode = -1;
  157.             }
  158.         }
  159.         else
  160.             SetUpSendECB ();
  161.     }
  162.     return (returnCode);
  163. }
  164.