Reputation: 125
I searched a lot but I was not able to find a direct difference between the two. When do we use each one when it comes to creating a client socket?
Upvotes: 2
Views: 4903
Reputation: 1236
It is all in the name... A typical network socket is a connection between two ports on two machines.
The ServerSocket is the one that waits for clients to connect to it.... it 'binds' to a port, and it 'Listens' for connections, and when they happen, it 'accepts' the connection. The result of the accepted connection is a Java Socket. The client that connected (if it is also Java), also has a Java Socket. You now have two sockets connected to each other.
The Socket is described above.
Now, the address is the details about how to find/identify the remote side of the Socket connection.
A SocketAddress is the abstract class for something that can tell Java where to connect to when contacting a server, and it allows the Sockets to identify remote servers/clients once the connection is made.
An InetSocketAddress is a special SocketAddress designed to represent the standard TCP Protocol address, so it thus has methods to set/query the host name, IP address, and Socket of the remote side of the connection (or, in fact the local side too).
So, the (Inet)Socket address is used to establish Socket connections...
Summary:
ServerSocket is a listener that waits for socket connections to be established. Socket is the communication channel between two systems (one the server, the other the client). SocketAddress is an abstract class that identifies an address InetSocketAddress is a class that is specific for the TCP protocol consisting of IP Addresses/host-names, and port numbers. This is used to establish internet/TCPIP sockets.
Reference taken from https://softwareengineering.stackexchange.com/questions/231150/whats-the-difference-between-socketaddress-and-serversocket-in-java
Upvotes: 3
Reputation: 17568
An InetSocketAddress
does not manage a Socket in any way.
I think you mean Socket
vs DatagramSocket
.
Socket
is for connections communicating via TCP (reliable).
DatagramSocket
is for connections communicating via UDP (unreliable).
Or, if you're referring to SocketAddress
vs InetSocketAddress
:
SocketAddress
is simply the abstract implementation of a Socket Address with no protocol.
InetSocketAddress
is an implmentation of SocketAddress
, for IP.
Upvotes: 4
Reputation: 90
A SocketAddress is the abstract class for something that can tell Java where to connect to when contacting a server, and it allows the Sockets to identify remote servers/clients once the connection is made.
An InetSocketAddress is a special SocketAddress designed to represent the standard TCP Protocol address, so it thus has methods to set/query the host name, IP address, and Socket of the remote side of the connection (or, in fact the local side too).
Upvotes: 1
Reputation: 533820
From the Javadoc for Socket
This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.
and for InetSocketAddress
This class implements an IP Socket Address (IP address + port number)
The address is like the location of your house, the Socket is the road which leads to that house.
Upvotes: 2