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 $CharacterEncoding the character encoding to assume HandlerFunctionsKeys Default keys to include in the data association HandlerFunctions Automatic how to handle socket events RecordSeparators None how 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 allBasic Examples (1)
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:
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:
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:
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:
HandlerFunctions (1)
Specify the function to run on generated data with HandlerFunctions and the "DataReceived" event:
CharacterEncoding (1)
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)
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