python - UDP client cannot talk through an external IP address -
i running udp server , client (python). when within same local network, client able talk server. when server ip address set ip address of router (which has udp port forwarding server), client not able talk server @ all. wondering if can point out why works within local network (on different machines) cannot make client connect server using external ip address of router both client , server connected.
the code client
import socket import sys host, port = "<ip address of router port forwarded server>", 5000 data = " hello client" #.join(sys.argv[1:]) # sock_dgram socket type use udp sockets sock = socket.socket(socket.af_inet, socket.sock_dgram) # can see, there no connect() call; udp has no connections. # instead, data directly sent recipient via sendto(). sock.sendto(data + "\n", (host, port)) received = sock.recv(1024) print "sent: {}".format(data) print "received: {}".format(received)
code server
import socketserver class myudphandler(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) if __name__ == "__main__": host, port = "<local ip address of server", 5000 server = socketserver.udpserver((host, port), myudphandler) server.serve_forever()
ok figured out going on.
router connected 2 computers - computer , computer b. computer can talk computer b using local network (udp server client). when computer (udp client) sends data computer b (udp server) using router ip address (external ip address) router port forwarding computer b, not working. apparently server accept connections originate outside local network when client uses external ip address
Comments
Post a Comment