Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
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
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X3 [33X[0;0YUsing streams[133X[101X23[33X[0;0YThe package implements new kind of [5XGAP[105X input-output streams, called4input-output TCP streams. Such streams are based on the functionality for5the TCP/IP protocol usage provided by the [5XGAP[105X package [5XIO[105X, and may constitute6an independent interest for [5XGAP[105X users.[133X78[33X[0;0YInput-output TCP streams are intended to support all operations, implemented9for streams in [5XGAP[105X. It is assumed that all existing code using streams10should work with this kind of streams as well (please let us know, if you11will notice that this is not the case!). We installed methods for12input-output TCP streams to support the following operations: [2XViewObj[102X13([14XReference: ViewObj[114X), [2XPrintObj[102X ([14XReference: PrintObj[114X), [2XReadByte[102X ([14XReference:14ReadByte[114X), [2XReadLine[102X ([14XReference: ReadLine[114X), [2XReadAll[102X ([14XReference: ReadAll[114X),15[2XWriteByte[102X ([14XReference: WriteByte[114X), [2XWriteLine[102X ([14XReference: WriteLine[114X), [2XWriteAll[102X16([14XReference: WriteAll[114X), [2XIsEndOfStream[102X ([14XReference: IsEndOfStream[114X), [2XCloseStream[102X17([14XReference: CloseStream[114X), [2XFileDescriptorOfStream[102X ([14XReference:18FileDescriptorOfStream[114X), [2XUNIXSelect[102X ([14XReference: UNIXSelect[114X).[133X192021[1X3.1 [33X[0;0YInput-output TCP streams[133X[101X2223[1X3.1-1 IsInputOutputTCPStream[101X2425[29X[2XIsInputOutputTCPStream[102X[32X filter2627[33X[0;0Y[2XIsInputOutputTCPStream[102X is a subcategory of [2XIsInputOutputStream[102X ([14XReference:28IsInputOutputStream[114X). Streams in the category [2XIsInputOutputTCPStream[102X are29created with the help of the function [2XInputOutputTCPStream[102X ([14X3.1-3[114X) with one30or two arguments dependently on whether they will be used in the client or31server mode. Examples of their creation and usage will be given in32subsequent sections.[133X3334[1X3.1-2 IsInputOutputTCPStreamRep[101X3536[29X[2XIsInputOutputTCPStreamRep[102X[32X filter3738[33X[0;0YThis is the representation used for streams in the category39[2XIsInputOutputTCPStream[102X ([14X3.1-1[114X).[133X4041[1X3.1-3 InputOutputTCPStream[101X4243[29X[2XInputOutputTCPStream[102X( [3Xdesc[103X ) [32X function44[29X[2XInputOutputTCPStream[102X( [3Xhost[103X, [3Xport[103X ) [32X function45[6XReturns:[106X [33X[0;10Ystream[133X4647[33X[0;0YThe one-argument version must be called from the [5XSCSCP[105X server. Its argument48[3Xdesc[103X must be a socket descriptor obtained using [2XIO_accept[102X ([14XIO: IO_accept[114X)49function from the [5XIO[105X package (see the example below). It returns a stream in50the category [2XIsInputOutputTCPStream[102X ([14X3.1-1[114X) which will use this socket to51accept incoming connections. In most cases, the one-argument version is52called automatically from [2XRunSCSCPserver[102X ([14X5.2-1[114X) rather then manually.[133X5354[33X[0;0YThe version with two arguments, a string [3Xhost[103X and an integer [3Xport[103X, must be55called from the [5XSCSCP[105X client. It returns a stream in the category56[2XIsInputOutputTCPStream[102X ([14X3.1-1[114X) which will be used by the client for57communication with the [5XSCSCP[105X server running at hostname [3Xhost[103X on port [3Xport[103X.58In most cases, the two-argument version is called automatically from the59higher level functions, for example, [2XEvaluateBySCSCP[102X ([14X6.3-1[114X).[133X606162[1X3.2 [33X[0;0YExample of client-server communication via input-output TCP streams[133X[101X6364[33X[0;0YThe following example demonstrates the low-level interaction between client65and server using input-output TCP stream, and shows how such streams are66created in the function [2XRunSCSCPserver[102X ([14X5.2-1[114X). It uses some functions from67the [5XIO[105X package (see the [5XIO[105X manual for their description). We will show step68by step what is happens on server and client (of course, if you will try69this example, the numbers denoting descriptors may be different).[133X7071[33X[0;0YFirts, we will start two [5XGAP[105X sessions, one for the server, another one for72the client. Now we enter the following commands on the server's side:[133X7374[4X[32X Example [32X[104X75[4X[28X[128X[104X76[4X[25Xgap>[125X [27Xsock := IO_socket( IO.PF_INET, IO.SOCK_STREAM, "tcp" );[127X[104X77[4X[28X3[128X[104X78[4X[25Xgap>[125X [27Xlookup := IO_gethostbyname( "localhost" );[127X[104X79[4X[28Xrec( name := "localhost", aliases := [ ], addrtype := 2, length := 4, [128X[104X80[4X[28X addr := [ "\177\000\000\>" ] )[128X[104X81[4X[25Xgap>[125X [27Xport:=26133;[127X[104X82[4X[28X26133[128X[104X83[4X[25Xgap>[125X [27Xres := IO_bind( sock, IO_make_sockaddr_in( lookup.addr[1], port ) );[127X[104X84[4X[28Xtrue[128X[104X85[4X[25Xgap>[125X [27XIO_listen( sock, 5 );[127X[104X86[4X[28Xtrue[128X[104X87[4X[25Xgap>[125X [27Xsocket_descriptor := IO_accept( sock, IO_MakeIPAddressPort("0.0.0.0",0) );[127X[104X88[4X[28X[128X[104X89[4X[32X[104X9091[33X[0;0YAfter the last command you will not see the [5XGAP[105X prompt because the server92starts to wait for an incoming connection. Now we go to the client's side93and create an input-output TCP stream to the server. Here it can be created94in one step:[133X9596[4X[32X Example [32X[104X97[4X[28X[128X[104X98[4X[25Xgap>[125X [27Xclientstream:=InputOutputTCPStream( "localhost", 26133 );[127X[104X99[4X[28XCreating a socket...[128X[104X100[4X[28XConnecting to a remote socket via TCP/IP...[128X[104X101[4X[28X[128X[104X102[4X[32X[104X103104[33X[0;0YNow we are trying to connect to the server, and as soon as the connection105will be established, the stream will be created at the client side, and we106will see the output and the new [5XGAP[105X prompt:[133X107108[4X[32X Example [32X[104X109[4X[28X[128X[104X110[4X[28X< input/output TCP stream to localhost >[128X[104X111[4X[28Xgap>[128X[104X112[4X[28X[128X[104X113[4X[32X[104X114115[33X[0;0YOn the server you will get the socket descriptor and then you will be able116to create a stream from it:[133X117118[4X[32X Example [32X[104X119[4X[28X[128X[104X120[4X[28X4[128X[104X121[4X[25Xgap>[125X [27Xserverstream := InputOutputTCPStream( socket_descriptor );[127X[104X122[4X[28X< input/output TCP stream to socket >[128X[104X123[4X[28X[128X[104X124[4X[32X[104X125126[33X[0;0YNow we can write to this stream on the client side and then read from it on127the server side and backwards. First, write on the client:[133X128129[4X[32X Example [32X[104X130[4X[28X[128X[104X131[4X[25Xgap>[125X [27XWriteLine( clientstream, "12345" );[127X[104X132[4X[28Xtrue[128X[104X133[4X[28X[128X[104X134[4X[32X[104X135136[33X[0;0YNow read and write on the server:[133X137138[4X[32X Example [32X[104X139[4X[28X[128X[104X140[4X[25Xgap>[125X [27XReadLine( serverstream );[127X[104X141[4X[28X"12345\n"[128X[104X142[4X[25Xgap>[125X [27XWriteLine( serverstream, "54321" );[127X[104X143[4X[28Xtrue[128X[104X144[4X[28X[128X[104X145[4X[32X[104X146147[33X[0;0YAnd finally we read on the client and close the stream:[133X148149[4X[32X Example [32X[104X150[4X[28X[128X[104X151[4X[25Xgap>[125X [27XReadLine( clientstream );[127X[104X152[4X[28X"54321\n"[128X[104X153[4X[25Xgap>[125X [27XCloseStream( clientstream );[127X[104X154[4X[28X[128X[104X155[4X[32X[104X156157[33X[0;0Yand similarly close the stream on the server:[133X158159[4X[32X Example [32X[104X160[4X[28X[128X[104X161[4X[25Xgap>[125X [27XCloseStream( serverstream );[127X[104X162[4X[28X[128X[104X163[4X[32X[104X164165[33X[0;0YIn this way one can organise remote communication between two copies of [5XGAP[105X166in various ways. In subsequent chapters we explain how it is implemented167using [5XSCSCP[105X to ensure compatibility not only with [5XGAP[105X but with any other168[5XSCSCP[105X-compliant system.[133X169170171172