Definition
client_socket_read(sock, length=-1, timeout=-1)
Features
This function receives data from the server.
Parameters
|
Parameter Name |
Data Type |
Default Value |
Description |
|---|---|---|---|
|
sock |
socket.socket |
- |
Socket instance returned from client_socket_open() |
|
length |
int |
-1 |
Number of bytes of the received data
|
|
timeout |
int float |
-1 |
Waiting time for receipt
|
Return
|
Value (res, rx_data) |
Description |
|
|
res |
>0 |
Number of bytes of the received data |
|
-1 |
The server is not connected. |
|
|
-2 |
Socket.error occurred during data reception |
|
|
-3 |
Timeout during data reception |
|
|
rx_data |
Received data (byte type) |
|
Exception
|
Exception |
Description |
|---|---|
|
DR_Error (DR_ERROR_TYPE) |
Parameter data type error occurred |
|
DR_Error (DR_ERROR_VALUE) |
Parameter value is invalid |
Example
sock = client_socket_open("192.168.137.200", 20002)
res, rx_data = client_socket_read(sock) # Indefinite wait until the data is received
# Reads all received data since the length is omitted.
# Waits indefinitely until the data is received since timeout is omitted.
# (res = size of received data, rx_data=received data) is returned when the data is received.
res, rx_data = client_socket_read(sock, timeout=3) # Waits for up to 3 seconds until the data is received.
# (res = size of received data, rx_data=received data) is returned if the data is received within 3 seconds.
# (res = -3, rx_data=None) is returned if the data is not received within 3 seconds.
res, rx_data = client_socket_read(sock, length=64) # Reads 64 bytes of the received data.
res, rx_data = client_socket_read(sock, length=64, timeout=3)
# Reads 64 bytes of the received data within the 3-second timeout.
rx_msg = rx_data.decode() # rx_data is a byte type and can be converted to a string type
# using decode().
# For example, if rx_data = b"abcd",
# rx_msg= "abcd".
client_socket_close(sock)