This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic