Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 848
Aadit M Shah Avatar asked Oct 02 '12 05:10

Aadit M Shah


People also ask

What is the difference between a socket and a connection?

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.

What is a connected socket?

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.

What happens when a socket is closed?

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.

When would you use a socket connection?

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.


1 Answers

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..

  • be open and connected at the same time.
  • be open and not connected at the same time.
  • not be connected when closed.

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.

like image 136
Russell Gutierrez Avatar answered Oct 20 '22 05:10

Russell Gutierrez