Hi,
i really stuck into a problem:
I am missing in Arduino a Socket server sample which receives data from a TCP-Server.
Because my implementation of a socker-server in Micropython has a blocking method “connection.recv()” .
Blocking means: stopping code and not returning when client is connected but not sending anything.
Do you have a working example?
My code @ line 124: micropython code
data = self._connection.recv(1024)
Found no working example with “select”, “poll” or “socket.setblocking(0)”.
Arduino-WiFi-Server (SocketServer) library contains no read-implementation:
Am i missing something?
regards
Juergen
/edit: read methods found in ClientContext.h . Hope these are non-blocking …
May you deliver a programming sample for a socket-server for Arduino?
Analog to ESP8266?
ClientContextSocket
The solution is really easy:
import socket
import sys
import time
def run():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (‘0.0.0.0’, 23)
print(‘starting up on {} port {}’.format(*server_address))
sock.bind(server_address)
sock.listen(0)
while (True):
print(‘waiting for a connection’)
connection, client_address = sock.accept()
print(‘connection from’, client_address)
connection.setblocking(False)
while (True):
try:
data = connection.recv(16)
print(‘received {!r}’.format(data))
if data:
print(‘sending data back to the client’)
connection.sendall(data)
else:
print(‘No data: connection closed by the client’)
break;
except Exception as e:
print('Exception: ', e)
print(‘no data from’, client_address)
time.sleep(1)
Clean up the connection.
connection.close()
print("closed. ")
run()
Would apreciate, if this sample-code would be part of the official MP-image,
to avoid that more users stumble in this “trap” !
regards,
Juergen
1 Like