home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / spxdos.exe / CHAT.C next >
Text File  |  1995-01-27  |  6KB  |  232 lines

  1. /****************************************************************************
  2. **    File:    CHAT.C
  3. **
  4. **    Desc:    Sample SPX chat program.    
  5. **
  6. **            This Sample code demonstrates how to set up and use SPX
  7. **            communications to communicate between 2 nodes.
  8. **        
  9. **        
  10. **        
  11. **
  12. **        DISCLAIMER  
  13. **  
  14. **    Novell, Inc. makes no representations or warranties with respect to
  15. **    any NetWare software, and specifically disclaims any express or
  16. **    implied warranties of merchantability, title, or fitness for a
  17. **    particular purpose.  
  18. **
  19. **    Distribution of any NetWare software is forbidden without the
  20. **    express written consent of Novell, Inc.  Further, Novell reserves
  21. **    the right to discontinue distribution of any NetWare software.
  22. **    
  23. **    Novell is not responsible for lost profits or revenue, loss of use
  24. **    of the software, loss of data, costs of re-creating lost data, the
  25. **    cost of any substitute equipment or program, or claims by any party
  26. **    other than you.  Novell strongly recommends a backup be made before
  27. **    any software is installed.   Technical support for this software
  28. **    may be provided at the discretion of Novell.
  29. **
  30. **    Programmers:
  31. **
  32. **        Ini    Who                        Firm
  33. **        -----------------------------------------------------------------------
  34. **        TDOC  Technical Documentation Team
  35. **        KLB    Karl Bunnell                Novell Developer Support.
  36. **
  37. **    History:
  38. **
  39. **        When        Who    What
  40. **        -----------------------------------------------------------------------
  41. **        10-25-88    TDOC    First code.
  42. **        01-27-95 KLB   Ported this example to the NetWare Client SDK
  43. */
  44.  
  45. /****************************************************************************
  46. **    Include headers, macros, function prototypes, etc.
  47. */
  48.     /*------------------------------------------------------------------------
  49.     **    MACROS
  50.     */
  51.     #define NWDOS
  52.  
  53.  
  54.     /*------------------------------------------------------------------------
  55.     **    ANSI
  56.     */
  57.     #include <stdio.h>
  58.     #include <string.h>
  59.  
  60.  
  61.     /*------------------------------------------------------------------------
  62.     **    NetWare
  63.     */
  64.     #include <nwcalls.h>
  65.     #include <nwipxspx.h>
  66.     #include "chat.h"
  67.  
  68.  
  69.     /*------------------------------------------------------------------------
  70.     **    Globals
  71.     */
  72.     extern    void    SendMessage();
  73.     extern    void    TearDownConnection();
  74.     extern    int     PollForPacket();
  75.     extern    void    SetUpSendECBs();
  76.     extern    void    SetUpReceiveECBs();
  77.     extern     ECB       *SetUpInitialECB();
  78.     extern     void    DrawScreen();
  79.     extern    void    Dispatcher();
  80.     extern     void    ClearScreen();
  81.  
  82.     extern WORD        SPXConnectionNumber, Socket, connectionID,
  83.                 listeningConnection, preferredConnectionID;
  84.     extern int        ACTIVE_CONNECTION;
  85.  
  86.  
  87.     char    hangUp[] = "I hung up.  Call me back when you get a chance.";
  88.     NWCONN_HANDLE    connHandle;
  89.  
  90.  
  91. void main(argc,argv)
  92. int        argc;
  93. char    *argv[];
  94. {
  95.     int           ccode;
  96.     NWCCODE        cCode;
  97.     char    key, userName[48], callerName[48], currentServer[48],
  98.             preferredServer[48], *command;
  99.     NWFLAGS    result;
  100.             
  101.  /* Test for the correct number of arguments on command line */ 
  102.     if (argc != 2) 
  103.     {
  104.         printf ("CHAT.EXE v1.01\n");
  105.         printf ("by Novell Technical Documentation\n\n");
  106.         printf ("Format: Chat fileserver/userName\n");
  107.         printf ("Example: Chat Server_1/Rasputin\n");
  108.         exit (); 
  109.     }
  110.  
  111.  
  112.     NWCallsInit(NULL, NULL);
  113.  
  114.     command = argv[1];
  115.     ParseDestination (preferredServer, userName, command);
  116.  
  117. //    CheckServer (currentServer, preferredServer);
  118.  
  119.     cCode = NWGetConnectionHandle(
  120.                 /* server name    */ preferredServer,
  121.                 /* reserved       */ 0,
  122.                 /* conn handle    */ &connHandle,
  123.                 /* reserved       */ 0
  124.                );
  125.  
  126.     GetConnectionAndNode (preferredServer, userName, &listeningConnection);
  127.  
  128.     ccode = SayHello (&listeningConnection);
  129.     if (ccode)
  130.         Error ("Sorry, there's no answer.");
  131.  
  132.  /* Format the screen */
  133.     DrawScreen ();
  134.  
  135.  /* Establish a connection */
  136.     ccode =    EstablishCallingSide ();
  137.     if (ccode)
  138.     {
  139.         NWSendBroadcastMessage (connHandle, hangUp, 1, &listeningConnection, &result);
  140.         Error ("So much for that.");
  141.     }
  142.  
  143.  /* While connection is active, poll for and send messages */
  144.      Update ("Connection is established.");
  145.     while (ACTIVE_CONNECTION)
  146.     {
  147.          while (!kbhit())
  148.         {
  149.             if ( (ACTIVE_CONNECTION = PollForPacket()) == 0)
  150.             {
  151.                 Update ("Your partner hung up.  Press ESCAPE twice to close.");
  152.                 break;
  153.             }
  154.         }
  155.  
  156.      /* A key was hit */
  157.          if ( (key = getch()) == ESCAPE)
  158.         {
  159.             Update ("Press ESCAPE to hang up / ENTER to resume.");
  160.             if ( (key = getch()) == ESCAPE)
  161.             {
  162.                 Update ("Closing connection...");
  163.                 ACTIVE_CONNECTION = 0;
  164.                 break;
  165.             }
  166.             Update ("Connection is active.");
  167.         }
  168.         else
  169.             Dispatcher (key);        
  170.      }
  171.  
  172.     TearDownConnection ();
  173.  
  174. //    if (preferredConnectionID);
  175. //        SetPreferredConnectionID (connectionID);
  176.  
  177. }
  178.  
  179.  
  180. EstablishCallingSide ()
  181. {
  182.     int        ccode, returnCode = 0, keepTrying = 1;
  183.     char    key;
  184.     BYTE    retryCount = 0x00, watchDog = 0x00, majRevNumber, minRevNumber;
  185.     WORD    tempIDNumber, maxConnections, maxConnAvailable;
  186.     ECB        *initialECB;
  187.  
  188.  /* See if SPX is installed */
  189.     ccode =    SPXInitialize (&majRevNumber,&minRevNumber,&maxConnections,
  190.                 &maxConnAvailable);
  191.     if (ccode != SPX_INSTALLED)
  192.         Error ("SPX is not installed on this machine.");
  193.  
  194.  /* Open an IPX socket */
  195.     ccode = IPXOpenSocket ( (BYTE *)&Socket, (BYTE)0 );
  196.     if (ccode)
  197.         Error ("Unable to open a socket.");
  198.  
  199.  /* Set up an initial ECB for listening and some ECBs for receiving */
  200.      SetUpReceiveECBs ();
  201.     initialECB = SetUpInitialECB ();
  202.  
  203.  /* Try to establish a connection */
  204.     while (keepTrying)
  205.     {
  206.         Update ("Waiting for someone to answer...");
  207.         ccode = SPXEstablishConnection (  (BYTE)retryCount, (BYTE)watchDog,
  208.             &SPXConnectionNumber, initialECB );
  209.         if (ccode)
  210.             Error ("Connection error.  Unable to continue.");
  211.  
  212.         while (initialECB->inUseFlag)
  213.         {
  214.             IPXRelinquishControl ();
  215.         }
  216.  
  217.          keepTrying = initialECB->completionCode;
  218.         if (keepTrying)
  219.         {
  220.             Update ("No answer.  Press ESCAPE to hang up / ENTER to keep trying.");
  221.             if ( (key = getch()) == ESCAPE)
  222.             {
  223.                 keepTrying = 0;
  224.                 returnCode = -1;
  225.             }
  226.         }
  227.         else
  228.             SetUpSendECB ();
  229.     }
  230.     return (returnCode);
  231. }
  232.