CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Views: 418346
1
2
3 Using streams
3
4
The package implements new kind of GAP input-output streams, called
5
input-output TCP streams. Such streams are based on the functionality for
6
the TCP/IP protocol usage provided by the GAP package IO, and may constitute
7
an independent interest for GAP users.
8
9
Input-output TCP streams are intended to support all operations, implemented
10
for streams in GAP. It is assumed that all existing code using streams
11
should work with this kind of streams as well (please let us know, if you
12
will notice that this is not the case!). We installed methods for
13
input-output TCP streams to support the following operations: ViewObj
14
(Reference: ViewObj), PrintObj (Reference: PrintObj), ReadByte (Reference:
15
ReadByte), ReadLine (Reference: ReadLine), ReadAll (Reference: ReadAll),
16
WriteByte (Reference: WriteByte), WriteLine (Reference: WriteLine), WriteAll
17
(Reference: WriteAll), IsEndOfStream (Reference: IsEndOfStream), CloseStream
18
(Reference: CloseStream), FileDescriptorOfStream (Reference:
19
FileDescriptorOfStream), UNIXSelect (Reference: UNIXSelect).
20
21
22
3.1 Input-output TCP streams
23
24
3.1-1 IsInputOutputTCPStream
25
26
IsInputOutputTCPStream filter
27
28
IsInputOutputTCPStream is a subcategory of IsInputOutputStream (Reference:
29
IsInputOutputStream). Streams in the category IsInputOutputTCPStream are
30
created with the help of the function InputOutputTCPStream (3.1-3) with one
31
or two arguments dependently on whether they will be used in the client or
32
server mode. Examples of their creation and usage will be given in
33
subsequent sections.
34
35
3.1-2 IsInputOutputTCPStreamRep
36
37
IsInputOutputTCPStreamRep filter
38
39
This is the representation used for streams in the category
40
IsInputOutputTCPStream (3.1-1).
41
42
3.1-3 InputOutputTCPStream
43
44
InputOutputTCPStream( desc )  function
45
InputOutputTCPStream( host, port )  function
46
Returns: stream
47
48
The one-argument version must be called from the SCSCP server. Its argument
49
desc must be a socket descriptor obtained using IO_accept (IO: IO_accept)
50
function from the IO package (see the example below). It returns a stream in
51
the category IsInputOutputTCPStream (3.1-1) which will use this socket to
52
accept incoming connections. In most cases, the one-argument version is
53
called automatically from RunSCSCPserver (5.2-1) rather then manually.
54
55
The version with two arguments, a string host and an integer port, must be
56
called from the SCSCP client. It returns a stream in the category
57
IsInputOutputTCPStream (3.1-1) which will be used by the client for
58
communication with the SCSCP server running at hostname host on port port.
59
In most cases, the two-argument version is called automatically from the
60
higher level functions, for example, EvaluateBySCSCP (6.3-1).
61
62
63
3.2 Example of client-server communication via input-output TCP streams
64
65
The following example demonstrates the low-level interaction between client
66
and server using input-output TCP stream, and shows how such streams are
67
created in the function RunSCSCPserver (5.2-1). It uses some functions from
68
the IO package (see the IO manual for their description). We will show step
69
by step what is happens on server and client (of course, if you will try
70
this example, the numbers denoting descriptors may be different).
71
72
Firts, we will start two GAP sessions, one for the server, another one for
73
the client. Now we enter the following commands on the server's side:
74
75
 Example 
76

77
gap> sock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );
78
3
79
gap> lookup := IO_gethostbyname( "localhost" );
80
rec( name := "localhost", aliases := [ ], addrtype := 2, length := 4, 
81
 addr := [ "\177\000\000\>" ] )
82
gap> port:=26133;
83
26133
84
gap> res := IO_bind( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );
85
true
86
gap> IO_listen( sock, 5 );
87
true
88
gap> socket_descriptor := IO_accept( sock, IO_MakeIPAddressPort("0.0.0.0",0) );
89

90

91
92
After the last command you will not see the GAP prompt because the server
93
starts to wait for an incoming connection. Now we go to the client's side
94
and create an input-output TCP stream to the server. Here it can be created
95
in one step:
96
97
 Example 
98

99
gap> clientstream:=InputOutputTCPStream( "localhost", 26133 );
100
Creating a socket...
101
Connecting to a remote socket via TCP/IP...
102

103

104
105
Now we are trying to connect to the server, and as soon as the connection
106
will be established, the stream will be created at the client side, and we
107
will see the output and the new GAP prompt:
108
109
 Example 
110

111
< input/output TCP stream to localhost >
112
gap>
113

114

115
116
On the server you will get the socket descriptor and then you will be able
117
to create a stream from it:
118
119
 Example 
120

121
4
122
gap> serverstream := InputOutputTCPStream( socket_descriptor );
123
< input/output TCP stream to socket >
124

125

126
127
Now we can write to this stream on the client side and then read from it on
128
the server side and backwards. First, write on the client:
129
130
 Example 
131

132
gap> WriteLine( clientstream, "12345" );
133
true
134

135

136
137
Now read and write on the server:
138
139
 Example 
140

141
gap> ReadLine( serverstream );
142
"12345\n"
143
gap> WriteLine( serverstream, "54321" );
144
true
145

146

147
148
And finally we read on the client and close the stream:
149
150
 Example 
151

152
gap> ReadLine( clientstream );
153
"54321\n"
154
gap> CloseStream( clientstream );
155

156

157
158
and similarly close the stream on the server:
159
160
 Example 
161

162
gap> CloseStream( serverstream );
163

164

165
166
In this way one can organise remote communication between two copies of GAP
167
in various ways. In subsequent chapters we explain how it is implemented
168
using SCSCP to ensure compatibility not only with GAP but with any other
169
SCSCP-compliant system.
170
171
172