Sockets - Maple Help

Overview of the Sockets Package

 Calling Sequence Sockets[command](arguments) command(arguments) use Sockets in ... end use

Description

 • The Sockets package is a suite of tools for network communication in Maple. The commands in this package enable you to connect to processes on remote hosts on a network (such as an Intranet or the Internet) and exchange data with these processes. In particular, it enables two independent Maple processes running on different machines on a network to communicate with one another.
 • For important notes on network security, see the release notes for this package Sockets,release.
 • This package presents a user interface to reliable, connection-oriented, stream sockets in the Internet domain (TCP/IP). You can create a client socket by using procedure Sockets[Open], which enables you to connect to and communicate with a server on a remote machine. You can also create a server socket by using procedure Sockets[Serve], which enables you to offer computational services to which others can connect and make requests.
 • The Sockets package automatically shuts down any open connections that it is managing before being garbage collected, or when the Maple process in which it is running terminates normally. However, there is no user control over when this occurs (except in the case of termination), so you should not rely on it to shut down socket connections normally.
 • Each command in the Sockets package can be accessed by using either the long form or the short form of the command name in the command calling sequence.
 As the underlying implementation of the Sockets package is a module, it is also possible to use the form Sockets:-command to access a command from the package. For more information,  see Module Members.
 • If you are using these routines for programming, you can access the exports of this package by enclosing your code in a use statement that binds this package; for example, use Sockets in ... end use. See use for more details.
 • All socket connections created by routines in this package are represented by an opaque type exported as Sockets:-socketID, which is local to the Sockets package. You can test whether an expression expr has the correct structure for a socket ID by using type( expr, Sockets:-socketID ). (Note that the type must not be quoted in this case.)

List of Sockets Package Commands

 • The following is a list of available commands.

 To display the help page for a particular Sockets command, see Getting Help with a Command in a Package.

Examples

 > $\mathrm{with}\left(\mathrm{Sockets}\right)$
 $\left[{\mathrm{Address}}{,}{\mathrm{Close}}{,}{\mathrm{Configure}}{,}{\mathrm{GetHostName}}{,}{\mathrm{GetLocalHost}}{,}{\mathrm{GetLocalPort}}{,}{\mathrm{GetPeerHost}}{,}{\mathrm{GetPeerPort}}{,}{\mathrm{GetProcessID}}{,}{\mathrm{HostInfo}}{,}{\mathrm{LookupService}}{,}{\mathrm{Open}}{,}{\mathrm{ParseURL}}{,}{\mathrm{Peek}}{,}{\mathrm{Read}}{,}{\mathrm{ReadBinary}}{,}{\mathrm{ReadLine}}{,}{\mathrm{Serve}}{,}{\mathrm{Status}}{,}{\mathrm{Write}}{,}{\mathrm{WriteBinary}}\right]$ (1)

Find out where you are on the network.

 > $\mathrm{GetHostName}\left(\right)$
 ${"be527fdac554"}$ (2)

Open a connection to the echo server on the peer (a particular machine on the same network as the machine that generated this help page).

 > $\mathrm{with}\left(\mathrm{Sockets}\right):$
 > $\mathrm{sid}≔\mathrm{Open}\left("mantis","echo"\right)$
 ${0}$ (3)

Send a message to the peer.

 > $\mathrm{Write}\left(\mathrm{sid},\mathrm{sprintf}\left("Hello from %s!\n",\mathrm{GetHostName}\left(\right)\right)\right)$
 ${25}$ (4)

 > $\mathrm{Read}\left(\mathrm{sid}\right)$
 ${"Hello from be527fdac554!"}$ (5)

Shut down the connection.

 > $\mathrm{Close}\left(\mathrm{sid}\right)$
 ${\mathrm{true}}$ (6)

The following program finger enables you to make a query by using the finger protocol.

 > finger := proc( _who )     local who, at, host, sock;     who := _who;     at := StringTools:-FirstFromLeft( "@", who );     if at <> FAIL then         host := who[ 1 + at .. -1 ];         who := who[ 1 .. at - 1 ]     else         host := "localhost"     end if;     use Sockets in         sock := Open( host, finger );         Write( sock, sprintf( "%s\r\n", who ) );         printf( "%s\n", Read( sock, 5 ) );         Close( sock )     end use;     NULL end proc:

References

 For background material on network programming concepts in the Sockets package, see references.