  
  [1X3 [33X[0;0YUsing streams[133X[101X
  
  [33X[0;0YThe  package  implements  new  kind  of  [5XGAP[105X  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[105X package [5XIO[105X, and may constitute
  an independent interest for [5XGAP[105X users.[133X
  
  [33X[0;0YInput-output TCP streams are intended to support all operations, implemented
  for  streams  in  [5XGAP[105X.  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[102X
  ([14XReference:  ViewObj[114X),  [2XPrintObj[102X ([14XReference: PrintObj[114X), [2XReadByte[102X ([14XReference:
  ReadByte[114X),  [2XReadLine[102X  ([14XReference:  ReadLine[114X),  [2XReadAll[102X ([14XReference: ReadAll[114X),
  [2XWriteByte[102X ([14XReference: WriteByte[114X), [2XWriteLine[102X ([14XReference: WriteLine[114X), [2XWriteAll[102X
  ([14XReference: WriteAll[114X), [2XIsEndOfStream[102X ([14XReference: IsEndOfStream[114X), [2XCloseStream[102X
  ([14XReference:       CloseStream[114X),      [2XFileDescriptorOfStream[102X      ([14XReference:
  FileDescriptorOfStream[114X), [2XUNIXSelect[102X ([14XReference: UNIXSelect[114X).[133X
  
  
  [1X3.1 [33X[0;0YInput-output TCP streams[133X[101X
  
  [1X3.1-1 IsInputOutputTCPStream[101X
  
  [29X[2XIsInputOutputTCPStream[102X[32X filter
  
  [33X[0;0Y[2XIsInputOutputTCPStream[102X  is  a subcategory of [2XIsInputOutputStream[102X ([14XReference:
  IsInputOutputStream[114X).  Streams  in  the  category [2XIsInputOutputTCPStream[102X are
  created  with the help of the function [2XInputOutputTCPStream[102X ([14X3.1-3[114X) 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.[133X
  
  [1X3.1-2 IsInputOutputTCPStreamRep[101X
  
  [29X[2XIsInputOutputTCPStreamRep[102X[32X filter
  
  [33X[0;0YThis   is   the   representation   used   for   streams   in   the  category
  [2XIsInputOutputTCPStream[102X ([14X3.1-1[114X).[133X
  
  [1X3.1-3 InputOutputTCPStream[101X
  
  [29X[2XInputOutputTCPStream[102X( [3Xdesc[103X ) [32X function
  [29X[2XInputOutputTCPStream[102X( [3Xhost[103X, [3Xport[103X ) [32X function
  [6XReturns:[106X  [33X[0;10Ystream[133X
  
  [33X[0;0YThe  one-argument version must be called from the [5XSCSCP[105X server. Its argument
  [3Xdesc[103X  must  be  a socket descriptor obtained using [2XIO_accept[102X ([14XIO: IO_accept[114X)
  function from the [5XIO[105X package (see the example below). It returns a stream in
  the  category  [2XIsInputOutputTCPStream[102X  ([14X3.1-1[114X) which will use this socket to
  accept  incoming  connections.  In  most  cases, the one-argument version is
  called automatically from [2XRunSCSCPserver[102X ([14X5.2-1[114X) rather then manually.[133X
  
  [33X[0;0YThe  version  with two arguments, a string [3Xhost[103X and an integer [3Xport[103X, must be
  called  from  the  [5XSCSCP[105X  client.  It  returns  a  stream  in  the  category
  [2XIsInputOutputTCPStream[102X  ([14X3.1-1[114X)  which  will  be  used  by  the  client  for
  communication  with  the [5XSCSCP[105X server running at hostname [3Xhost[103X on port [3Xport[103X.
  In  most  cases,  the  two-argument version is called automatically from the
  higher level functions, for example, [2XEvaluateBySCSCP[102X ([14X6.3-1[114X).[133X
  
  
  [1X3.2 [33X[0;0YExample of client-server communication via input-output TCP streams[133X[101X
  
  [33X[0;0YThe  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[102X ([14X5.2-1[114X). It uses some functions from
  the  [5XIO[105X package (see the [5XIO[105X 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).[133X
  
  [33X[0;0YFirts,  we  will start two [5XGAP[105X sessions, one for the server, another one for
  the client. Now we enter the following commands on the server's side:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27Xsock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );[127X[104X
    [4X[28X3[128X[104X
    [4X[25Xgap>[125X [27Xlookup := IO_gethostbyname( "localhost" );[127X[104X
    [4X[28Xrec( name := "localhost", aliases := [  ], addrtype := 2, length := 4, [128X[104X
    [4X[28X  addr := [ "\177\000\000\>" ] )[128X[104X
    [4X[25Xgap>[125X [27Xport:=26133;[127X[104X
    [4X[28X26133[128X[104X
    [4X[25Xgap>[125X [27Xres := IO_bind( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27XIO_listen( sock, 5 );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27Xsocket_descriptor := IO_accept( sock, IO_MakeIPAddressPort("0.0.0.0",0) );[127X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YAfter  the  last  command you will not see the [5XGAP[105X 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:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27Xclientstream:=InputOutputTCPStream( "localhost", 26133 );[127X[104X
    [4X[28XCreating a socket...[128X[104X
    [4X[28XConnecting to a remote socket via TCP/IP...[128X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YNow  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[105X prompt:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[28X< input/output TCP stream to localhost >[128X[104X
    [4X[28Xgap>[128X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YOn  the  server you will get the socket descriptor and then you will be able
  to create a stream from it:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[28X4[128X[104X
    [4X[25Xgap>[125X [27Xserverstream := InputOutputTCPStream( socket_descriptor );[127X[104X
    [4X[28X< input/output TCP stream to socket >[128X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YNow  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:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27XWriteLine( clientstream, "12345" );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YNow read and write on the server:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27XReadLine( serverstream );[127X[104X
    [4X[28X"12345\n"[128X[104X
    [4X[25Xgap>[125X [27XWriteLine( serverstream, "54321" );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YAnd finally we read on the client and close the stream:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27XReadLine( clientstream );[127X[104X
    [4X[28X"54321\n"[128X[104X
    [4X[25Xgap>[125X [27XCloseStream( clientstream );[127X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0Yand similarly close the stream on the server:[133X
  
  [4X[32X  Example  [32X[104X
    [4X[28X[128X[104X
    [4X[25Xgap>[125X [27XCloseStream( serverstream );[127X[104X
    [4X[28X[128X[104X
  [4X[32X[104X
  
  [33X[0;0YIn  this way one can organise remote communication between two copies of [5XGAP[105X
  in  various  ways.  In  subsequent chapters we explain how it is implemented
  using  [5XSCSCP[105X  to  ensure  compatibility not only with [5XGAP[105X but with any other
  [5XSCSCP[105X-compliant system.[133X
  
