zoukankan      html  css  js  c++  java
  • establish状态,本地ip和端口连接本地ip端口可能是一样的。

    以下是从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.

  • 相关阅读:
    Cornfields POJ
    二维RMQ模板
    降雨量 HYSBZ
    Frequent values UVA
    UVA
    Argus UVALive
    关于二分图有向边和无向边问题探讨
    Guardian of Decency UVALive
    SAM I AM UVA
    【062新题】OCP 12c 062出现大量新题-15
  • 原文地址:https://www.cnblogs.com/workharder/p/10206683.html
Copyright © 2011-2022 走看看