Socket
Setting up a server with blocking sockets
import usocket
import _thread
import time
# Thread for handling a client
def client_thread(clientsocket,n):
# Receive maxium of 12 bytes from the client
r = clientsocket.recv(12)
# If recv() returns with 0 the other end closed the connection
if len(r) == 0:
clientsocket.close()
return
else:
# Do something wth the received data...
print("Received: {}".format(str(r)))
# Sends back some data
clientsocket.send(str(n))
# Close the socket and terminate the thread
clientsocket.close()
# Set up server socket
serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
serversocket.bind(("192.168.0.249", 6543))
# Accept maximum of 5 connections at the same time
serversocket.listen(5)
# Unique data to send back
c = 0
while True:
# Accept the connection of the clients
(clientsocket, address) = serversocket.accept()
# Start a new thread to handle the client
_thread.start_new_thread(client_thread, (clientsocket, c))
c = c+1Using a client with non-blocking sockets
Connecting to a server with non-blocking SSL wrapped socket
Last updated