1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
<Chapter Label="UsingStreams">
<Heading>Using streams</Heading>
The package implements new kind of &GAP; input-output streams, called
input-output TCP streams. Such streams are based on the functionality
for the TCP/IP protocol usage provided by the &GAP; package &IO;, and
may constitute an independent interest for &GAP; users.
<P/>
Input-output TCP streams are intended to support all operations, implemented
for streams in &GAP;. 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:
<Ref BookName="ref" Oper="ViewObj"/>,
<Ref BookName="ref" Oper="PrintObj"/>,
<Ref BookName="ref" Oper="ReadByte"/>,
<Ref BookName="ref" Oper="ReadLine"/>,
<Ref BookName="ref" Oper="ReadAll"/>,
<Ref BookName="ref" Oper="WriteByte"/>,
<Ref BookName="ref" Oper="WriteLine"/>,
<Ref BookName="ref" Oper="WriteAll"/>,
<Ref BookName="ref" Oper="IsEndOfStream"/>,
<Ref BookName="ref" Oper="CloseStream"/>,
<Ref BookName="ref" Oper="FileDescriptorOfStream"/>,
<Ref BookName="ref" Oper="UNIXSelect"/>.
<P/>
<Section Label="Streams">
<Heading>Input-output TCP streams</Heading>
<#Include Label="IsInputOutputTCPStream">
<#Include Label="IsInputOutputTCPStreamRep">
<#Include Label="InputOutputTCPStream">
</Section>
<Section Label="StreamsExample">
<Heading>Example of client-server communication via input-output TCP streams</Heading>
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 <Ref Func="RunSCSCPserver"/>. It uses some
functions from the &IO; package (see the &IO; 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).
<P/>
Firts, we will start two &GAP; sessions, one for the server, another one
for the client. Now we enter the following commands on the server's side:
<Log>
<![CDATA[
gap> sock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );
3
gap> lookup := IO_gethostbyname( "localhost" );
rec( name := "localhost", aliases := [ ], addrtype := 2, length := 4,
addr := [ "\177\000\000\>" ] )
gap> port:=26133;
26133
gap> res := IO_bind( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );
true
gap> IO_listen( sock, 5 );
true
gap> socket_descriptor := IO_accept( sock, IO_MakeIPAddressPort("0.0.0.0",0) );
]]>
</Log>
After the last command you will not see the &GAP; 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:
<Log>
<![CDATA[
gap> clientstream:=InputOutputTCPStream( "localhost", 26133 );
Creating a socket...
Connecting to a remote socket via TCP/IP...
]]>
</Log>
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 &GAP; prompt:
<Log>
<![CDATA[
< input/output TCP stream to localhost >
gap>
]]>
</Log>
On the server you will get the socket descriptor and then you will be able
to create a stream from it:
<Log>
<![CDATA[
4
gap> serverstream := InputOutputTCPStream( socket_descriptor );
< input/output TCP stream to socket >
]]>
</Log>
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:
<Log>
<![CDATA[
gap> WriteLine( clientstream, "12345" );
true
]]>
</Log>
Now read and write on the server:
<Log>
<![CDATA[
gap> ReadLine( serverstream );
"12345\n"
gap> WriteLine( serverstream, "54321" );
true
]]>
</Log>
And finally we read on the client and close the stream:
<Log>
<![CDATA[
gap> ReadLine( clientstream );
"54321\n"
gap> CloseStream( clientstream );
]]>
</Log>
and similarly close the stream on the server:
<Log>
<![CDATA[
gap> CloseStream( serverstream );
]]>
</Log>
In this way one can organise remote communication between two copies of
&GAP; in various ways. In subsequent chapters we explain how it is
implemented using &SCSCP; to ensure compatibility not only with &GAP;
but with any other &SCSCP;-compliant system.
</Section>
</Chapter>
|