LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2009, 01:30 AM   #1
nilu511
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
socket programming


Hi all,
I have to run a client and server socket program in Java. When the server is up the client gets connected and my application works fine. In some condition when the server is down, the client needs to connect then it fails. For that scenario I need to run a daemon that tries to connect to the server for about 3 times before it finally throws an exception.

How should i proceed with this?
 
Old 08-28-2009, 03:39 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Why a daemon; can't the client itself attempt three times ?
 
Old 08-28-2009, 11:08 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by nilu511 View Post
Hi all,
I have to run a client and server socket program in Java. When the server is up the client gets connected and my application works fine. In some condition when the server is down, the client needs to connect then it fails. For that scenario I need to run a daemon that tries to connect to the server for about 3 times before it finally throws an exception.

How should i proceed with this?
As noted earlier, why do you require a daemon? Just have the client attempt to connect to the server. If it cannot connect after N-tries, then the client should exit gracefully.

Consider something like:
Code:
Socket sock = new Socket();
InetSocketAddress addr = new InetSocketAddress(host, port);

for (int i = 0; i < 3; ++i) {
   try {
      sock.connect(addr);
      break;
   }
   catch (IOException e) {
      // error
   }
}

if (sock.isConnected()) {
   // ok
}
else {
   // error
}
 
Old 09-01-2009, 12:41 AM   #4
nilu511
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you for the help and i was able to solve the problem.
 
Old 09-05-2009, 08:40 AM   #5
nilu511
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
I had a small doubt again. The solution that is suggested does not hold good in case when the client is running and the server goes down. Instead of exiting, the client should check three times whether the server has come up.
Pls suggest.
 
Old 09-05-2009, 11:39 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'm not up on the specifics of sockets in Java, but I know how they work in general. What I'd do is have a pair of loops like this (based on dwhitney67's post):
Code:
Socket sock = new Socket();
InetSocketAddress addr = new InetSocketAddress(host, port);

while (!sock.isConnected()) {
   for (int i = 0; i < 3;) {
      try {
         sock.connect(addr);
         break;
      }
      catch (IOException e) {
         if (++i < 3) Thread.sleep(5000);
         continue;
      }
   }

   if (!sock.isConnected()) {
      break;
   }

   while (/*read or write socket*/) {
      // do stuff
   }
}

try {
   sock.close();
}

catch (IOException e) {
}

//exit routines...
This way when the inner while loop exits, the big loop will attempt to reconnect if the socket is no longer connected. Otherwise, it's assumed that the loop exit was proactive and that the client has chosen to terminate the connection. This will happen as many times as the server disconnects the client and it can reconnect within 3 tries.
Kevin Barry

Last edited by ta0kira; 09-05-2009 at 11:46 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket Programming jawadhashmi Programming 4 05-24-2005 03:04 AM
Socket programming maldini1010 Linux - Networking 2 01-27-2005 12:17 PM
socket programming???? harbir Linux - Networking 2 07-05-2004 02:52 AM
Socket Programming cxel91a Programming 4 03-19-2003 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration