Skip to main content
Skip table of contents

server_socket_read(sock, length=-1, timeout=-1)

Features

This function reads data from the client.

Parameters

Parameter NameData TypeDefault ValueDescription

sock

socket.socket

-

Socket instance returned from server_socket_open()

socket instance

length

int

-1

Number of bytes of the received data

  • -1 : Not specified (The number of bytes to read is not specified.)
  • n(>=0) : The specified number of bytes is read.

timeout

int

float

-1

Waiting time for receipt

  • -1 : Indefinite wait
  • n(>0) : n seconds

Return

Value (res, rx_data)

Description

res

0

Number of bytes of the received data

-1

The client is not connected.

-2

socket.error occurred during data reception

-3

Timeout during data reception

rx_data

Received data (byte type)

Example

PY
sock = server_socket_open(20002)
 
res, rx_data = server_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 = server_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 = server_socket_read(sock, length=64)   # Reads 64 bytes of the received data.
 
res, rx_data = server_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".

server_socket_close(sock)
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.