i have client socket behind nat , want local port number used process.
to illustrate question, here's quick example.
let's create server using following code:
welcome_socket = socket.socket(socket.af_inet, socket.sock_stream) welcome_socket.bind(("", 1500)) welcome_socket.listen(5)
i listen incoming connections:
(client_socket, address) = self.welcome_socket.accept()
i connect client (behind nat) using following code:
sock = socket.create_connection(("server_address", 1500))
here i'm little confused.
the address on server side has public address of wifi network client connected (which expect) , port number, based on understanding of nats, should different actual port number used client , used address translation.
however, if used getsockname() function on client, same port number 1 given server.
returning example in code.
on server:
client_socket.getpeername() >>> ('wifi_address', 4551)
on client:
sock.getsockname() >>> ('local_address', 4551)
so, both port numbers same, though client behind nat. how case? misunderstanding how nat works? or there command physical address client socket bound to?
any insight appreciated.
it router using port address translation (or one-to-many nat). wiki link further quotes
pat attempts preserve original source port. if source port used, pat assigns first available port number starting beginning of appropriate port group 0-511, 512-1023, or 1024-65535. when there no more ports available , there more 1 external ip address configured, pat moves next ip address try allocate original source port again. process continues until runs out of available ports , external ip addresses.
and should reason why seeing port 4551 on server.
(this link should clarify difference between nat , pat)
Comments
Post a Comment