• TCP/IP 学习


    http://pan.baidu.com/s/1bn1S4Kf

    重点学习

    6.4 TCP Socket Life Cycle

    6.5 Demultiplexing Demystified

     

    -------- from   http://stackoverflow.com/questions/1694144/can-two-applications-listen-to-the-same-port

    can two application listen to the same port?

    Yes (for TCP) you can have two programs listen on the same socket, if the programs are designed to do so. When the socket is created by the first program, make sure the SO_REUSEADDR option is set on the socket before you bind(). However, this may not be what you want. What this does is an incoming TCP connection will be directed to one of the programs, not both, so it does not duplicate the connection, it just allows two programs to service the incoming request. For example, web servers will have multiple processes all listening on port 80, and the O/S sends a new connection to the process that is ready to accept new connections.

    ------- from http://stackoverflow.com/questions/152457/what-is-the-difference-between-a-port-and-a-socket/152863#152863

    A TCP socket is an endpoint instance defined by an IP address and a port in the context of either a particular TCP connection or the listening state.

    A port is a virtualisation identifier defining a service endpoint (as distinct from a service instance endpoint aka session identifier).

    A TCP socket is not a connection, it is the endpoint of a specific connection.

    There can be concurrent connections to a service endpoint, because a connection is identified by both its local and remote endpoints, allowing traffic to be routed to a specific service instance.

    There can only be one listener socket for a given address/port combination.

    Exposition

    This was an interesting question that forced me to re-examine a number of things I thought I knew inside out. You'd think a name like "socket" would be self-explanatory: it was obviously chosen to evoke imagery of the endpoint into which you plug a network cable, there being strong functional parallels. Nevertheless, in network parlance the word "socket" carries so much baggage that a careful re-examination is necessary.

    In the broadest possible sense, a port is a point of ingress or egress. The French word porte literally means door. Ports, then, are transportation endpoints whether you ship data or big steel containers.

    For the purpose of this discussion I will limit consideration to the context of TCP-IP networks. The OSI model is all very well but has never been completely implemented, much less widely deployed in high-traffic high-stress conditions.

    The combination of an IP address and a port is strictly known as an endpoint and is sometimes called a socket. This usage originates with RFC793, the original TCP specification.

    A TCP connection is defined by two endpoints aka sockets.

    An endpoint (socket) is defined by the combination of a network address and a port identifier. Note that address/port does not completely identify a socket (more on this later).

    The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single network interface possible.

    It is the socket pair (the 4-tuple consisting of the client IP address, client port number, server IP address, and server port number) that specifies the two endpoints that uniquely identifies each TCP connection in an internet. (TCP-IP Illustrated Volume 1, W. Richard Stevens)

    In most C-derived lanaguages, TCP connections are established and manipulated using methods on an instance of a Socket class. Although it is common to operate on a higher level of abstraction, typically an instance of a NetworkStream class, this generally exposes a reference to a socket object. To the coder this socket object appears to represent the connection because the connection is created and manipulated using methods of the socket object.

    In C#, to establish a TCP connection (to an existing listener) first you create a TcpClient. If you don't specify an endpoint to the TcpClient constructor it uses defaults - one way or another the local endpoint is defined. Then you invoke the Connect method on the instance you've created. This method requires a parameter describing the other endpoint.

    All this is a bit confusing and leads you to believe that a socket is a connection, which is bollocks. I was labouring under this misapprehension until Richard Dorman asked the question.

    Having done a lot of reading and thinking, I'm now convinced that it would make a lot more sense to have a classTcpConnection with a constructor that takes two arguments, LocalEndpoint and RemoteEndpoint. You could probably support a single argument RemoteEndpoint when defaults are acceptable for the local endpoint. This is ambiguous on multihomed computers, but the ambiguity can be resolved using the routing table by selecting the interface with the shortest route to the remote endpoint.

    Clarity would be enhanced in other respects, too. A socket is not identified by the combination of IP address and port:

    [...]TCP demultiplexes incoming segments using all four values that comprise the local and foreign addresses: destination IP address, destination port number, source IP address, and source port number. TCP cannot determine which process gets an incoming segment by looking at the destination port only. Also, the only one of the [various] endpoints at [a given port number] that will receive incoming connection requests is the one in the listen state. (p255, TCP-IP Illustrated Volume 1, W. Richard Stevens)

    As you can see, it is not just possible but quite likely for a network service to have numerous sockets with the same address/port, but only one listener socket on a particular address/port combination. Use of a socket object as a proxy for a TCP connection is thus very misleading.

    References

    1. TCP-IP Illustrated Volume 1 The Protocols, W. Richard Stevens, 1994 Addison Wesley

    2. RFC793, Information Sciences Institute, University of Southern California for DARPA

  • 相关阅读:
    PHP中文字符串编码转换
    html表单样式, table隔行高亮, 长字符串自动换行
    PHP带重试功能的curl
    redis+crontab+php异步处理任务
    emoji表情初探
    iptables进行DNAT(目标地址转换)
    Linux作为路由器(一)
    nginx正向代理http(一)
    Linux Shell sort排序常用命令(转载)
    Zabbix添加自定义监控项(一)
  • 原文地址:https://www.cnblogs.com/webundle/p/3573861.html
Copyright © 2020-2023  润新知