Click here to Skip to main content
16,005,120 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: strange char* compile error? Pin
George_George15-Feb-09 17:02
George_George15-Feb-09 17:02 
GeneralRe: strange char* compile error? Pin
«_Superman_»15-Feb-09 18:16
professional«_Superman_»15-Feb-09 18:16 
AnswerRe: strange char* compile error? Pin
Stuart Dootson16-Feb-09 1:13
professionalStuart Dootson16-Feb-09 1:13 
QuestionHow redraw images if screen resolution changes? Pin
Supra215-Feb-09 4:09
Supra215-Feb-09 4:09 
AnswerRe: How redraw images if screen resolution changes? Pin
Alexander M.,15-Feb-09 7:34
Alexander M.,15-Feb-09 7:34 
AnswerRe: How redraw images if screen resolution changes? Pin
Hamid_RT15-Feb-09 21:52
Hamid_RT15-Feb-09 21:52 
QuestionCreate a Excel ListBox from C++ Pin
Taubi15-Feb-09 3:59
Taubi15-Feb-09 3:59 
QuestionDetecting Server (Socket) Disconnect? Pin
gvanto15-Feb-09 2:33
gvanto15-Feb-09 2:33 
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@mainboxUnsure | :~ /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@mainboxUnsure | :~ /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>

Find Your Dream IT-Job in Australia, Free!
http://www.WebCV.com.au
AnswerRe: Detecting Server (Socket) Disconnect? Pin
Stuart Dootson15-Feb-09 3:09
professionalStuart Dootson15-Feb-09 3:09 
AnswerRe: Detecting Server (Socket) Disconnect? Pin
gvanto16-Feb-09 17:26
gvanto16-Feb-09 17:26 
AnswerRe: Detecting Server (Socket) Disconnect? Pin
Eytukan15-Feb-09 5:48
Eytukan15-Feb-09 5:48 
Questionerror MIDL2020 : error generating type library : SaveAllChanges Failed : Pin
pandit8415-Feb-09 0:19
pandit8415-Feb-09 0:19 
AnswerRe: error MIDL2020 : error generating type library : SaveAllChanges Failed : Pin
Nuri Ismail15-Feb-09 0:49
Nuri Ismail15-Feb-09 0:49 
Questionfatal error C1189: #error : This add-in is for VC++ 6.0 only Pin
pandit8414-Feb-09 23:18
pandit8414-Feb-09 23:18 
AnswerRe: fatal error C1189: #error : This add-in is for VC++ 6.0 only Pin
Stuart Dootson14-Feb-09 23:25
professionalStuart Dootson14-Feb-09 23:25 
GeneralRe: fatal error C1189: #error : This add-in is for VC++ 6.0 only Pin
pandit8414-Feb-09 23:39
pandit8414-Feb-09 23:39 
GeneralRe: fatal error C1189: #error : This add-in is for VC++ 6.0 only Pin
Stuart Dootson15-Feb-09 0:39
professionalStuart Dootson15-Feb-09 0:39 
QuestionCEditView Pin
grassrootkit14-Feb-09 22:44
grassrootkit14-Feb-09 22:44 
AnswerRe: CEditView Pin
grassrootkit14-Feb-09 22:56
grassrootkit14-Feb-09 22:56 
GeneralRe: CEditView Pin
Stuart Dootson14-Feb-09 23:13
professionalStuart Dootson14-Feb-09 23:13 
GeneralRe: CEditView Pin
grassrootkit15-Feb-09 0:50
grassrootkit15-Feb-09 0:50 
GeneralRe: CEditView Pin
Stuart Dootson15-Feb-09 3:01
professionalStuart Dootson15-Feb-09 3:01 
GeneralRe: CEditView Pin
grassrootkit15-Feb-09 5:34
grassrootkit15-Feb-09 5:34 
GeneralRe: CEditView Pin
grassrootkit15-Feb-09 22:31
grassrootkit15-Feb-09 22:31 
GeneralRe: CEditView Pin
Stuart Dootson15-Feb-09 23:30
professionalStuart Dootson15-Feb-09 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.