How to stop a socket when it is connecting to remote?  
Author Message
JTL.zheng





PostPosted: 2007-5-16 23:04:00 Top

java-programmer, How to stop a socket when it is connecting to remote? in one thread it's:
Global.socket_control = new
Socket(InetAddress.getByName("192.168.1.22"), 1234);

when the IP:192.168.1.22 does not exist, it will take so much time to
wait before throw a "time out exception"
how can I stop this waiting in another thread?

I have try "Global.socket_control.close()"
but it doesn't work because Global.socket_control is null

Thank you very much in advance

 
SadRed





PostPosted: 2007-5-17 7:01:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? On May 17, 12:04 am, "JTL.zheng" <email***@***.com> wrote:
> in one thread it's:
> Global.socket_control = new
> Socket(InetAddress.getByName("192.168.1.22"), 1234);
>
> when the IP:192.168.1.22 does not exist, it will take so much time to
> wait before throw a "time out exception"
> how can I stop this waiting in another thread?
>
> I have try "Global.socket_control.close()"
> but it doesn't work because Global.socket_control is null
>
> Thank you very much in advance

Use connect() timeout instead of using a full constructor.

 
SadRed





PostPosted: 2007-5-17 11:20:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? On May 17, 12:04 am, "JTL.zheng" <email***@***.com> wrote:
> in one thread it's:
> Global.socket_control = new
> Socket(InetAddress.getByName("192.168.1.22"), 1234);
>
> when the IP:192.168.1.22 does not exist, it will take so much time to
> wait before throw a "time out exception"
> how can I stop this waiting in another thread?
>
> I have try "Global.socket_control.close()"
> but it doesn't work because Global.socket_control is null
>
> Thank you very much in advance

Use no-arg Socket constructor and connect() with explicit timeout arg.

 
 
JTL.zheng





PostPosted: 2007-5-17 23:38:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? thank you very much

but is it other way to stop it?
I mean long time "time out" maybe useful when it's a long distance(is
it?)

 
 
Esmond Pitt





PostPosted: 2007-5-18 9:08:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? JTL.zheng wrote:
> but is it other way to stop it?
> I mean long time "time out" maybe useful when it's a long distance(is
> it?)

So if you don't want to use a timeout how are you going to decide when
you want to stop it?
 
 
Mark Space





PostPosted: 2007-5-18 12:10:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? Esmond Pitt wrote:
> JTL.zheng wrote:
>> but is it other way to stop it?
>> I mean long time "time out" maybe useful when it's a long distance(is
>> it?)
>
> So if you don't want to use a timeout how are you going to decide when
> you want to stop it?

Maybe the user presses the Cancel button? (I.e., some non-timer
external event...)
 
 
JTL.zheng





PostPosted: 2007-5-18 17:57:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? On May 18, 12:10 pm, Mark Space <email***@***.com> wrote:
> Esmond Pitt wrote:
> > JTL.zheng wrote:
> >> but is it other way to stop it?
> >> I mean long time "time out" maybe useful when it's a long distance(is
> >> it?)
>
> > So if you don't want to use a timeout how are you going to decide when
> > you want to stop it?
>
> Maybe the user presses the Cancel button? (I.e., some non-timer
> external event...)

Yes, I want to provide a "Cancel" button to stop waiting when it's
trying to connect to remote.
how can I do with this "Cancel" button?

 
 
Esmond Pitt





PostPosted: 2007-5-19 13:53:00 Top

java-programmer >> How to stop a socket when it is connecting to remote? JTL.zheng wrote:
> Yes, I want to provide a "Cancel" button to stop waiting when it's
> trying to connect to remote.
> how can I do with this "Cancel" button?

Try

socket = new Socket();
socket.connect(...);

That way there is an existing socket object for the cancel button to
close, whereupon 'Any thread currently blocked in an I/O operation upon
this socket will throw a SocketException.'
 
 
JTL.zheng





PostPosted: 2007-5-21 23:51:00 Top

java-programmer >> How to stop a socket when it is connecting to remote?
> Try
>
> socket = new Socket();
> socket.connect(...);
>

Thank you very much
It works!
: )