Two Laptop Bag*
The moose likes Sockets and Internet Protocols and the fly likes Can we use two ServerSocket in a single program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Can we use two ServerSocket in a single program?" Watch "Can we use two ServerSocket in a single program?" New topic
Author

Can we use two ServerSocket in a single program?

vinoth sathyamoorthy
Greenhorn

Joined: Mar 20, 2009
Posts: 2
Hi friends,



iam writing a simple client server.in my program i use two client and one server.

in client1 i used port as 1999, and i want to send name and one id number.
in client2 i used port number as 2999,and i want to send phone number and address.

in server side ServerSocket obj=new ServerSocket(3999);
ServerSocket obj=new ServerSocket(4999);
above coding cause no output.

how can serversocket used when two different client are coming?
which test condition to use handle?

please help me.









Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41597
    
  55
Using more than one ServerSocket is possible, provided they listen on different ports. But that makes sense only if they do different things, not if it's the same code listening.

The client doesn't get to specify the port it's running on, it only gets to specify the server port it wants to connect to. So I don't understand what you mean by those ports 1999 and 2999.

You may want to work through the relevant Java Tutorial chapter: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html


Ping & DNS - my free Android networking tools app
vinoth sathyamoorthy
Greenhorn

Joined: Mar 20, 2009
Posts: 2

sorry, that two port numbers(1999 and 2999) are serversocket(1999) and serversocket(2999) listening on the port numbers.

how to check which client socket is coming in server side. if more than one client socket are coming how to handle?

how to test these condition using if or any other?

what methods are available.

Thanks.
Vinoth
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41597
    
  55
As I said, the client socket is meaningless to the server. The client doesn't get to choose which socket is used. I really think you should work through the tutorial I linked to; it explains how a ServerSocket can handle multiple clients.
 
 
subject: Can we use two ServerSocket in a single program?