I am trying to make a server that allows TCP, UDP and HTTP connections by making each process have its own thread. I already accomplished TCP and HTTP connections, and the UDP portion starts up correctly, but when I try to send a message via UDP, the system hangs. Does anyone know how to fix this problem?
The UDP Class Handler:
class ThreadedUDPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("{} wrote: ".format(self.client_address[0]))
print(data)
socket.sendto(data.upper(), self.client_address)
The UDP Thread:
class ThreadedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
pass
Location in code:
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "127.0.0.1", 8000
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
http_thread = threading.Thread(target=httpd.serve_forever)
print("Website serving at port", PORT)
udpserver = ThreadedUDPServer((HOST,PORT+1), ThreadedUDPRequestHandler)
udp_thread = threading.Thread(target=udpserver.serve_forever)
print("UDP serving at port", PORT+1)
tcpserver = ThreadedTCPServer((HOST, PORT-1), ThreadedTCPRequestHandler)
server_thread = threading.Thread(target=tcpserver.serve_forever)
print("TCP serving at port", PORT-1)
udp_thread.start()
Solved: I forgot to add
udp_thread.start()
I forgot to add
udp_thread.start()
at the bottom
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.