File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Multiple client sockets sharing port number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Multiple client sockets sharing port number" Watch "Multiple client sockets sharing port number" New topic
Author

Multiple client sockets sharing port number

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 112
Hi,

one very basic question. I am starting in this field and the answer will clarify many things. Is it possible for a server to hold several client connections sharing the same port? (meaning the client socket, not the listening socket (serversocket)).
I have done some practice with some code pieces from the internet, and have seen that each time a new client connects, a new port number is assigned. I figure to think that the server might be bound to a concrete number of connections derived from its port capacity. So, then it arises the question, is it possible to use several connections in the same port number?.

At the beginning I thought Nooo.... , BUT since TCP ensures completion and that packages are received in the right order, perhaps I can build a logic in my program that allows me to separate packages corresponding to each connection.

On the other hand, if possible, how can I decide the port being shared. The serverSocket accepts the connection and creates a socket automatically without having the chance to specify a port number.

Many thanks in advance. Any help is greatly appreciated.

Carlos.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Since it's a basic question, can I ask another basic question in return? Here it is: why do you want to know? You can certainly have multiple clients connected to the same server; they all use the same server socket but they all use different client sockets. This works perfectly well in practice, so it's not obvious why you want to interfere with the process.
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 112
Thanks Paul for your quick answer,

I don't think I ever need to implement such a solution, and probably is out of my scope right now.
However in case an application hosted in a server received huges amount of requests, in order to be able to attend them a different approach should be adopted isn't it?. I am not aware right now which amount of simultaneous connections can a regular server attend, but I guess other server architechtures are more suitable when such situations arise...

This is more a question for my knowledge rather than something I must do in real life.

Many thanks again.

Carlos.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Carlos Conti wrote:However in case an application hosted in a server received huges amount of requests, in order to be able to attend them a different approach should be adopted isn't it?


No, I don't think so. Why do you say that?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple client sockets sharing port number
 
Similar Threads
TCP/Socket mechanics of "Accept()" etc.
Connection is failing at ObjectInputStream for Server
Socket-port relationship....
Socket
Creating a Application that runs both a client and a server (p2p)