SocketReadMessage

SocketReadMessage[socket]

reads the next available message on the specified socket, returning it as a byte array.

Details and Options

  • The socket is specified as a SocketObject[].
  • For ZMQ sockets, SocketReadMessage returns the next discrete message sent to the socket. It returns all multi-part elements joined together into a single ByteArray.
  • For TCP sockets, SocketReadMessage will return a ByteArray of the data currently in the operating system buffer.

Examples

open allclose all

Basic Examples  (2)

Open a ZMQ socket and connect a client to it:

In[1]:=
Click for copyable input

Writing a message from one side will send the message to the other side as a ByteArray:

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

Turn it into a string with ByteArrayToString:

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

Close the sockets:

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

For TCP sockets, the size of the message returned by SocketReadMessage is not deterministic and is controlled by the OS:

In[1]:=
Click for copyable input

Write out a large message from the server to the client:

In[2]:=
Click for copyable input

Read messages from the socket until there is no more data available:

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

Confirm the individual buffers add up to the entire message sent on the socket:

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

Close the sockets:

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

Scope  (1)

Applications  (1)

Neat Examples  (1)

See Also

SocketConnect  SocketOpen  SocketObject  Read  DeviceReadBuffer  ByteArrayToString

Introduced in 2017
(11.2)