limiting an URL connection

M

Marcelo

Dear programmers,

I have some trouble while doing a connection with a java application to
a web server. I would like to limit the time of the opening connection
when there is reception bytes (the server allows connection, but there
is no information)

Actually, in my code, the application stands for ever when I ask for
information from the remote url.

How can I fix this connection problem? Is there a way to limit the time
of the first stablished connection?

thanks a lot

Marcelo

part of the code,

urlObject = new URL(url);
URLConnection con = urlObject.openConnection();

//The problem is in this line
//The application waits here un indeterminated time...
int length = con.getContentLength();
 
O

Oliver Wong

Marcelo said:
Dear programmers,

I have some trouble while doing a connection with a java application to a
web server. I would like to limit the time of the opening connection when
there is reception bytes (the server allows connection, but there is no
information)

Actually, in my code, the application stands for ever when I ask for
information from the remote url.

How can I fix this connection problem? Is there a way to limit the time of
the first stablished connection?

thanks a lot

Marcelo

part of the code,

urlObject = new URL(url);
URLConnection con = urlObject.openConnection();

//The problem is in this line
//The application waits here un indeterminated time...
int length = con.getContentLength();

I'm not very familiar with the URLConnection class, but don't you need
to call .connect() before calling .getContentLength() ?

- Oliver
 
M

Marcelo

I don't really know. The program works for the active urls, however, for
the valid but passif urls, the programs waits without any answer from
the server (and that's what i want to control).

Marcelo
 
M

mgungora

getContentLength() does an implicit connect()...

I don't think there is a built-in mechanism for time-out. You need to
write a worker thread for your "connect" method that will return
immediately when connected, or otherwise terminate after a specified
period.

If you have used a client Socket, there is a timeout property that can
be set, but URLConnection lacks that property.

Regards,
-murat
 
R

Roedy Green

I have some trouble while doing a connection with a java application to
a web server. I would like to limit the time of the opening connection
when there is reception bytes (the server allows connection, but there
is no information)

there are some network system properties you might want to have a look
at. See http://mindprod.com/jgloss/properties.html

If you can't do what you need there or with the socket methods You can
always use a timer and when it "bings" kill the socket if it has not
already succeeded. I have not done this, but IIRC there is a way to
interrupt a thread that makes it immediately abandon any I/O
operation.
 
O

Oliver Wong

Roedy Green said:
there are some network system properties you might want to have a look
at. See http://mindprod.com/jgloss/properties.html

If you can't do what you need there or with the socket methods You can
always use a timer and when it "bings" kill the socket if it has not
already succeeded. I have not done this, but IIRC there is a way to
interrupt a thread that makes it immediately abandon any I/O
operation.

From the Thread.interrupt() javadocs:

<quote>
If this thread is blocked in an invocation of the wait(), wait(long), or
wait(long, int) methods of the Object class, or of the join(), join(long),
join(long, int), sleep(long), or sleep(long, int), methods of this class,
then its interrupt status will be cleared and it will receive an
InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel
then the channel will be closed, the thread's interrupt status will be set,
and the thread will receive a ClosedByInterruptException.
</quote>

- Oliver
 
C

Chris Uppal

Oliver said:
If this thread is blocked in an I/O operation upon an interruptible
channel then the channel will be closed, the thread's interrupt status
will be set, and the thread will receive a ClosedByInterruptException.

Hmm... There's a thought. Does a server socket count as an interruptible
channel ?

A bit awkward of interupting one thread waiting for incoming connections closed
the whole socket.

I suppose I should go look it up...

-- chris
 
R

Roedy Green

Hmm... There's a thought. Does a server socket count as an interruptible
channel ?

These would pretty well have to be, otherwise how would sockets
implement their own timeouts? Let us know what you discover.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top