wood burning stoves*
The moose likes Sockets and Internet Protocols and the fly likes Client Failed to connect, how do I attempt a reconnect? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client Failed to connect, how do I attempt a reconnect?" Watch "Client Failed to connect, how do I attempt a reconnect?" New topic
Author

Client Failed to connect, how do I attempt a reconnect?

James Koehler
Greenhorn

Joined: Aug 28, 2007
Posts: 6
I am very new to java and I am creating a portable network class. Everything works fine except one thing, if the client fails to connect to the server, say it is not up yet, how do I attempt another connection with a socket?
do
if say socket = new socket(IP, Port);
loop until timeout retries reached or connected

Here is the code
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I haven't fully read your code, but if you want to reconnect to a host, you can do something like this:


One more thing, if the socket fails to connect, the socket object will be null. So when you check whether the socket is connected or not (socket.isConnected()), it will throw a NullPointerException.

Hope this helps.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
James Koehler
Greenhorn

Joined: Aug 28, 2007
Posts: 6
No that does not help but thank you for helping. The problem is that when I try to reuse the socket over and over again it just skips over it after the first failed attempt. I would like to be able to attempt reconnects without having to restart the application.
James Koehler
Greenhorn

Joined: Aug 28, 2007
Posts: 6
Ok I see what was wrong now. It would appear that the socket was just initializing the first time and took a few seconds to start and if it failed to connect, the next time you tried to reconnect it would only look for > a second before moving on so a while loop was necessary. Thank you for your help and I will be back I am sure once I start getting into the hard parts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client Failed to connect, how do I attempt a reconnect?
 
Similar Threads
File Transfer via Sockets
how to detect connection status
help with java. lang. NullPointer Exception
not able to connect...
Creating a Pipe that connects 2 sockets