Reputation: 37
im trying to make a udp server that only accept data, and not recieving back to client, and i had this problem, and i dont know what causing this bad file descriptor, here is the server code :
import threading
import socket
import logging
class Broker():
def __init__(self):
logging.info('Initializing Broker')
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind(('', 6668))
self.clients_list = []
def talkToClient(self, ip):
#logging.info("Sending 'ok' to %s", ip)
print ("Buat Thread Baru")
while True:
try:
data = self.sock.recv(1024)
if data:
# Set the response to echo back the recieved data
response = data
self.sock.send(response)
else:
raise error('Client disconnected')
except:
self.sock.close()
return False
self.sock.sendto("ok", ip)
def listen_clients(self):
while True:
msg, client = self.sock.recvfrom(1024)
self.sock.settimeout(20)
logging.info('Received data from client %s: %s', client, msg)
t = threading.Thread(target=self.talkToClient, args=(client,))
t.start()
if __name__ == '__main__':
# Make sure all log messages show up
logging.getLogger().setLevel(logging.DEBUG)
b = Broker()
b.listen_clients()
Upvotes: 0
Views: 727
Reputation: 42758
UDP is connectionless, so in your thread-code a error Client disconnected
is never possible. But for any other error (in your case probably socket.timeout
), the socket is closed. Subsequently, in your main program a bad filedescriptor
error is raised.
If you want timeout in the thread, you need to open a new socket. And also, you have to send a hello-message to the client, so he knows the new port for the one-to-one communication.
Also notice: UDP is not reliable.
Upvotes: 1