Whats the difference between a socket which is open and a socket which is connected?

后端 未结 2 1566
粉色の甜心
粉色の甜心 2020-12-19 00:09

The Java Socket class has two methods isClosed and isConnected to check whether the socket is closed or connected respectively. I want

2条回答
  •  醉话见心
    2020-12-19 00:59

    This page gives a fairly good overview on socket states: http://diranieh.com/SOCKETS/SocketStates.htm and the difference between TCP and UDP sockets. Particularly:

    • State "Open" (TCP and UDP): An unnamed socket has been created. An unnamed socket is one that is not bound to a local address and port
    • State "Connected" (TCP only): An association (virtual circuit) has been established between a local and remote host. Sending and receiving data is now possible.

    Note that newer implementations of java.net.DatagramSocket support an extension to the TCP/IP network states: A DatagramSocket can also be in the state "connected", so that isConnected() does not necessarily return false even though a datagram socket is never "connected" at the network layer. In particular: "When a DatagramSocket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected."

    For more information, see the sources and the JavaDoc.

提交回复
热议问题