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.
  • If SocketListen[spec,] creates a new SocketObject, that socket is closed automatically when DeleteObject is called on the returned SocketListener.
  • 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
    HandlerFunctionsKeys Defaultkeys to include in the data association
    HandlerFunctions Automatichow to handle socket events
    RecordSeparators Nonehow 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:

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

Delete the listener and close the sockets:

Scope  (2)

Listen on a TCP socket on an open port on your system, printing off all data:

Connect to the socket with a web browser:

Opening the URL with the system browser will print the request in the Messages window:

Stop listening:

Make a pair of client and server sockets:

Submit to the server socket a HandlerFunctions for incoming data to save the message inside a variable:

When the client writes to the socket, the server will print the message:

Inspect the message:

Change the HandlerFunctions to additionally print the data as a list of bytes:

Stop listening for events with this socket:

Because the listener no longer exists, messages sent to the server will not call any HandlerFunctions:

The handler function did not update the value of msg:

The message can be read from the server with SocketReadMessage:

Close the sockets:

Options  (4)

HandlerFunctionsKeys  (1)

Modify the HandlerFunctionsKeys to be only the data as a ByteArray with "DataByteArray":

Change the value after starting listening on the socket:

Show the updated value of the HandlerFunctionsKeys:

Stop listening and close the socket:

HandlerFunctions  (1)

Specify the function to run on generated data with HandlerFunctions and the "DataReceived" event:

Change the value after starting it:

Stop listening and close the socket:

CharacterEncoding  (1)

Listen for messages, encoding the string key "Data" with a different encoding and encoding the "DataBytes" normally:

Inspect the message to see the different encodings:

Stop listening and close the socket:

RecordSeparators  (1)

Listen on a socket, splitting up messages by whitespace:

Create a client and send a message on this client to the listener's socket:

Now msgs will have the message string split up by the RecordSeparators value:

Stop listening and close the socket:

Listen on a socket, splitting up messages by a set number of bytes:

Applications  (1)

Start a web server, sending a basic HTML page back as the response to all clients:

Import the result from the Wolfram Language:

Open the website in your system browser:

Stop listening and close the socket:

Interactive Examples  (1)

Listen on a socket, continuously writing again to the server a rotated message as soon as a message is received:

Write out an initial message:

Visualize the dynamic rotation of the string:

Stop listening and close the sockets:

Wolfram Research (2017), SocketListen, Wolfram Language function, https://reference.wolfram.com/language/ref/SocketListen.html (updated 2021).

Text

Wolfram Research (2017), SocketListen, Wolfram Language function, https://reference.wolfram.com/language/ref/SocketListen.html (updated 2021).

CMS

Wolfram Language. 2017. "SocketListen." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/SocketListen.html.

APA

Wolfram Language. (2017). SocketListen. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SocketListen.html

BibTeX

@misc{reference.wolfram_2024_socketlisten, author="Wolfram Research", title="{SocketListen}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/SocketListen.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_socketlisten, organization={Wolfram Research}, title={SocketListen}, year={2021}, url={https://reference.wolfram.com/language/ref/SocketListen.html}, note=[Accessed: 21-December-2024 ]}