Socket udp EINPROGRESS OSError
-
Dear all,
I am developing a fw which have to send to my pc a data. O the other side there is a server udp in my PC(a script written in Pycharm).
Client:
class UDP:
def init(self):
self.wlan = WLAN(mode=WLAN.STA)
self.wlan.connect(ssid='AP_Progettazione', auth=(WLAN.WPA2, 'Progettazione2019'))
while not self.wlan.isconnected():
time.sleep(0.25)
print("Wifi .. Connecting")
print ("Wifi Connected")self.socket_list = []Set up the first socket in non-blocking mode
self.s1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.s1.setblocking(False) self.socket_list.append(self.s1) # Set up the second socket in non-blocking mode self.s2 = socket.socket() self.s2.setblocking(False) self.socket_list.append(self.s2) self.s1s = ssl.wrap_socket(self.s1) self.s2s= ssl.wrap_socket(self.s2) # Create a new poll object self.p = uselect.poll() # self.s1.settimeout(10) # self.s2.settimeout(10) # Register the sockets into the poll object, wait for all kind of events self.p.register(self.s1, uselect.POLLIN | uselect.POLLOUT | uselect.POLLHUP | uselect.POLLERR) self.p.register(self.s2, uselect.POLLIN | uselect.POLLOUT | uselect.POLLHUP | uselect.POLLERR) try: self.s2s.connect(socket.getaddrinfo("192.168.1.31", 80)[0][-1]) self.s1s.connect(socket.getaddrinfo("192.168.1.31", 80)[0][-1]) print("SOCKET CREATE") except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise # Raise all other errors def Send_datagram(self,p): while True: # Wait for any action to happen infinitely l = self.p.poll() # Start processing the actions happened for t in l: # First element of the returned tuple is the socket itself sock = t[0] # Second element of the returned tuple is the events happened event = t[1] # If any error or connection drop happened close the socket if(event & uselect.POLLERR or event & uselect.POLLHUP): sock.close() continue # If the socket is writable then send some data # The socket becomes writable if the connect() was successfull if(event & uselect.POLLOUT): # If any error occurs during sending here, do "nothing", poll() will return with error event, close the socket there try: sock.send("Data to send") # We only want to send one message on this socket, in the future wait only for new incoming messages self.p.modify(sock, uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR) except: pass # If any new data is received then get it if(event & uselect.POLLIN): # If any error occurs during receiving here, do "nothing", poll() will return with error event, close the socket there try: r = sock.recv(1) # If recv() returns with 0 the other end closed the connection if len(r) == 0: sock.close() continue else: # Do something with the received data... print("Data received: " + str(r)) except: pass def Thread(self): print("Questo è il thread che manda via udp") _thread.start_new_thread(self.Send_datagram,(self.p,))Server:
import socketlocalIP = "127.0.0.1"
#localIP ="192.168.1.31"
localPort = 80bufferSize = 1024
msgFromServer = "Hello UDP Client"
bytesToSend = str.encode(msgFromServer)
Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
Listen for incoming datagrams
while (True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)message = bytesAddressPair[0] address = bytesAddressPair[1] clientMsg = "Message from Client:{}".format(message) clientIP = "Client IP Address:{}".format(address) print(clientMsg) print(clientIP) # Sending a reply to client UDPServerSocket.sendto(bytesToSend, address)How can I resolve this?
Thanks in advance
Let me know
Francesco Pugliese
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login