Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Making objects wait/threads return objects

  Asked By: Adelisa    Date: Sep 30    Category: Java    Views: 953
  

I'm writing a client/server application and at one point the client
program has to connect to the server and read in data to create an
object. The problem is that when it tries to read in the data it gets
an EOFException. I think the reason for this is that the server has
not yet sent the data because it has not yet gotten to that stage in
it's code. So I have two choices:
(a) Make the Connection object wait. I tried using this but kept
getting IllegalMoritorState exceptions.
(b) Have Connection as a subclass of Thread and tell it to sleep
until enough data is available. The problem here is that
Thread.start() calls "public void run()", and I need it to return an
object once it has read in the data.

Does anyone have any ideas as to how to get around this?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Sam Anderson     Answered On: Sep 30

Let me start  by giving you the simplest answer and see if that
helps... try { ENTER YOUR sock = new ServerSocket, ect, HERE }catch
(EOFException e) ... although I find it odd to return  an
EOFException, more likely an IOException, for failure to connect  to
the client... but then again I'm only speculating... hope this does
not insult... but I've got to start somewhere...

 
Answer #2    Answered By: Mehreen Malik     Answered On: Sep 30

The problem  is that it ALWAYS throws an EOF Exception, as the client
is trying to read  in the data  before the server  has a chance to send
it (the client recovers quicker after the initial handshake than the
server who has to find the data within a database, construct a packet
and send it out to the client.) I want the client to wait  until a
certain amount of data is available before continuing (or timeout if
the data doesn't present itself within 30 seconds).

Surely this is a common problem when writing  client/server
applications? Has anyone else had any experience with this problem?

As an aside, in a previous program (that used threads) I used the
code:
while(in.available() < 5)
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
}
}
...which worked fine. However my current program does not use
threads, and I would have difficulty changing it to do so.
Plus the main method would then end up just calling:
thread.start();
thread.join();
which seems a bit wasteful. There must be a more elegant solution?

 
Didn't find what you were looking for? Find more on Making objects wait/threads return objects Or get search suggestion and latest updates.




Tagged: