Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

limiting an URL connection

Thread view: 
Marcelo - 14 Nov 2005 19:55 GMT
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();
Oliver Wong - 14 Nov 2005 20:25 GMT
> Dear programmers,
>
[quoted text clipped - 21 lines]
> //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
Marcelo - 14 Nov 2005 20:39 GMT
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
mgungora@gmail.com - 14 Nov 2005 21:22 GMT
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
Roedy Green - 15 Nov 2005 07:03 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 15 Nov 2005 15:25 GMT
>>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
[quoted text clipped - 9 lines]
> 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
Chris Uppal - 15 Nov 2005 16:41 GMT
> 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
Roedy Green - 15 Nov 2005 19:13 GMT
On Tue, 15 Nov 2005 16:41:52 -0000, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-THIS.org> wrote, quoted or indirectly
quoted someone who said :

>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.