  
  [1X3 Using streams[0X
  
  The  package  implements  new  kind  of  [5XGAP[0m  input-output  streams,  called
  input-output  TCP  streams.  Such streams are based on the functionality for
  the TCP/IP protocol usage provided by the [5XGAP[0m package [5XIO[0m, and may constitute
  an independent interest for [5XGAP[0m users.
  
  Input-output TCP streams are intended to support all operations, implemented
  for  streams  in  [5XGAP[0m.  It  is  assumed that all existing code using streams
  should  work  with  this kind of streams as well (please let us know, if you
  will  notice  that  this  is  not  the  case!).  We  installed  methods  for
  input-output  TCP  streams  to  support  the  following  operations: [2XViewObj[0m
  ([14XReference:  ViewObj[0m),  [2XPrintObj[0m ([14XReference: PrintObj[0m), [2XReadByte[0m ([14XReference:
  ReadByte[0m),  [2XReadLine[0m  ([14XReference:  ReadLine[0m),  [2XReadAll[0m ([14XReference: ReadAll[0m),
  [2XWriteByte[0m ([14XReference: WriteByte[0m), [2XWriteLine[0m ([14XReference: WriteLine[0m), [2XWriteAll[0m
  ([14XReference: WriteAll[0m), [2XIsEndOfStream[0m ([14XReference: IsEndOfStream[0m), [2XCloseStream[0m
  ([14XReference:       CloseStream[0m),      [2XFileDescriptorOfStream[0m      ([14XReference:
  FileDescriptorOfStream[0m), [2XUNIXSelect[0m ([14XReference: UNIXSelect[0m).
  
  
  [1X3.1 Input-output TCP streams[0X
  
  [1X3.1-1 IsInputOutputTCPStream[0m
  
  [2X> IsInputOutputTCPStream______________________________________________[0Xfilter
  
  [2XIsInputOutputTCPStream[0m  is  a subcategory of [2XIsInputOutputStream[0m ([14XReference:
  IsInputOutputStream[0m).  Streams  in  the  category [2XIsInputOutputTCPStream[0m are
  created  with the help of the function [2XInputOutputTCPStream[0m ([14X3.1-3[0m) with one
  or  two  arguments dependently on whether they will be used in the client or
  server  mode.  Examples  of  their  creation  and  usage  will  be  given in
  subsequent sections.
  
  [1X3.1-2 IsInputOutputTCPStreamRep[0m
  
  [2X> IsInputOutputTCPStreamRep___________________________________________[0Xfilter
  
  This   is   the   representation   used   for   streams   in   the  category
  [2XIsInputOutputTCPStream[0m ([14X3.1-1[0m).
  
  [1X3.1-3 InputOutputTCPStream[0m
  
  [2X> InputOutputTCPStream( [0X[3Xdesc[0X[2X ) _____________________________________[0Xfunction
  [2X> InputOutputTCPStream( [0X[3Xhost, port[0X[2X ) _______________________________[0Xfunction
  [6XReturns:[0X  stream
  
  The  one-argument version must be called from the [5XSCSCP[0m server. Its argument
  [3Xdesc[0m  must  be  a  socket descriptor obtained using [2XIO_accept[0m ([14XIO_accept???[0m)
  function from the [5XIO[0m package (see the example below). It returns a stream in
  the  category  [2XIsInputOutputTCPStream[0m  ([14X3.1-1[0m) which will use this socket to
  accept  incoming  connections.  In  most  cases, the one-argument version is
  called automatically from [2XRunSCSCPserver[0m ([14X5.2-1[0m) rather then manually.
  
  The  version  with two arguments, a string [3Xhost[0m and an integer [3Xport[0m, must be
  called  from  the  [5XSCSCP[0m  client.  It  returns  a  stream  in  the  category
  [2XIsInputOutputTCPStream[0m  ([14X3.1-1[0m)  which  will  be  used  by  the  client  for
  communication  with  the [5XSCSCP[0m server running at hostname [3Xhost[0m on port [3Xport[0m.
  In  most  cases,  the  two-argument version is called automatically from the
  higher level functions, for example, [2XEvaluateBySCSCP[0m ([14X6.3-1[0m).
  
  
  [1X3.2 Example of client-server communication via input-output TCP streams[0X
  
  The  following example demonstrates the low-level interaction between client
  and  server  using  input-output  TCP stream, and shows how such streams are
  created  in the function [2XRunSCSCPserver[0m ([14X5.2-1[0m). It uses some functions from
  the  [5XIO[0m package (see the [5XIO[0m manual for their description). We will show step
  by  step  what  is  happens on server and client (of course, if you will try
  this example, the numbers denoting descriptors may be different).
  
  Firts,  we  will start two [5XGAP[0m sessions, one for the server, another one for
  the client. Now we enter the following commands on the server's side:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> sock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );[0X
    [4X3[0X
    [4Xgap> lookup := IO_gethostbyname( "localhost" );[0X
    [4Xrec( name := "localhost", aliases := [  ], addrtype := 2, length := 4, [0X
    [4X  addr := [ "\177\000\000\>" ] )[0X
    [4Xgap> port:=26133;[0X
    [4X26133[0X
    [4Xgap> res := IO_bind( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );[0X
    [4Xtrue[0X
    [4Xgap> IO_listen( sock, 5 );[0X
    [4Xtrue[0X
    [4Xgap> socket_descriptor := IO_accept( sock, IO_MakeIPAddressPort("0.0.0.0",0) );[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  After  the  last  command you will not see the [5XGAP[0m prompt because the server
  starts  to  wait  for an incoming connection. Now we go to the client's side
  and  create an input-output TCP stream to the server. Here it can be created
  in one step:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> clientstream:=InputOutputTCPStream( "localhost", 26133 );[0X
    [4XCreating a socket...[0X
    [4XConnecting to a remote socket via TCP/IP...[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  Now  we  are  trying to connect to the server, and as soon as the connection
  will  be  established, the stream will be created at the client side, and we
  will see the output and the new [5XGAP[0m prompt:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4X< input/output TCP stream to localhost >[0X
    [4Xgap>[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  On  the  server you will get the socket descriptor and then you will be able
  to create a stream from it:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4X4[0X
    [4Xgap> serverstream := InputOutputTCPStream( socket_descriptor );[0X
    [4X< input/output TCP stream to socket >[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  Now  we can write to this stream on the client side and then read from it on
  the server side and backwards. First, write on the client:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> WriteLine( clientstream, "12345" );[0X
    [4Xtrue[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  Now read and write on the server:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ReadLine( serverstream );[0X
    [4X"12345\n"[0X
    [4Xgap> WriteLine( serverstream, "54321" );[0X
    [4Xtrue[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  And finally we read on the client and close the stream:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ReadLine( clientstream );[0X
    [4X"54321\n"[0X
    [4Xgap> CloseStream( clientstream );[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  and similarly close the stream on the server:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> CloseStream( serverstream );[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  In  this way one can organise remote communication between two copies of [5XGAP[0m
  in  various  ways.  In  subsequent chapters we explain how it is implemented
  using  [5XSCSCP[0m  to  ensure  compatibility not only with [5XGAP[0m but with any other
  [5XSCSCP[0m-compliant system.
  
