Two Laptop Bag*
The moose likes Sockets and Internet Protocols and the fly likes ServerSocket vs Socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "ServerSocket vs Socket" Watch "ServerSocket vs Socket" New topic
Author

ServerSocket vs Socket

Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
I am unable to understand the difference. ServerSocket is used to create a server that can accept and send responses to a client. And client which is made by Socket, requests the server. But the client can also respond since it has both input and output streams.
In a case, where i want to make 10 such units - which can receive and send responses to each other -> i can make them all clients or all servers, isnt? That way server and client behave the same.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Gaurav Wadhwani wrote:I am unable to understand the difference. ServerSocket is used to create a server that can accept and send responses to a client. And client which is made by Socket, requests the server. But the client can also respond since it has both input and output streams.
In a case, where i want to make 10 such units - which can receive and send responses to each other -> i can make them all clients or all servers, isnt? That way server and client behave the same.



It's what happens *before* the plumbing is setup. The server socket sits on a port to accept connection from clients. The client sockets makes the connection requests to the server.

If all you have are servers, then you have everybody waiting for connections -- with no connections actually being made. If all you have are clients, then you have everybody trying to connect -- with nobody actually accepting the connections.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68

If all you have are servers, then you have everybody waiting for connections -- with no connections actually being made. If all you have are clients, then you have everybody trying to connect -- with nobody actually accepting the connections.


So, does it mean that servers cant request and clients cant accept?

If that is so, then how do i model my problem where there are 10 units which must communicate with each other. If i have to make a server and client for each unit that would be not efficient.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Gaurav Wadhwani wrote:
So, does it mean that servers cant request and clients cant accept?


Well, that is how TCP works. Server sockets accept, and client socket initiate the connections. If you want something else, Java also supports UDP, over either unicast or multicast.


Gaurav Wadhwani wrote:
If that is so, then how do i model my problem where there are 10 units which must communicate with each other. If i have to make a server and client for each unit that would be not efficient.


These are server sockets and client sockets -- not full blown server / client as defined in the application sense. Don't know what you mean by "not efficient" -- as that would imply that there is another way to do TCP.

Henry
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
Oh ok.Thank you Henry that makes things clear nd laso gives me idea about solving the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServerSocket vs Socket