Sockets

Sockets[]

returns all active socket connections initiated by your current Wolfram Language session.

Sockets[All]

returns all sockets connected to your current session, including remote sockets originating outside your current session.

Sockets[spec]

returns only sockets specified by spec.

Details

  • Returned sockets are all given as SocketObject constructs.
  • Possible forms of spec include:
  • "Remote"sockets that originate outside the current session
    "Local"sockets that originate within the current session
    "Server"local sockets opened with SocketOpen
    "Client"local sockets connected with SocketConnect
    "TCP"sockets with protocol of "TCP"
    "ZMQ"sockets with protocol of "ZMQ"
    {"ZMQ","type"}sockets with protocol "ZMQ_type"
    Allall local and remote sockets
  • Close closes socket connections and frees associated resources.
  • If a socket is opened with SocketOpen and connected to in the same Wolfram Language session with SocketConnect, both sides of the socket will have an associated SocketObject.

Examples

open allclose all

Basic Examples  (1)

Open a socket and show all currently active sockets:

Close the socket:

Scope  (2)

Only see local sockets on your machine:

Close the socket:

Listen on a socket with SocketListen and see the socket that is listened on:

The socket can also be viewed with the property "Socket" on the SocketListener[] object:

Close the socket and delete the listener:

Possible Issues  (1)

When connecting to a server socket with SocketConnect in the same session, the socket returned by SocketConnect is different than the socket object returned by the server socket's "ConnectedClients" property.

Open a server socket and connect a client to it:

Sockets[] will only show the connections you opened, because they are both local:

There is also an additional remote socket created with the same details as the client socket accessible inside Sockets["Remote"]:

The UUID for the remote client does not match the local client returned by SocketConnect:

These two sockets are the two ends of the socket connection; writing to one of them will send a message to the other:

Closing the server will disconnect the remote client and remove it from the list of active sockets:

Close the client:

Introduced in 2017
 (11.2)
 |
Updated in 2020
 (12.1)