home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / n / netbioc.zip / TTY2.C < prev    next >
C/C++ Source or Header  |  1992-03-01  |  9KB  |  355 lines

  1. /***************************************************************************
  2.  *  TTy2.C                                   *
  3.  *    This file shows the usage of NetBIOS session commands.           *
  4.  *    The program must be run two machines who wish to communicate.       *
  5.  *    In this sample program, one side must receive packets while the       *
  6.  *    while the other side sends packets. They can of course change       *
  7.  *    their roles. The program, of course, is not very user friendly       *
  8.  *    and can be improved by implementing asynchronous send and receives.*
  9.  *    This will give user the ability to receive and send  at the       *
  10.  *    same  time.                               *
  11.  *                                       *
  12.  *    It is possible for receiver side to not name the remote person.    *
  13.  *    is illustrated by second usage line under LISTENER below       *
  14.  * Sample Usage:                               *
  15.  *    Machine TALKER            Machine LISTENER               *
  16.  *    --------------            ----------------               *
  17.  *     c:>TTy2 talker listener    c:>TTy2 listener talker           *
  18.  *                    c:>TTy2 talker
  19.  *                                       *
  20.  *  History:   Alok Sinha  October, 1991    Created               *
  21.  *                                       *
  22.  **************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <conio.h>
  28.  
  29. #include <common.h>
  30. #include <namesrv.h>
  31. #include <session.h>
  32.  
  33.  
  34. #define  LINE_SIZE 80
  35. #define  SEND       1
  36. #define  RECEIVE   2
  37. #define  LISTEN    3
  38. #define  CALL       4
  39. #define  ESC       0x1B
  40. #define  NEWLINE   0x0D
  41. #define  _EOF_       0x1A
  42. #define  SEND_TIME_OUT      ((unsigned char) 120)
  43. #define  RECEIVE_TIME_OUT ((unsigned char) 120)
  44.  
  45. void CopyToBuffer ( char *pchDest , char *pchSrc);
  46. BOOL CallListen   ( unsigned char ucLana, char *pchRemoteNameBuffer,
  47.             char *pchLocalNameBuffer, unsigned char far *ucLsn);
  48. void SendReceive  ( unsigned char ucLsn, unsigned char ucLana,
  49.             char *pchRemoteNameBuffer);
  50. int  GetLine      ( char *pchBuffer);
  51.  
  52. int main ( int argc, char **argv)
  53. {
  54.  
  55.     char chLocalNameBuffer  [ NCBNAMSZ ];
  56.     char chRemoteNameBuffer [ NCBNAMSZ ];
  57.     unsigned char   ucLana;
  58.     unsigned char   ucLsn;
  59.     unsigned char   ucNameNum;
  60.     unsigned char   ucRc;
  61.  
  62.  
  63.     if (argc < 2 )
  64.     {
  65.     printf ("Usage: TTy2 <local name> <remote name> [<lana>]\n");
  66.     exit (1);
  67.  
  68.     }
  69.     CopyToBuffer( chLocalNameBuffer, argv[1]);
  70.     if (argc == 2)
  71.     CopyToBuffer( chRemoteNameBuffer, "*");
  72.     else if (argc >= 3)
  73.     CopyToBuffer( chRemoteNameBuffer, argv[2]);
  74.  
  75.     printf("Local Name:  [%16.16s]\n", chLocalNameBuffer);
  76.  
  77.     printf("Remote Name: [%16.16s]\n", chRemoteNameBuffer);
  78.  
  79.     /* What is the LAN Adaptor number */
  80.     if (argc >= 4)
  81.     ucLana = (unsigned  char ) atoi ( argv [3] );
  82.     else
  83.     ucLana = 0;
  84.     printf ( "LANA: [%d] \n",     ucLana);
  85.  
  86.     /* Initialize in case of OS/2 */
  87.     NetInit( ucLana);
  88.  
  89.     /* Add a Name */
  90.     ucRc =     AddName ( chLocalNameBuffer,
  91.              ADD_UNIQUE_NAME,
  92.              ucLana,
  93.              &ucNameNum
  94.                );
  95.  
  96.     printf("AddName:: RC: [%x] NameNumber: [%x]\n", ucRc, ucNameNum);
  97.  
  98.     if (ucRc )
  99.     return NetCleanUp (1);
  100.  
  101.     /*
  102.      * Make a connection. Only if connection is successful, send and
  103.      * receive data.
  104.      */
  105.     if (CallListen( ucLana, chRemoteNameBuffer, chLocalNameBuffer, &ucLsn)
  106.     == TRUE)
  107.     {
  108.     SendReceive ( ucLsn, ucLana, chRemoteNameBuffer);
  109.     }
  110.  
  111.  
  112.     /* Delete the Name */
  113.     ucRc =     DelName ( chLocalNameBuffer,
  114.              ucLana,
  115.              ucNameNum
  116.                );
  117.  
  118.     printf("DelName:: RC: [%x] \n", ucRc);
  119.  
  120.     return NetCleanUp ( 0 );
  121. }
  122.  
  123. /*
  124.  * FUNCTION    :: CallListen()
  125.  * PARAMETERS  ::
  126.  *   [in]     ucLana        :- LAN adaptor numner over which to communicate.
  127.  *   [in/out] pchRemoteName :- char pointer to a NetBIOS name of remote
  128.  *                       listener/receiver
  129.  *   [in]     pchLocalName  :- char pointer to a NetBIOS name of local
  130.  *                       listener/receiver
  131.  *
  132.  * RETURN VALUE:
  133.  *        NO_ERROR  if connection was sucessfull.
  134.  *
  135.  *        pchRemoteName will contain the name of the caller, if
  136.  *        pchRemoteName was '*' and operation was NCB.RECEIVE.
  137.  *
  138.  * Comments:
  139.  *        Provides a NetBIOS connection between local
  140.  *        name and remote name.
  141.  *
  142.  */
  143. BOOL CallListen   ( unsigned char ucLana,
  144.             char far *pchRemoteNameBuffer,
  145.             char far *pchLocalNameBuffer,
  146.             unsigned char far *ucLsn)
  147.  
  148. {
  149.     int mode;
  150.     int ch;
  151.     unsigned    char    ucRc;
  152.  
  153.     mode = LISTEN;
  154.     printf("Default mode is Listen.\n");
  155.     printf("Press ESC to change  to Call mode, Else press enter.\n");
  156.     printf("Press <cntrl><z> to quit. \n");
  157.  
  158.     ch = getch ();
  159.     if (ch==_EOF_)
  160.     return FALSE;
  161.     if (ch==ESC)
  162.     mode = CALL;
  163.  
  164.  
  165.     if (mode==LISTEN)
  166.     {
  167.     printf("Listening for %16.16s\n", pchRemoteNameBuffer);
  168.     ucRc = Listen( ucLana,
  169.                pchRemoteNameBuffer,
  170.                pchLocalNameBuffer,
  171.                SEND_TIME_OUT,
  172.                RECEIVE_TIME_OUT,
  173.                ucLsn
  174.              );
  175.     if (ucRc)
  176.         printf("Error in Listen: RC: [%x]\n", ucRc);
  177.     }
  178.     else
  179.     {
  180.     printf("Calling  %16.16s\n", pchRemoteNameBuffer);
  181.     ucRc = Call(    ucLana,
  182.                pchRemoteNameBuffer,
  183.                pchLocalNameBuffer,
  184.                SEND_TIME_OUT,
  185.                RECEIVE_TIME_OUT,
  186.                ucLsn
  187.            );
  188.     if (ucRc)
  189.         printf("Error in Call: RC: [%x]\n", ucRc);
  190.  
  191.     }
  192.  
  193.     printf("\n\n");
  194.     /* Return success or failure */
  195.  
  196.     return ( (ucRc)?FALSE:TRUE);
  197.  
  198. }
  199.  
  200. /*
  201.  * FUNCTION    :: SendReceive()
  202.  * PARAMETERS  ::
  203.  *      [in] ucLsn        :- Local session number
  204.  *      [in] ucLana        :- LAN adaptor numner over which to communicate.
  205.  *      [in] pchRemoteName :- char pointer to a NetBIOS name of remote
  206.  *                   listener/receiver.
  207.  
  208.  *
  209.  * RETURN VALUE:
  210.  *        NONE.
  211.  * Comments:
  212.  *        Provides a very simple one-way TTy service between a local
  213.  *        name and remote name.
  214.  *
  215.  */
  216.  
  217. void SendReceive ( unsigned char ucLsn, unsigned char ucLana, char * pchRemoteName)
  218. {
  219.     int mode;
  220.     int ch;
  221.     char    chLine [ LINE_SIZE ];
  222.     unsigned    short    usLineSize, i;
  223.     unsigned    char    ucRc;
  224.  
  225.     mode = RECEIVE;
  226.     printf("Default mode is Receive.\n");
  227.     printf("Press ESC to change  mode, Else press enter.\n");
  228.     printf("Press <cntrl><z> to quit. \n");
  229.  
  230.     ch = getch ();
  231.     if (ch==_EOF_)
  232.     return    (void) HangSess ( ucLana, ucLsn);
  233.     if (ch==ESC)
  234.     mode = SEND;
  235.  
  236.     do
  237.     {
  238.     switch (mode)
  239.     {
  240.         case SEND:
  241.         printf("Send to [%16.16s]: ", pchRemoteName);
  242.         ch = GetLine (chLine);
  243.  
  244.         printf("\n");
  245.  
  246.         if (ch==ESC)
  247.             { mode = RECEIVE; break; }
  248.  
  249.         if (ch==_EOF_)
  250.         {
  251.             chLine [ 0 ]= -1;
  252.             chLine [ 1 ]= '\0';
  253.         }
  254.  
  255.         ucRc = SendSess (      ucLana,
  256.                        ucLsn,
  257.                        chLine,
  258.                        strlen(chLine)
  259.                 );
  260.         if (ch==_EOF_)
  261.             return (void) HangSess ( ucLana, ucLsn);
  262.  
  263.         if (ucRc)
  264.         {
  265.             printf("Error Sending Packet: RC: [%x]\n", ucRc);
  266.             return (void) HangSess ( ucLana, ucLsn);
  267.         }
  268.  
  269.         break;
  270.  
  271.         case RECEIVE:
  272.         printf("Receiveing from [%16.16s]: ", pchRemoteName);
  273.  
  274.         usLineSize = LINE_SIZE;
  275.         ucRc = ReceiveSess ( ucLana,
  276.                      ucLsn,
  277.                      chLine,
  278.                      &usLineSize
  279.                    );
  280.         if (ucRc)
  281.         {
  282.             printf("Error Receiving Packet: RC: [%x]\n", ucRc);
  283.             return (void) HangSess ( ucLana, ucLsn);
  284.         }
  285.         else
  286.         {
  287.             if (chLine [ 0 ] == -1 )
  288.             {
  289.             printf("Remote user Hang Up\n");
  290.             return (void) HangSess ( ucLana, ucLsn);
  291.             }
  292.  
  293.             for (i= 0; i< usLineSize; i++)
  294.             printf("%c", chLine [i]);
  295.             printf("\n");
  296.         }
  297.  
  298.         /*
  299.          * check if user wants to get out or change mode. Yes, this
  300.          * is definitely boring as receiver has to keep hiting
  301.          * the key board. This problem is solved by implementing
  302.          * async. receive.
  303.          */
  304.         ch = getch ();
  305.         if (ch==_EOF_)
  306.             return (void) HangSess ( ucLana, ucLsn);
  307.         if (ch==ESC)
  308.            mode = SEND;
  309.         break;
  310.     }
  311.  
  312.     } while (ch != _EOF_);
  313.  
  314.     return (void) HangSess ( ucLana, ucLsn);
  315.  
  316. }
  317.  
  318. /*
  319.  * FUNCTION    :: GetLine()
  320.  * PARAMETERS  ::
  321.  *     [in/out] pchBuffer:- char pointer to a buffer which will receive
  322.  *                  user input.
  323.  *
  324.  * RETURN VALUE:
  325.  *        The last character typed by the user.
  326.  * Comment:
  327.  *        Allows user to type in a LINE_SIZE big line or simply
  328.  *        a ESC or <control><z>
  329.  */
  330.  
  331.  
  332. int GetLine ( char *pchBuffer)
  333. {
  334.     int ch, index;
  335.  
  336.     index = 0 ;
  337.     do
  338.     {
  339.     ch = getch();
  340.     if ((ch == ESC) || (ch == _EOF_))
  341.         return ch;
  342.  
  343.     putch( ch );
  344.     pchBuffer [index++] = ch;
  345.  
  346.     if (ch == NEWLINE)
  347.     {
  348.         pchBuffer [index] = '\0';
  349.         return ch;
  350.     }
  351.  
  352.     } while ( index < LINE_SIZE);
  353.  
  354. }
  355.