?
Solved

timeout problem

Posted on 2003-03-05
21
Medium Priority
?
155 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Leoleo
  • 10
  • 8
  • 3
21 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8077332
Would have to see the block of code. Could you post it?
0
 
LVL 92

Expert Comment

by:objects
ID: 8077442
> 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.
0
 

Author Comment

by:Leoleo
ID: 8077755
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");
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Leoleo
ID: 8077774
the try catch  thing is from another class in another file
0
 

Author Comment

by:Leoleo
ID: 8077784
bbb will have the value 0 even though i did not let the server respond
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8077823
try adding:

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

din.read(returnData) ;
0
 
LVL 92

Expert Comment

by:objects
ID: 8077826
> byte bbb=new byte[1];

That won't compile.

> din.read(returnData) ;

You ignore the return value.


0
 

Author Comment

by:Leoleo
ID: 8077828
bbb will have the value 0 even though i did not let the server respond
0
 

Author Comment

by:Leoleo
ID: 8077848
>> 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[]
0
 

Author Comment

by:Leoleo
ID: 8077861
>> 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[]
0
 
LVL 92

Expert Comment

by:objects
ID: 8077879
> 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 :)


0
 

Author Comment

by:Leoleo
ID: 8077901
sigh
just tried flushing, doesn't work
0
 
LVL 92

Expert Comment

by:objects
ID: 8077945
i don't think your problem has anything to do with timeouts or flushing.
Suspect it's probably a problem with your server code.

0
 

Author Comment

by:Leoleo
ID: 8077946
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.
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8077947
^  exactly. this works fine ( minus the typos ).
0
 

Author Comment

by:Leoleo
ID: 8077951
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
0
 
LVL 92

Expert Comment

by:objects
ID: 8077972
> 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.
0
 
LVL 92

Expert Comment

by:objects
ID: 8077979
> i deliberately comment out the code for the server to reply

No wonder your getting eos :)

Your code is behaving as expected.
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 8077987
you need to keep the stream open on the server to get your timeout.
0
 

Author Comment

by:Leoleo
ID: 8078093
hahaha so that why thank you very much....
0
 
LVL 92

Expert Comment

by:objects
ID: 8078128
glad I could help :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question