以下是从stackoverflow上抄下来的,很有帮助

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端口可能是一样的。的更多相关文章

  1. 配置Windows 2008 R2 防火墙允许远程访问SQL Server 2008 R2 更改端口 连接字符串 IP+逗号+端口号

      1.先修改 sql server 2008R2的端口号吧,1433经常成为别人入侵的端口,在sql server 配置管理器 -->sql server 网络配置-->MSSQLSER ...

  2. 【liunx】使用xshell连接虚拟机上的CentOS 7,使用xhell连接本地虚拟机上的Ubuntu, 获取本地虚拟机中CentOS 7的IP地址,获取本地虚拟机中Ubuntu 的IP地址,Ubuntu开启22端口

    注意,如果想用xshell去连接本地虚拟机中的linux系统,需要本地虚拟机中的系统是启动的才能连接!!!!! ============================================ ...

  3. Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

    本地内网IP和外网IP的区别: 根据我的经验一台电脑需要两个ip才可以上网,一个是本地的内网ip 一个是外网的ip 本地的ip 一般是192.168.1.2这种样子  只要在不同的路由器上可以重复 外 ...

  4. SSH的端口转发:本地转发Local Forward和远程转发Remote Forward

    关于使用ssh portforwarding来进行FQ的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下. 首先要记住一件事情就是: SSH 端口转发自然需要 SSH 连接,而 SSH ...

  5. win7 64 位操作系统,进程System,PID为4,扫描连接局域网ip地址的139和445端口

    偶然发现电脑的System进程不间断扫描连接局域网内的其它IP对应的445和139端口,这是个问题. 上网搜索,立即关闭139端口的监听. 方法如下: 本地连接属性-TCP/IP属性-高级-WINS选 ...

  6. 配置SecureCRT连接本地虚拟机中的Linux系统

    转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...

  7. 开启本地MySql数据库远程连接

    解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 33 ...

  8. XShell连接本地Ubuntu虚拟机

    VMware Workstation 安装好本地虚拟机之后,直接在虚拟机上敲命令着实不方便. 这个时候我们就需要一个远程命令工具来管理虚拟机,这里推荐使用XShell远程命令行工具 1.下载工具 直接 ...

  9. 1、win10下连接本地系统上的Linux操作系统(分别以Nat方式和桥接模式实现)

    1.win10下连接本地系统上的Linux操作系统(分别以Nat方式和桥接模式实现) 一.准备知识:win10下打开Administrator的方式 在win10操作系统中,Administrator ...

随机推荐

  1. IdentityServer4实现OAuth2.0四种模式之客户端模式

    一,准备内容 IdentityServer4 是Asp.net core的一个中间件,用于添加符合OpenId Connect和OAuth2.0规范的终端到Asp.net Core应用.在这里简单介绍 ...

  2. Spring Boot + RabbitMQ 配置参数解释

    最近生产RabbitMQ出了几次问题,所以抽时间整理了一份关于Spring Boot 整合RabbitMQ环境下的配置参数解释,通过官网文档和网上其他朋友一些文章参考归纳整理而得,有错误之处还请指正~ ...

  3. 易百教程人工智能python修正-人工智能监督学习(回归)

    回归是最重要的统计和机器学习工具之一. 我们认为机器学习的旅程从回归开始并不是错的. 它可以被定义为使我们能够根据数据做出决定的参数化技术,或者换言之,允许通过学习输入和输出变量之间的关系来基于数据做 ...

  4. 四 python中关于OOP的常用术语

    抽象/实现 抽象指对现实世界问题和实体的本质表现,行为和特征建模,建立一个相关的子集,可以用于 绘程序结构,从而实现这种模型.抽象不仅包括这种模型的数据属性,还定义了这些数据的接口. 对某种抽象的实现 ...

  5. Attribute与Property关系

    总的来说,其实是HTML Attribute 与 DOM property之间的关系. 1 什么是Property? JS DOM Object对象有property.一个property可能是不同数 ...

  6. python3基础之“函数(1)”

    1.type:查看当前字符串的类型 c=' print(type(c),c) b= int(c) print(type(b),b) num=" a=int(num,base=16) prin ...

  7. Vue的11个生命周期函数的用法

    实例的生命周期函数(官方11个):beforeCreate:在实例部分(事件/生命周期)初始化完成之后调用.created:在完成外部的注入/双向的绑定等的初始化之后调用.beforeMount:在页 ...

  8. 老毛桃制作U盘-linux

    使用老毛桃制作ubuntu启动镜像 选择ISO模式 开始制作 模拟启动 制作完成,模拟启动测试.出现如下错误: Failed to load ldlinux.c32 Boot failed: plea ...

  9. PowerDesigner 连接数据库,更新数据库;

    首先:以管理员身份运行Powerdesigner 一.连接服务器的某个数据库: 点击新建数据源图标 选择数据源类型:用户数据源:这里说是只用于当前机器,实际局域网里的都可以. Successfully ...

  10. C++(三十五) — 运算符重载

    运算符重载的实质:函数重载.除了增加一个关键字 operator 外,与函数重载没有区别,都是通过该类的某个对象来访问重载运算符. (1)重载运算符时,运算符运算顺序和优先级不变,操作数个数不变: ( ...