Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

ServerSocket & multiple clients

Thread view: 
Gargoyle - 03 Aug 2006 10:40 GMT
Need help please.

I am trying to write a small client-server application that uses a
ServerSocket that allows connection of two clients via two ports (one on each
port, say 3000 & 4000).

I've tried different techniques, but always get the first client working but
the second client waiting until after the first client has ended (see code
snippet below).

How should I approach this (without the use of threads)?

(I am using the loopback address of "127.0.0.1" as both clients are on the
same PC)

 private ServerSocket ss1;
 private ServerSocket ss2;
 private Socket socket1;
 private Socket socket2;

   try
   {
     ss1 = new ServerSocket(3000);
     ss2 = new ServerSocket(4000);
   }

and then

       socket1 = ss1.accept();
       socket2 = ss2.accept();
Gordon Beaton - 03 Aug 2006 11:54 GMT
> I am trying to write a small client-server application that uses a
> ServerSocket that allows connection of two clients via two ports
> (one on each port, say 3000 & 4000).

It might be worth knowing that *one* ServerSocket listening on *one*
port can handle *many* clients. You will get a unique Socket for each
connected client. Unless your clients are accessing completely
different service on the server, a single ServerSocket is the normal
way of doing this.

> I've tried different techniques, but always get the first client
> working but the second client waiting until after the first client
> has ended (see code snippet below).

Either use a separate thread for each ServerSocket, or use
Selector.select() to tell you when one of the ServerSockets has an
incoming connection that needs to be accepted.

Once you're able to accept() multiple clients, you still have the
problem of actually communicating with each of them, but the same two
solutions apply.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.