Link to home
Start Free TrialLog in
Avatar of Leoleo
Leoleo

asked on

timeout problem

Hi
I have this client Server program that uses the Socket class n ServerSocket class of java.net. I want the client to abort listening for the response if the timeout value is up. I have set the setSoTimeout(1500) before the read() method of DataInputStream class. It seems to be that either the Socket did not throw an exception or the read() method read the stream even though there is nothing in there. Appreciate if anyone can help me with this.

Thanks
Avatar of functionpointer
functionpointer

Would have to see the block of code. Could you post it?
Avatar of Mick Barry
> or the read() method read the stream even though there is nothing in there.

does the read() method return or not?
It will return -1 if eos.
Avatar of Leoleo

ASKER

yes e read() returns a byte[]

part of the code segement coz it is too long

 public NetworkProvider(String host, int port, int timeOut) throws IOException{

      socket = new Socket(host,port);
      System.out.println("Finished establishing connection") ;
      socket.setSoTimeout(timeOut * 100) ;

      dout=new DataOutputStream(socket.getOutputStream());
      din = new DataInputStream(socket.getInputStream() );
  }

  public byte[] send(byte[] data) throws IOException {
    System.out.println( socket.getSoTimeout() );
    byte[] returnData= new byte[1];

    dout.write(data) ;

    din.read(returnData) ;

    return returnData;
  }


try{
NetworkProvider a=new NetworkProvider(address, port, timeout);

byte bbb=new byte[1];

bbb=a.send(aaa);
}catch(InterruptedIOException e) {
System.out.println("aaaaa");
}
Avatar of Leoleo

ASKER

the try catch  thing is from another class in another file
Avatar of Leoleo

ASKER

bbb will have the value 0 even though i did not let the server respond
try adding:

dout.write(data) ;
dout.flush();

din.read(returnData) ;
> byte bbb=new byte[1];

That won't compile.

> din.read(returnData) ;

You ignore the return value.


Avatar of Leoleo

ASKER

bbb will have the value 0 even though i did not let the server respond
Avatar of Leoleo

ASKER

>> byte bbb=new byte[1];

>That won't compile.

actully it is byte[] bbb= new byte[1];

>> din.read(returnData) ;

>You ignore the return value.

actually it return value by writing into the returnData in the argument. returnData is a byte[]
Avatar of Leoleo

ASKER

>> byte bbb=new byte[1];

>That won't compile.

actully it is byte[] bbb= new byte[1];

>> din.read(returnData) ;

>You ignore the return value.

actually it return value by writing into the returnData in the argument. returnData is a byte[]
> actually it return value by writing into the returnData
> in the argument. returnData is a byte[]

The value returned from read() tells you how many bytes were read. It doesn't necessarily fill the entire buffer.
Read your javadoc :)


Avatar of Leoleo

ASKER

sigh
just tried flushing, doesn't work
i don't think your problem has anything to do with timeouts or flushing.
Suspect it's probably a problem with your server code.

Avatar of Leoleo

ASKER

ya u r right, i got -1 for the return value. But shouldn't the InterruptedIOException been thrown by the socket, or is there anyway i can do this. I need the socket to listen until timeout.
^  exactly. this works fine ( minus the typos ).
Avatar of Leoleo

ASKER

i deliberately comment out the code for the server to reply coz i wanted to test if i can catch the exception n do failover
> But shouldn't the InterruptedIOException been thrown by the socket

It is *not* timing out. You have reached the end of the stream.

Check your server code.
> i deliberately comment out the code for the server to reply

No wonder your getting eos :)

Your code is behaving as expected.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Leoleo

ASKER

hahaha so that why thank you very much....
glad I could help :)