SocketListen

SocketListen[socket,fun]

starts listening on the specified socket, asynchronously applying fun whenever data is received on the socket.

SocketListen[port,fun]

starts listening for active connections on the specified port of 127.0.0.1.

SocketListen[address,fun]

starts listening for active connections on the specified address address on the local machine.

SocketListen[spec,opts]

starts listening for active connections defined by spec using the options opts.

SocketListen[spec]

starts listening for active connections defined by spec, but does not set up handler functions, etc.

Details and Options

  • In SocketListen[socket,], socket is of the form SocketObject[].
  • SocketListen[] gives a SocketListener object.
  • For ZMQ sockets, fun is by default applied whether or not a complete message is received.
  • For raw TCP sockets, fun is by default applied to every data buffer generated by the operating system.
  • With the option settings RecordSeparators->"s" or RecordSeparators->{"s1","s2",}, fun is applied to each block of data received between successive delimiters matching s or the si.
  • The function fun is applied to an association representing each event, with the following default keys:
  • "Data"the data received, as a string
    "Socket"the SocketObject being listened on
    "SourceSocket"the SocketObject that generated the event
    "Timestamp"time when the event occurred (as a DateObject)
  • The following additional keys can also be requested in HandlerFunctionsKeys:
  • "DataBytes"the raw data received, as a list of byte values
    "DataByteArray"the raw data received, as a ByteArray object
    "MultipartComplete"whether a multipart ZMQ message is complete
  • SocketListen accepts the following options:
  • CharacterEncoding$CharacterEncodingthe character encoding to assume
    HandlerFunctionsKeysDefaultkeys to include in the data association
    HandlerFunctionsAutomatichow to handle socket events
    RecordSeparatorsNonehow messages are taken to be delimited
  • The following events can be generated while listening on the socket:
  • "DataReceived"data is available on the socket

Examples

open allclose all

Basic Examples  (1)

Listen on an available socket to print off the association for the event:

In[1]:=
Click for copyable input
Out[1]=

Send some data (printed messages may appear in the Messages window):

In[2]:=
Click for copyable input

Delete the listener and close the sockets:

In[3]:=
Click for copyable input
Out[3]=

Scope  (2)

Options  (5)

Applications  (1)

Interactive Examples  (1)

See Also

SocketListener  SocketOpen  SocketConnect  ChannelListen

Introduced in 2017
(11.2)