I am trying to write an application that communicates with a server via Sockets on windows. I need a reliable way of telling if the connection is interrupted, for example by internet connectivity issues etc. I do not mean the return of connect(), but while the socket has already been esablished.So far the only one that was supposed to work was to send a message to the server with
int issocketclosed = send(socketFd," \b",3,0);
My integer however does not change when the connection gets killed. On the server side i am using "netcat -k -lv PORT" to listen for the connection and to simulate interruption i am CTRL-C -ing netcat .
My Socket code looks something like this:
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
HANDLE s = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in connect_info;
connect_info.sin_family = AF_INET;
connect_info.sin_port = htons( port );
connect_info.sin_addr.s_addr = inet_addr(ipaddr);
connect(s,(struct sockaddr*)&connect_info,sizeof(connect_info));
while(1){
Sleep(5000); //Do Stuff with socket
int issocketclosed = send(socketFd," \b",3,0); //Check for connectin error
if(issocketclosed == -1){
connect(s,(struct sockaddr*)&connect_info,sizeof(connect_info)); // Reconnect
}
}
I know this is not the first time this question has been asked but i have tried the other ones such as getsocketopt() without success. Any help is appreciated!