ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
}
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.