Python socket error while receiving UDP data. (10054)

I am currently having a problem using the UDP module and Python. We have a server and clients. The problem occurs when we send data to the user. Perhaps the user could close his connection to the server using a client failure, disable ISP, or some other incorrect method. Thus, you can send data to a private socket.

Of course, with UDP, you cannot determine if the data has really been reached or closed because it does not bother (at least, this does not raise an exception). However, if you send data and it is closed, you somehow return the data (???), which ultimately leads to a socket error on sock.recvfrom. [Errno 10054] An existing connection was forcibly closed by the remote host. Almost like an automatic response from a connection.

Although this is normal and can be handled with try: except: block (even if it slightly reduces server performance). The problem is that I can’t say from whom this comes from, or from the fact that the socket is closed. Is there anyway to find out who (ip, socket #) sent this? That would be great, as I could just turn them off and remove them from the data. Any suggestions? Thank.

Server:

import socket

class Server(object):
    def __init__(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.connected = {}

    def connect(self):
        self.socket.bind(('127.0.0.1', 5579))

    def find_data(self):
        while 1:
            data, address = self.socket.recvfrom(1024)
            self.got_data(data,address)
            if self.connected.has_key(address):
                pass
            else:
                self.connected[address] = None

    def got_data(self, data, address):
        print "GOT",data,"FROM",address
        for people in self.connected:
            print people
            self.send_data('hi', people)

    def send_data(self, data, address):
        self.socket.sendto(data,address)


if __name__ == '__main__':
    server = Server()
    server.connect()
    print "NOW SEARCHING FOR DATA"
    server.find_data()

Customer:

import socket, time

class Client(object):
    def __init__(self):
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    def connect(self):
        self.socket.connect(('127.0.0.1', 5579))

    def send_data(self):
        self.socket.sendto('hi',('127.0.0.1', 5579))

    def got_data(self, data, address):
        print "GOT",data,"FROM",address


if __name__ == '__main__':
    client = Client()
    client.connect()
    while 1:
        client.send_data()
        time.sleep(5)
+3
source share
3 answers

Firstly, it is perhaps a specific platform, and you are not talking about the platform on which you work; however, 10054 WSAECONNRESET, so I assume some kind of Windows platform.

-, , UDP. Connect() , Send(), SendTo(), , , Send() , , Connect().

-, , WSAECONNRESET, ERROR_PORT_UNREACHABLE; , , . UDP , , ICMP Port Unreachable, , , . , , , , , reset error...

ICMP- , Winsock, UDP Recv/RecvFrom. , UDP- , , , , - . , Windows Vista, UDP - , , .

, ; ICMP- , , , UDP , . , , .

, - , , UDP ( ). , UDP, , , - , , , - - "" , .

, ; . , . , , "" Recv() RecvFrom(), python, , .

+8

, . socket.recv(), socket.recvfrom() - , .

+2

, .

  • UDP , Client.connect
  • Server.connected dict. , , .

A proper connection to the network is difficult because the library is sockettoo low-level (this is a thin shell around C sockets). A lot of detail has been left in your code. I suggest trying a higher level library like twisted . Here is an example in UDP to get you started.

+1
source

Source: https://habr.com/ru/post/1739814/


All Articles