Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Socket and ServerSocket?

If Socket represents client side and ServerSocket represents server side, why Socket.read reads the data from server side? I'm really confused, Can you please clarify it to me?

like image 288
sevugarajan Avatar asked Jan 05 '10 06:01

sevugarajan


People also ask

What is difference between socket & ServerSocket?

A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

What is the use of socket and ServerSocket?

The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the console until the client is connected.

What is the purpose of the ServerSocket class?

The ServerSocket class lets client programs connect with a server program. When a client connects, the server socket creates a Socket object, which the server can then use to communicate with the client. Creates a server socket that isn't bound to any port. Creates a server socket and binds it to the specified port.

What is meant by socket in server?

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.


2 Answers

(I post this answer because I always feel it's important to make the logic right.)

I suggest you take a look at the following sample.

http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

Admittedly, when carrying out TCP/IP communication, all the necessary information can be provided by the Socket class alone for the sole purpose of communication. No matter it is on server side or client side.

As you can see from the above link, server side use the following code to acquire its own Socket instance. That is, another socket is created on the same server local port and the client port pair.

enter image description here

Then, server use this Socket instance to talk to the client.

And to make the picture complete, below code snippet shows client's Socket instance.

enter image description here

So if Socket can do it all already, why do we still need the ServerSocket?

This is because of the working paradigm of communication over TCP/IP protocol.

When 2 programs talk over TCP/IP, usually one will passively listen/wait on a <IP:port> and the other one will actively connect to it.

So you can see, at this very starting phase of the communication, the 2 sides have very different behaviors. So 2 different classes are used to reflect this difference.

  • Socket class encapsulates the behavior of the active side. (a.k.a. the client)
  • ServerSocket class encapsulates the behavior of the passive side (a.k.a. the server)

Once the ServerSocket accomplished its listening task and detected an incoming connection, it will accept() it and create a new Socket instance to facilitate the communication.

Similarily, in java.nio package, you will find ServerSocketChannel and SocketChannel classes. And still, they behave like this:

ServerSocketChannel -------------> SocketChannel                       accept() 

So, to some extent, I agree with @JohnK as he pointed out in the comment, it's more or less just a 6-letter difference.

like image 89
smwikipedia Avatar answered Sep 22 '22 03:09

smwikipedia


why socket.read reads the data from serverside

Because it is reading the data sent by the server through the network, it is not reading directly the server filesystem or resouces ( db , ram or anything like that ) it is reading the data that was already processed by the ServerSocket.

Think about the Socket as your web browser and the ServerSocket as the remote webserver.

When you request an image, page, etc, the webserver ( The ServerSocket ) writes the bytes to the client, in turn the client has to read them ( to know what the webserver sent right? ) and process them by displaying them to the final user.

The same happend with ServerSocket/Socket but at a lower level. The socket reads information from the ServerSocket.

Does it make sense?

like image 28
OscarRyz Avatar answered Sep 23 '22 03:09

OscarRyz