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

  1. /***************************************************************************
  2.  *  TTy.C                                   *
  3.  *    This file shows the usage of NetBIOS datagram commands.        *
  4.  *    The program must be run two machines who wish to communicate.       *
  5.  *    In this sample program, one side must receive datgrams while the   *
  6.  *    while the other side sends datagrams. 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 datagram at the*
  10.  *    same  time.                               *
  11.  *                                       *
  12.  *                                       *
  13.  * Sample Usage:                               *
  14.  *    Machine TALKER            Machine LISTENER               *
  15.  *    --------------            ----------------               *
  16.  *     c:>TTy talker listener        c:>TTy listener talker           *
  17.  *                                       *
  18.  *  History:   Alok Sinha  October, 1991    Created               *
  19.  *                                       *
  20.  **************************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <conio.h>
  26.  
  27. #include <common.h>
  28. #include <namesrv.h>
  29. #include <datasrv.h>
  30.  
  31.  
  32. #define  LINE_SIZE 80
  33. #define  SEND       1
  34. #define  RECEIVE   2
  35. #define  ESC       0x1B
  36. #define  NEWLINE   0x0D
  37. #define  _EOF_       0x1A
  38.  
  39. void CopyToBuffer ( char *pchDest , char *pchSrc);
  40. void SendReceive  ( char ucLana, char usNameNum, char * pchRemoteName);
  41. int  GetLine      ( char *pchBuffer);
  42.  
  43. int main ( int argc, char **argv)
  44. {
  45.  
  46.     char chLocalNameBuffer  [ NCBNAMSZ ];
  47.     char chRemoteNameBuffer [ NCBNAMSZ ];
  48.     unsigned char   ucLana;
  49.     unsigned char   ucNameNum;
  50.     unsigned char   ucRc;
  51.  
  52.  
  53.     if (argc < 3 )
  54.     {
  55.     printf ("Usage: TTy <local name> <remote name> [<lana>]\n");
  56.     exit (1);
  57.  
  58.     }
  59.     CopyToBuffer( chLocalNameBuffer, argv[1]);
  60.     CopyToBuffer( chRemoteNameBuffer, argv[2]);
  61.  
  62.     printf("Local Name:  [%16.16s]\n", chLocalNameBuffer);
  63.  
  64.     printf("Remote Name: [%16.16s]\n", chRemoteNameBuffer);
  65.  
  66.  
  67.     /* What is the LAN Adaptor number */
  68.     if (argc >= 4)
  69.     ucLana = (unsigned  char ) atoi ( argv [3] );
  70.     else
  71.     ucLana = 0;
  72.     printf ( "LANA: [%d] \n",     ucLana);
  73.  
  74.     /* Initialize in case of OS/2 */
  75.     NetInit( ucLana);
  76.  
  77.  
  78.     /* Add a Name */
  79.     ucRc =     AddName ( chLocalNameBuffer,
  80.              ADD_UNIQUE_NAME,
  81.              ucLana,
  82.              &ucNameNum
  83.                );
  84.  
  85.     printf("AddName:: RC: [%x] NameNumber: [%x]\n", ucRc, ucNameNum);
  86.  
  87.     if (ucRc )
  88.     return NetCleanUp (1);
  89.  
  90.     SendReceive( ucLana, ucNameNum, chRemoteNameBuffer);
  91.  
  92.     /* Delete the Name */
  93.     ucRc =     DelName ( chLocalNameBuffer,
  94.              ucLana,
  95.              ucNameNum
  96.                );
  97.  
  98.     printf("DelName:: RC: [%x] \n", ucRc);
  99.  
  100.     return NetCleanUp (0);
  101. }
  102.  
  103.  
  104. /*
  105.  * FUNCTION    :: SendReceive()
  106.  * PARAMETERS  ::
  107.  *     [in] ucLana        :- LAN adaptor numner over which to communicate.
  108.  *     [in] ucNameNum        :- Name number associated with local name.
  109.  *     [in] pchRemoteName :- char pointer to a NetBIOS name of remote
  110.  *                   listener/receiver
  111.  *
  112.  * RETURN VALUE:
  113.  *        NONE.
  114.  * Comments:
  115.  *        Provides a very simple one-way TTy service between a local
  116.  *        name and remote name.
  117.  *
  118.  */
  119.  
  120. void SendReceive ( char ucLana, char ucNameNum, char * pchRemoteName)
  121. {
  122.     int mode;
  123.     int ch;
  124.     char    chLine [ LINE_SIZE ];
  125.     unsigned    short    usLineSize, i;
  126.     unsigned    char    ucRc;
  127.  
  128.     mode = RECEIVE;
  129.     printf("Default mode is Receive.\n");
  130.     printf("Press ESC to change  mode, Else press enter.\n");
  131.     printf("Press <cntrl><z> to quit. \n");
  132.  
  133.     ch = getch ();
  134.     if (ch==_EOF_)
  135.     return ;
  136.     if (ch==ESC)
  137.     mode = SEND;
  138.  
  139.     do
  140.     {
  141.     switch (mode)
  142.     {
  143.         case SEND:
  144.         printf("Send: ");
  145.         ch = GetLine (chLine);
  146.  
  147.         printf("\n");
  148.  
  149.         if (ch==ESC)
  150.             { mode = RECEIVE; break; }
  151.  
  152.         if (ch==_EOF_)
  153.         {
  154.             chLine [ 0 ]= -1;
  155.             chLine [ 1 ]= '\0';
  156.         }
  157.  
  158.         ucRc = SendDatagram ( pchRemoteName,
  159.                        ucLana,
  160.                        ucNameNum,
  161.                        chLine,
  162.                        strlen(chLine)
  163.                     );
  164.         if (ch==_EOF_)
  165.             return;
  166.  
  167.         if (ucRc)
  168.             printf("Error Sending Datagram: RC: [%x]\n", ucRc);
  169.  
  170.         break;
  171.  
  172.         case RECEIVE:
  173.         printf("Receiving: ");
  174.  
  175.         usLineSize = LINE_SIZE;
  176.         ucRc = ReceiveDatagram ( ucLana,
  177.                      ucNameNum,
  178.                      chLine,
  179.                      &usLineSize
  180.                        );
  181.         if (ucRc)
  182.             printf("Error Receiving Datagram: RC: [%x]\n", ucRc);
  183.         else
  184.         {
  185.             if (chLine [ 0 ]==-1)
  186.             {
  187.                printf("Remote user Hang Up\n");
  188.                return;
  189.             }
  190.  
  191.             for (i= 0; i< usLineSize; i++)
  192.                 printf("%c", chLine [i]);
  193.             printf("\n");
  194.         }
  195.  
  196.         /*
  197.          * check if user wants to get out or change mode. Yes, this
  198.          * is definitely boring as receiver has to keep hiting
  199.          * the key board. This problem is solved by implementing
  200.          * async. receive.
  201.          */
  202.         ch = getch ();
  203.         if (ch==_EOF_)
  204.             return ;
  205.         if (ch==ESC)
  206.            mode = SEND;
  207.         break;
  208.     }
  209.  
  210.     } while (ch != _EOF_);
  211.  
  212.  
  213. }
  214. /*
  215.  * FUNCTION    :: GetLine()
  216.  * PARAMETERS  ::
  217.  *     [in/out] pchBuffer:- char pointer to a buffer which will receive
  218.  *                  user input.
  219.  *
  220.  * RETURN VALUE:
  221.  *        The last character typed by the user.
  222.  * Comment:
  223.  *        Allows user to type in a LINE_SIZE big line or simply
  224.  *        a ESC or <control><z>
  225.  */
  226.  
  227.  
  228. int GetLine ( char *pchBuffer)
  229. {
  230.     int ch, index;
  231.  
  232.     index = 0 ;
  233.     do
  234.     {
  235.     ch = getch();
  236.     if ((ch == ESC) || (ch == _EOF_))
  237.         return ch;
  238.  
  239.     putch( ch );
  240.     pchBuffer [index++] = ch;
  241.  
  242.     if (ch == NEWLINE)
  243.     {
  244.         pchBuffer [index] = '\0';
  245.         return ch;
  246.     }
  247.  
  248.     } while ( index < LINE_SIZE);
  249.  
  250. }
  251.