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
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
Would have to see the block of code. Could you post it?
> 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.
does the read() method return or not?
It will return -1 if eos.
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("Finish ed establishing connection") ;
socket.setSoTimeout(timeOu t * 100) ;
dout=new DataOutputStream(socket.ge tOutputStr eam());
din = new DataInputStream(socket.get InputStrea m() );
}
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(InterruptedIOExcept ion e) {
System.out.println("aaaaa" );
}
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("Finish
socket.setSoTimeout(timeOu
dout=new DataOutputStream(socket.ge
din = new DataInputStream(socket.get
}
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(InterruptedIOExcept
System.out.println("aaaaa"
}
ASKER
the try catch thing is from another class in another file
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) ;
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.
That won't compile.
> din.read(returnData) ;
You ignore the return value.
ASKER
bbb will have the value 0 even though i did not let the server respond
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[]
>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[]
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[]
>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 :)
> 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 :)
ASKER
sigh
just tried flushing, doesn't work
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.
Suspect it's probably a problem with your server code.
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 ).
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.
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.
No wonder your getting eos :)
Your code is behaving as expected.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hahaha so that why thank you very much....
glad I could help :)