• Post Reply Bookmark Topic Watch Topic
  • New Topic

client hangs while communicating with server...  RSS feed

 
mihir maniar
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a chat server development. i am facing a problem with that. the problem is that if a second client calls this server to get login then the client hangs. the first client that talks to the server logs in and eveything is fine. if second one tries to talk then it causes a problem. i haven't made the client class implementrunnable or extend thread. On my server side i have a server class that creates a new object of a threadclass(my custom class name and not the one provided by java) everytime it receives a request. these 2 classes do not either implement runnable or extend thread. but i have a while loop in thread class to talk to client . the problem that i face are like these.

1.> while(true)
{

while((temp=bfrc.readLine())!=null)
{

}
}

if i use this loop in the thread class then what happens is my server doesn't listen to second client that requests for login but listens to the first one and the conversation with the first client is absolutely fine.

2.> while((temp=bfrc.readLine())!=null)
{

}

if i use just use this loop then server terminates the talking with first client the moment he is logged in. then even if the first client send messages to the server to show in the chat room even then it doesn't listen because that while loop terminates after the logging in of the client. secondly this server then listens to many clients after this happens. so i do not what to do about this. the bfrc object that u see here is the bufferedreader object. i get the inputstream object from the socket.i get the inputstreamreader reader object from inputstream and then i get the buffered reader object from there.

i think using printwriter or printreader objects would do the trick coz it suspends the while loop that i am using here till the moment some data comes from client. and so i think that the server would be free to talk to other clients.
i hope this clarifies my problem. thnx in advance.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mihir maniar:
On my server side i have a server class that creates a new object of a threadclass(my custom class name and not the one provided by java) everytime it receives a request. these 2 classes do not either implement runnable or extend thread.


Naming a class "threadclass" is not the same as creating a new thread of execution. Have a look at the Java Tutorial on
Threads for the basics.
 
mihir maniar
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that i do not have a class that extends or implements a Thread class or Runnable respectively but do you think that i need to have a class that implements runnable or extends thread do achieve my goal
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to interact with more than one client at a time you will either need to use multiple threads or the nonblocking IO in the java.nio library.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!