gvanto
February 15th, 2009, 02:35 PM
After following the great tutorial on here: <a href="http://tldp.org/LDP/LG/issue74/tougher.html">http://tldp.org/LDP/LG/issue74/tougher.html</a>[<a href="http://tldp.org/LDP/LG/issue74/tougher.html" target="_blank" title="New Window">^</a>]
sources in folder here: <a href="http://www.sharedigest.com/SocketClientServer.zip">http://www.sharedigest.com/SocketClientServer.zip</a>[<a href="http://www.sharedigest.com/SocketClientServer.zip" target="_blank" title="New Window">^</a>]
I've managed to get the client and server talking to each other.
This is for an setup whereby data will be transmitted to my client via telnet from a remote server, and, according to them, I need to configure the client in such a way that it checks if the server dies and automatically attempts to reconnect after some (pre-specified) time period.
Currently, when I kill the server, I get ALOT of '' received by the client, continuously ... (output shown below, main routine shown below output)
I was wondering if there's a way to detect that the connection from the server's been lost, and consequently attempt to reconnect and keep listening for incoming messages?
Help much appreciated!
gvanto
socket newbie
<code>
Server:
pacific@mainbox:~/workspace/SocketClientServer/Debug$ ./SocketClientServer 0
starting server ...
Hello server from client.
Please enter string to send: hello back to client
Please enter string to send: another message
Please enter string to send: one more
Please enter string to send: //here i kill the server using ctrl+c
Client:
pacific@mainbox:~/workspace/SocketClientServer/Debug$ ./SocketClientServer 1
starting client ...
Sending hello server msg ...
Listening ...
We received this response from the server:
"hello back to client"
Listening ...
We received this response from the server:
"another message"
Listening ...
We received this response from the server:
"one more"
Listening ...
We received this response from the server: //AFter server killed, FLOOD of incoming BLANK messages received by client
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
</code>
MAIN ROUTINE:
<code>
/*
* main.cpp
*
* Created on: 15/02/2009
* Author: pacific
*
* http://tldp.org/LDP/LG/issue74/tougher.html
*/
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "ServerSocket.h"
#include "ClientSocket.h"
#include "SocketException.h"
#define DO_MAIN 1
using namespace std;
#ifdef DO_MAIN
int main(int argc, char *argv[]) {
if(argc > 1) {
/************************************************** ***
* *********** SERVER *******************************/
if(atoi( argv[1] ) == 0) { //server mode
cout << "starting server ... " << endl;
try {
//Create the server socket
ServerSocket *server = new ServerSocket(30000); //set up socket and listen on local port
while(true) { //wait for incoming connections
/**
* Contains all of our socket information, used to exchange data with the client.
*/
ServerSocket new_sock;
server->accept(new_sock); //accept new incoming socket connection
try {
while(true) {
string data;
new_sock >> data; //read data from new_sock into 'data'
cout << data << endl;
//new_sock << data; //sends data in 'data' back throught the socket to the client (echo server)
string sendstr;
while(1) {
cout << "Please enter string to send: ";
getline (cin, sendstr);
new_sock << sendstr; //send it!
}
}
}
catch(SocketException&) {}
}
}
catch(SocketException &e) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
}
/************************************************** ***
* *********** CLIENT *******************************/
if(atoi(argv[1]) == 1 ) { //client mode
cout << "starting client ... " << endl;
try {
//Create the server socket
ClientSocket client("localhost", 30000); //set up socket and listen on local port
string reply;
try {
cout << "Sending hello server msg ... " << endl;
client << "Hello server from client. "; //send
}
catch(SocketException &) {}
while(true) { //keep listening
try {
cout << "Listening ... " << endl;
//client << "Test message. "; //send
client >> reply; //receive
}
catch(SocketException &) {}
std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
}
}
catch(SocketException &e) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
}
}
else {
cout << "usage: ./SocketClientServer 0|1 (0 == server, 1 == client)" << endl;
}
return 0;
}
#endif
</code>
sources in folder here: <a href="http://www.sharedigest.com/SocketClientServer.zip">http://www.sharedigest.com/SocketClientServer.zip</a>[<a href="http://www.sharedigest.com/SocketClientServer.zip" target="_blank" title="New Window">^</a>]
I've managed to get the client and server talking to each other.
This is for an setup whereby data will be transmitted to my client via telnet from a remote server, and, according to them, I need to configure the client in such a way that it checks if the server dies and automatically attempts to reconnect after some (pre-specified) time period.
Currently, when I kill the server, I get ALOT of '' received by the client, continuously ... (output shown below, main routine shown below output)
I was wondering if there's a way to detect that the connection from the server's been lost, and consequently attempt to reconnect and keep listening for incoming messages?
Help much appreciated!
gvanto
socket newbie
<code>
Server:
pacific@mainbox:~/workspace/SocketClientServer/Debug$ ./SocketClientServer 0
starting server ...
Hello server from client.
Please enter string to send: hello back to client
Please enter string to send: another message
Please enter string to send: one more
Please enter string to send: //here i kill the server using ctrl+c
Client:
pacific@mainbox:~/workspace/SocketClientServer/Debug$ ./SocketClientServer 1
starting client ...
Sending hello server msg ...
Listening ...
We received this response from the server:
"hello back to client"
Listening ...
We received this response from the server:
"another message"
Listening ...
We received this response from the server:
"one more"
Listening ...
We received this response from the server: //AFter server killed, FLOOD of incoming BLANK messages received by client
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
""
Listening ...
We received this response from the server:
</code>
MAIN ROUTINE:
<code>
/*
* main.cpp
*
* Created on: 15/02/2009
* Author: pacific
*
* http://tldp.org/LDP/LG/issue74/tougher.html
*/
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "ServerSocket.h"
#include "ClientSocket.h"
#include "SocketException.h"
#define DO_MAIN 1
using namespace std;
#ifdef DO_MAIN
int main(int argc, char *argv[]) {
if(argc > 1) {
/************************************************** ***
* *********** SERVER *******************************/
if(atoi( argv[1] ) == 0) { //server mode
cout << "starting server ... " << endl;
try {
//Create the server socket
ServerSocket *server = new ServerSocket(30000); //set up socket and listen on local port
while(true) { //wait for incoming connections
/**
* Contains all of our socket information, used to exchange data with the client.
*/
ServerSocket new_sock;
server->accept(new_sock); //accept new incoming socket connection
try {
while(true) {
string data;
new_sock >> data; //read data from new_sock into 'data'
cout << data << endl;
//new_sock << data; //sends data in 'data' back throught the socket to the client (echo server)
string sendstr;
while(1) {
cout << "Please enter string to send: ";
getline (cin, sendstr);
new_sock << sendstr; //send it!
}
}
}
catch(SocketException&) {}
}
}
catch(SocketException &e) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
}
/************************************************** ***
* *********** CLIENT *******************************/
if(atoi(argv[1]) == 1 ) { //client mode
cout << "starting client ... " << endl;
try {
//Create the server socket
ClientSocket client("localhost", 30000); //set up socket and listen on local port
string reply;
try {
cout << "Sending hello server msg ... " << endl;
client << "Hello server from client. "; //send
}
catch(SocketException &) {}
while(true) { //keep listening
try {
cout << "Listening ... " << endl;
//client << "Test message. "; //send
client >> reply; //receive
}
catch(SocketException &) {}
std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
}
}
catch(SocketException &e) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
}
}
else {
cout << "usage: ./SocketClientServer 0|1 (0 == server, 1 == client)" << endl;
}
return 0;
}
#endif
</code>