home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / NETCLB35.ZIP / NETCLB35.EXE / EXAMPLES / IPXCHAT.C < prev    next >
C/C++ Source or Header  |  1996-01-03  |  5KB  |  184 lines

  1. /******************************* IPXCHAT ********************************/
  2. /* This example program will chat between two stations sending a single */
  3. /* character at a time in either direction.                             */
  4. /************************************************************************/
  5.  
  6. #define ESCAPE_KEY  0x1b
  7. #define RETURN_KEY  0x0d
  8.  
  9. #define SHORT_LIVED 0x00
  10. #define SOCKET      0x4848        /* Socket number we will use */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <dos.h>
  15. #include <conio.h>
  16.  
  17. #ifndef TURBOC
  18. #include <memory.h>
  19. #endif
  20.  
  21. #include "netware.h"
  22.  
  23. word program_socket ;
  24. word server_socket ;
  25.  
  26. EVENT_CONTROL_BLOCK receiveECB ;
  27. EVENT_CONTROL_BLOCK sendECB ;
  28. IPX_HEADER          IPX_receive_header ;
  29. IPX_HEADER          IPX_send_header ;
  30.  
  31. char packet_data;
  32.  
  33. word connection_number;
  34.  
  35. /********************** function prototypes ********************/
  36.  
  37. word SetupForReceive( void );
  38. void SetupForSend( int connection_number );
  39. void TransmitReceive( void );
  40.  
  41. /***************************** main() *************************/
  42.  
  43. void main( int argc, char *argv[] )
  44. {
  45. int rc;
  46.  
  47.     if (argc != 2)
  48.     {
  49.        printf("Invalid number of paramaters\n");
  50.        printf("Usage is:\n");
  51.        printf("         ipxchat <connection_number>\n");
  52.        exit(255);
  53.     }
  54.     else
  55.        connection_number = atoi(argv[1]);
  56.        
  57.     if (IPXInitialise() != 0)
  58.     {
  59.        printf("IPX is not installed........\n");
  60.        exit(255);
  61.     }
  62.     else
  63.        printf("IPX Initialised.......\n");
  64.     program_socket = SOCKET;
  65.     if (IPXOpenSocket( &program_socket , SHORT_LIVED ) != 0)
  66.     {
  67.        printf("Failed to open socket %04.4X\n",SOCKET);
  68.        exit(255);
  69.     }
  70.     else
  71.        printf("Socket %04.4X opened\n",SOCKET);
  72.     if ( (rc = SetupForReceive()) != 0)
  73.        printf("Failed to create listen ECB: %d\n",rc);
  74.     else
  75.     {
  76.        printf("Receive ECB created\n");
  77.        SetupForSend(connection_number) ;
  78.        TransmitReceive() ;
  79.     }
  80.     IPXCloseSocket(program_socket) ;
  81. }
  82.  
  83. /********************** SetupForReceive ***********************/
  84. word SetupForReceive( void )
  85. {
  86.    memset( (void *)&receiveECB,0,sizeof(EVENT_CONTROL_BLOCK));
  87.  
  88.    ConvertIntToNWInt( program_socket, &(receiveECB.socket_number) );
  89.  
  90.    receiveECB.fragment_count      = 2 ;
  91. /* receiveECB.esr                 = (void (FAR *) () ) 0;  */
  92.    receiveECB.esr                 = NULL;
  93.  
  94.    receiveECB.fragment[0].address = (void FAR *)&IPX_receive_header;
  95.    receiveECB.fragment[0].length  = sizeof(IPX_receive_header);
  96.  
  97.    receiveECB.fragment[1].address = (void FAR *)&packet_data;
  98.    receiveECB.fragment[1].length  = 1;
  99.  
  100.    return( IPXListenForPacket( &receiveECB ) );
  101. }
  102.  
  103. /*********************** SetupForSend *************************/
  104. void SetupForSend( int connection_number )
  105. {
  106. word transport_time;
  107.  
  108.     GetInternetAddress( connection_number,
  109.                         IPX_send_header.dest_addr.ina.network_number,
  110.                         IPX_send_header.dest_addr.ina.node_address,
  111.                         &server_socket);
  112.     IPX_send_header.packet_type = 4 ;
  113.     ConvertIntToNWInt(program_socket,&IPX_send_header.dest_addr.socket_number);
  114.  
  115.     memset( (void *)&sendECB,0,sizeof(EVENT_CONTROL_BLOCK));
  116.  
  117.     IPXGetLocalTarget(&IPX_send_header.dest_addr.ina,
  118.                       sendECB.immediate_address,&transport_time);
  119.     ConvertIntToNWInt( program_socket, &(sendECB.socket_number) );
  120.  
  121.     sendECB.fragment_count = 2;
  122. /*  sendECB.esr            = (void (FAR *) () ) 0;  */
  123.     sendECB.esr            = NULL;
  124.  
  125.     sendECB.fragment[0].address = (void FAR *)&IPX_send_header;
  126.     sendECB.fragment[0].length  = sizeof(IPX_send_header);
  127.  
  128.     sendECB.fragment[1].address = (void FAR *)&packet_data;
  129.     sendECB.fragment[1].length  = 1;
  130. }
  131.  
  132. /******************** TransmitReceive ************************/
  133. void TransmitReceive( void )
  134. {
  135.     int done;
  136.  
  137.     system("cls") ;
  138.     printf("Enter character to give to other side.\n");
  139.     printf("(Type ESCAPE to exit.)\n");
  140.  
  141.     done = 0;
  142.  
  143.     while(!done)
  144.     {
  145.         IPXRelinquishControl();
  146.         
  147.         if (!receiveECB.in_use)
  148.         {
  149.             switch(packet_data)
  150.             {
  151.                 case RETURN_KEY :
  152.                                  printf("\n");
  153.                                  break;
  154.                 case ESCAPE_KEY :
  155.                                  done = 1;
  156.                                  break;
  157.                 default :
  158.                                  printf("%c", packet_data);
  159.                                  break;
  160.             }
  161.             IPXListenForPacket( &receiveECB );
  162.         }
  163.         if (kbhit())
  164.         {
  165.             while(sendECB.in_use);
  166.             packet_data = (char) getch();
  167.             switch(packet_data)
  168.             {
  169.                 case RETURN_KEY : printf("\n");
  170.                                   break;
  171.                 case ESCAPE_KEY :
  172.                                   done = 1;
  173.                                   break;
  174.                 default :
  175.                                   printf("%c", packet_data);
  176.                                   break;
  177.             }
  178.             IPXSendPacket( &sendECB );
  179.         }
  180.     }
  181.  
  182.     IPXCancelEvent( &receiveECB );
  183. }
  184.