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

  1. /****************************************************************************
  2. **    File:    HELLO.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.  
  59.  
  60.     /*------------------------------------------------------------------------
  61.     **    NetWare
  62.     */
  63.     #include <nwcalls.h>
  64.     #include "chat.h"
  65.     #include <nwipxspx.h>
  66.  
  67.     /*------------------------------------------------------------------------
  68.     **    GLOBALS
  69.     */
  70.     extern    void    SendMessage();
  71.     extern    void    TearDownConnection();
  72.     extern    int    PollForPacket();
  73.     extern    void    SetUpReceiveECBs();
  74.     extern    ECB    *SetUpInitialECB();
  75.     extern    void    DrawScreen();
  76.     extern    void    Dispatcher();
  77.     extern     void    ClearScreen();
  78.     extern    void     SetUpSendECB();
  79.  
  80.     extern NETWORK_NODE     destNode;
  81.     extern WORD       SPXConnectionNumber,         /* SPXEstablishConnection returns in DX */
  82.                       Socket;
  83.     extern int        ACTIVE_CONNECTION;
  84.  
  85.     NWCONN_HANDLE    connHandle;
  86.  
  87.  
  88. void main(argc,argv)
  89. int        argc;
  90. char    *argv[];
  91. {
  92.     int        ccode = 0;
  93.     char    key = 0,
  94.             networkNumber[4];
  95.     WORD    connection, mysocket;
  96.     NETWORK_ADDR    iNetAddress;
  97.  
  98.  /* Test for the correct number of arguments on command line */ 
  99.     if (argc != 2) 
  100.     {
  101.         printf ("HELLO.EXE v1.01\n");
  102.         printf ("by Novell Technical Documentation\n\n");
  103.         printf ("Format: Hello station_number(hexidecimal)\n");
  104.         printf ("Example: Hello 3A\n");
  105.         exit (); 
  106.     }
  107.  
  108.     NWCallsInit(NULL, NULL);
  109.  
  110.  /* Get the target station number */
  111.      sscanf (argv[1], "%d", &connection);
  112.     NWGetDefaultConnectionID(&connHandle);
  113.  
  114.     NWGetInternetAddress (connHandle, connection, (BYTE *)&iNetAddress);
  115.     
  116.     memcpy(&destNode, iNetAddress.node, 6);
  117.     memcpy(&networkNumber, iNetAddress.network, 4);
  118.  
  119.  /* Format the screen */
  120.      DrawScreen ();
  121.  
  122.  /* Establish a connection */
  123.     ccode = EstablishListeningSide ();
  124.     if (ccode)
  125.             Error ("No answer.  Maybe they really didn't want to chat with you.");
  126.  
  127.  /* While connection is active, send or poll for messages */
  128.      Update ("Connection is established.");
  129.  
  130.     while (ACTIVE_CONNECTION)
  131.     {
  132.          while ( !kbhit () )
  133.         {
  134.             if ( (ACTIVE_CONNECTION = PollForPacket()) == 0)
  135.             {
  136.                  Update ("Your partner hung up.  Press ESCAPE twice to close.");
  137.                 break;
  138.             }
  139.         }
  140.  
  141.      /* A key was hit */
  142.          if ( (key = getch()) == ESCAPE)
  143.         {
  144.             Update ("Press ESCAPE to hang up / ENTER to resume.");
  145.             if ( (key = getch()) == ESCAPE)
  146.             {
  147.                 Update ("Closing connection...");
  148.                 ACTIVE_CONNECTION = 0;
  149.                 break;
  150.             }
  151.             Update ("Connection is active.");
  152.         }
  153.         else
  154.             Dispatcher (key);        
  155.      }
  156.  
  157.     TearDownConnection ();
  158. }
  159.  
  160.  
  161.  
  162. int    EstablishListeningSide ()
  163. {
  164.     int        ccode;
  165.     BYTE    retryCount = 0x00, watchDog = 0x00, majRevNumber, minRevNumber;
  166.     WORD    tempIDNumber, maxConnections, maxConnAvailable;
  167.     ECB        *initialECB;
  168.     char    key;
  169.  
  170.  /* See if SPX is installed */
  171.        Update ("Initializing SPX.",0);
  172.     ccode =    SPXInitialize (&majRevNumber,&minRevNumber,&maxConnections,
  173.                 &maxConnAvailable);
  174.     if (ccode != SPX_INSTALLED)
  175.         Error ("SPX is not installed.");
  176.  
  177.  /* Open an IPX socket */
  178.     ccode = IPXOpenSocket ( (BYTE *)&Socket, (BYTE)0 );
  179.     if (ccode)
  180.         Error ("Unable to open a socket.");
  181.  
  182.  /* Set up an ECB for listening and some ECBs for receiving */
  183.     Update ("Preparing packets.",0);
  184.      SetUpReceiveECBs ();
  185.      initialECB = SetUpInitialECB ();
  186.  
  187.  /* Have SPX begin listening for a connection */
  188.      Update ("Attempting a connection.",0);
  189.     SPXListenForConnection ( (BYTE)retryCount, (BYTE)watchDog, initialECB );
  190.  
  191.  /* If connection is successful, set up some ECBs for sending */
  192.     Update ("Attempting a connection.  Press ESCAPE to hang up.");
  193.      while ( !kbhit() && initialECB->inUseFlag)
  194.     {
  195.         /* WAIT HERE */;
  196.     }
  197.  
  198.     if (initialECB->completionCode == 0x00)
  199.     {
  200.         memcpy (&SPXConnectionNumber, initialECB->IPXWorkspace, 2);
  201.         SetUpSendECB ();
  202.         ccode = initialECB->completionCode;
  203.     }
  204.     else if ( (key = getch()) == ESCAPE )
  205.         ccode = -1;
  206.  
  207.     return (ccode);
  208. }
  209.