nat and tcp

one on zhiihiu, speaking with tcp nat principles, well written: penetrating means of principles and development actual combat

stackoverflow上一篇:How does the socket API accept() function work?

How does accept work? How is it implemented? There's a lot of confusion on this topic. Many people claim accept opens a new port and you communicate with the client through it. But this obviously isn't true, as no new port is opened. You actually can communicate through the same port with different clients, but how? When several threads call recv on the same port, how does the data know where to go?

Here is the answer High praise:

 

Your confusion lies in thinking that a socket is identified by Server IP : Server Port. When in actuality, sockets are uniquely identified by a quartet of information:

Client IP : Client Port and Server IP : Server Port

So while the Server IP and Server Port are constant in all accepted connections, the client side information is what allows it to keep track of where everything is going.

Example to clarify things:

Say we have a server at 192.168.1.1:80 and two clients, 10.0.0.1 and 10.0.0.2.

10.0.0.1 opens a connection on local port 1234 and connects to the server. Now the server has one socket identified as follows:

10.0.0.1:1234 - 192.168.1.1:80  

Now 10.0.0.2 opens a connection on local port 5678 and connects to the server. Now the server has two sockets identified as follows:

10.0.0.1:1234 - 192.168.1.1:80  
10.0.0.2:5678 - 192.168.1.1:80

Zhihu on another answer:

A1 : Only the public IP network can be accessed on the Internet user and the server's private IP can not be accessed by Internet users assume that the company's public IP = 1.1.1.1, server IP = 10.0.0.1, port mapping will produce the following static entries:

 

Upon receiving a NAT device purpose IP + port number 1.1.1.1:443 packets will be converted to 10.0.0.1:443, and the converted IP packets continue to be forwarded to the server.

Link: https: //www.zhihu.com/question/270396590/answer/38229019

Guess you like

Origin www.cnblogs.com/xiang-yin/p/12111673.html
NAT