The Java Socket
class has two methods isClosed
and isConnected
to check whether the socket is closed or connected respectively. I wanted to know what's the difference between a TCP socket which is only open and a TCP socket which is open and connected, and how is this different from UDP.
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.
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.
Sockets are useful for both stand-alone and network applications. Sockets allow you to exchange information between processes on the same machine or across a network, distribute work to the most efficient machine, and they easily allow access to centralized data.
To put things simply, a Socket
that is open is a socket that is either waiting for connection or has successfully connected with another Socket
. When a socket has been closed, it means that this socket is no longer available for connection, and that it's resources has already been released. A Socket
that is connected, well, it means that the socket is connected to another Socket
.
So a Socket
can..
UPDATE
from @Bryan
Apparently, there are half-closed or half-open states for TCP Sockets; which usage (today) is different from its original meaning. More on this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With