How to stop a socket when it is connecting to remote?

J

JTL.zheng

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
 
S

SadRed

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

SadRed

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

JTL.zheng

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?)
 
E

Esmond Pitt

JTL.zheng said:
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?
 
M

Mark Space

Esmond said:
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...)
 
J

JTL.zheng

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?
 
E

Esmond Pitt

JTL.zheng said:
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.'
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top