establish状态,本地ip和端口连接本地ip端口可能是一样的。

Posted workharder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了establish状态,本地ip和端口连接本地ip端口可能是一样的。相关的知识,希望对你有一定的参考价值。

How can you have a TCP connection back to the same port?

A TCP connection is uniquely identified by this tuple (local address, local port #, foreign address, foreign port #). There is no requirement that local address and foreign address, or even that the port numbers be different (though that would be exceedingly strange). But there is at most 1 TCP connection that has the same values for a given tuple.

When a computer connects to itself, it‘s local address and foreign address are almost always the same. After all, the ‘local‘ side and ‘foreign‘ side are actually the same computer. In fact, when this happens your computer should be showing two connections that have the same ‘local‘ and ‘foreign‘ addresses, but reversed port numbers. For example:

$ ssh localhost

will result in two connections that look something like this:

$ netstat -nA inet | fgrep :22
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address        State      
tcp        0      0 127.0.0.1:56039      127.0.0.1:22           ESTABLISHED 
tcp        0      0 127.0.0.1:22         127.0.0.1:56039        ESTABLISHED 

As you can see, the local address and foreign addresses are the same, but the port numbers are reversed. The unique tuple for this TCP connection is (127.0.0.1, 56039, 127.0.0.1, 22). There will be no other TCP connection that has these same four fields.

The fact you see two is because your computer is both ends of the connection. Each end has its own view of which one is ‘foreign‘ and which is ‘local‘.

You can even connect to yourself on the same port, and while this is not a common occurrence, it is not forbidden by the spec. Here is a sample program in Python which will do this:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((‘127.0.0.1‘, 56443))
s.connect((‘127.0.0.1‘, 56443))
time.sleep(30)

This code works because one way in which it‘s possible to open a TCP connection is to have the other side of the connection try to open one with you simultaneously. This is known as simultaneous SYN exchange, and the linked to StackOverflow answer describes what that‘s about.

以上是关于establish状态,本地ip和端口连接本地ip端口可能是一样的。的主要内容,如果未能解决你的问题,请参考以下文章

如何查看本地电脑ip

Linux中网卡相关命令以及SSH连接远程主机

301 网络基础:ip地址,域名,DNS服务器,端口,本地hosts

拼通IP,但是拼不通端口

mysql 不指定 ip 的连接默认都是 localhost

LTM_本地流量管理