By default, all created Socket objects have the property Socket.blocking set to true
Socket.blocking read/write property
The Socket.blocking property sets or checks if the Socket is in blocking mode.
- When set to
true
, all Socket operations (such as reading and sending data) will block until they have terminated. - When set to
false
, all Socket operations will return immediately a Task object that will perform asynchronously.
Example
local net = require "net"
-- create and connect a client TCP socket to the host "google.com"
local socket = net.Socket("google.com", 80)
-- sets the Socket in blocking mode (the default)
socket.blocking = true
socket:connect()
-- socket:send() will block until all data has been sent
socket:send("GET / HTTP/1.1\r\nHost: www.google.fr\r\nConnection: Close\r\n\r\n")
-- wait until some data is available
local data = socket:recv()
-- prints received data (an http response)
print(data)