?
Solved

timeout problem

Posted on 2003-03-05
21
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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
Course of the Month9 days, 15 hours left to enroll

762 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