SocketReadyQ[socket]
tests if there is any data immediately available to read from the specified socket.
SocketReadyQ[socket,t]
waits for up to t seconds to see if data becomes available to read.
SocketReadyQ
SocketReadyQ[socket]
tests if there is any data immediately available to read from the specified socket.
SocketReadyQ[socket,t]
waits for up to t seconds to see if data becomes available to read.
Details
- Sockets in SocketReadyQ[…] are specified using SocketObject[…].
- SocketReadyQ returns immediately; unlike Read etc., it does not block waiting for data to become available.
- For numeric values, t is interpreted as having units of seconds. A time Quantity can also be used.
Examples
open all close allBasic Examples (3)
Initially on connection, most websites will not send data until a request is made:
sock = SocketConnect["exampledata.wolfram.com:80"]SocketReadyQ[sock]Write a request to the socket and wait for up to 10 seconds for the server's response to arrive:
WriteString[sock, "GET /50states.txt HTTP/1.0
"];
SocketReadyQ[sock, 10]Close[sock]Open a server socket and connect a client socket to it:
server = SocketOpen[Automatic, "ZMQ"]
client = SocketConnect[server]Initially, neither of them has any data available:
SocketReadyQ /@ {server, client}Sending a message to the client makes the socket ready for reading:
WriteString[server, "hello from the server"];
SocketReadyQ[client]After reading the message, there is no more data available:
SocketReadMessage[client];
SocketReadyQ[client]Close /@ {server, client}Wait for a response using a Quantity as the second argument:
sock = SocketConnect["wolfram.com:80"];
SocketReadyQ[sock, Quantity[10, "Seconds"]]Close[sock]Neat Examples (1)
Make a simple ping time function, measuring how long it takes for an HTTP response to come back:
simplePing[host_String, timeout_ : Quantity[10, "Seconds"]] := Block[{sock = SocketConnect[host], res}, WriteString[sock, "HEAD /index.html HTTP/1.1
Host: " <> host <> "
"];
res = Quantity[First@AbsoluteTiming[SocketReadyQ[sock, timeout]], "Seconds"];
Close[sock];
res]Ping a host to see how long before it responds:
simplePing["wolfram.com:80"]Request the page multiple times:
Table[simplePing["wolfram.com:80"], 5]See Also
Related Guides
History
Text
Wolfram Research (2017), SocketReadyQ, Wolfram Language function, https://reference.wolfram.com/language/ref/SocketReadyQ.html.
CMS
Wolfram Language. 2017. "SocketReadyQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SocketReadyQ.html.
APA
Wolfram Language. (2017). SocketReadyQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SocketReadyQ.html
BibTeX
@misc{reference.wolfram_2026_socketreadyq, author="Wolfram Research", title="{SocketReadyQ}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/SocketReadyQ.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_socketreadyq, organization={Wolfram Research}, title={SocketReadyQ}, year={2017}, url={https://reference.wolfram.com/language/ref/SocketReadyQ.html}, note=[Accessed: 12-June-2026]}