Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
James Koehler
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Koehler
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic