restart broken tcp connection in windows using winsock 2 lib <p>am working on a c program that should log data from a norma 5000, but it seams like the normas tcpconnection randomly closes after an arbitary time (0.2-6h) but i need to log longer than that. </p> <p>when it Closes the Connection i can just restart program and it will continue to log. so i got the idea of just restarting my socket when the tcpconnection breaks. but my restart dosent work every time.. the function i Think you want to look at is shutdownCon, init and main.</p> <p>my question is: what am i doing wrong?</p> <p>winsock errors i get when the tcp Connection stops working 10053 (WSAECONNABORTED) or 10054 (WSAECONNRESET).</p> <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx</a></p> <pre><code>#pragma comment(lib, "Ws2_32.lib") #define WIN 1 #define debug 1 #include &lt;winsock2.h&gt; #include &lt;windows.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/timeb.h&gt; #include &lt;time.h&gt; #define HOST "192.168.0.101" #define PORT 23 #define SOCKET_HANDLE_FMT_PFX "" #define SOCKET_HANDLE_FMT "u" /* "%u" - 'typedef u_int SOCKET' */ typedef SOCKET socket_handle_t; typedef struct { socket_handle_t h; } *socket_t; /**************************************************************************** **/ static void Delay(double seconds) { Sleep((DWORD)(seconds * 1000)); } /**************************************************************************** **/ void wsa_error(char *func,int error) { fprintf(stderr,"%s() failed, error %d\n", func,error == -1 ? WSAGetLastError() : error); int c; /* must be int to hold EOF */ while((c = getchar()) != '\n' &amp;&amp; c != EOF); } /**************************************************************************** **/ int socket_setup(void) { #if WIN WSADATA wsaData; int wsaerrno; /* * Initialize Windows Socket DLL */ if ( (wsaerrno = WSAStartup( MAKEWORD(1,1), /* at least version 1.1 */ &amp;wsaData)) != 0 ) { wsa_error("WSAStartup",wsaerrno); return -1; } #endif /* WIN */ return 0; /* OK */ } /**************************************************************************** **/ int socket_cleanup(void) { #if WIN if ( WSACleanup() == SOCKET_ERROR ) wsa_error("WSACleanup",-1); #endif return 0; /* OK */ } /**************************************************************************** **/ socket_t socket_create(void) { socket_handle_t sh; socket_t s; sh = socket(AF_INET,SOCK_STREAM,0); #if WIN if ( sh == INVALID_SOCKET ) { wsa_error("socket",-1); return NULL; } #endif s = calloc(1,sizeof(*s)); if ( !s ) return NULL; s-&gt;h = sh; return s; /* OK */ } /**************************************************************************** **/ int socket_connect(socket_t s,struct sockaddr *addr,int addrlen) { int ret = 0; /* OK */ #if WIN if ( connect(s-&gt;h,addr,addrlen) == SOCKET_ERROR ) { wsa_error("connect",-1); return -1; } #endif return 0; /* OK */ } /**************************************************************************** **/ int socket_recv(socket_t s,void *buf,int len,int flags) { register int l; #if WIN l = recv(s-&gt;h,buf,len,flags); if ( l == SOCKET_ERROR ) { wsa_error("recv",-1); return -1; } #endif return l; } /**************************************************************************** **/ int socket_send(socket_t s,void *buf,int len,int flags) { register int slen; /* sent length */ #if WIN slen = send(s-&gt;h,buf,len,flags); if ( slen == SOCKET_ERROR ) { wsa_error("send",-1); return -1; } #endif return slen; } /**************************************************************************** **/ int socket_puts(socket_t s,char *str) { char buf[1024]; strcpy(buf,str); strcat(buf,"\n"); if ( socket_send(s,buf,strlen(buf),0) &lt; 0 ) return -1; return 0; } /**************************************************************************** **/ int socket_gets(socket_t s,char *str) { char buf[1024]; char *p; if ( socket_recv(s,buf,sizeof(buf),0) &lt; 0 ) return -1; if ( (p = memchr(buf,'\n',sizeof(buf))) != NULL ) { if ( p &gt; buf &amp;&amp; p[-1] == '\r' ) p--; *p = '\0'; } else buf[sizeof(buf)-1] = '\0'; strcpy(str,buf); return strlen(str); } /**************************************************************************** **/ /*is some thing wrong here?*/ int shutdownCon(socket_t s){ char buff[1024]; if (shutdown(s-&gt;h,2)== SOCKET_ERROR) { wsa_error("shutdownCon",-1); return -1; } while(socket_gets(s,buff)&gt; 1); if(closesocket(s-&gt;h) == SOCKET_ERROR) { wsa_error("shutdownCon",-1); return -1; } socket_cleanup(); return 0; } /*is some thing wrong here?*/ int init(socket_t *s){ struct sockaddr_in saddr; struct sockaddr_in *addr_in = (struct sockaddr_in *)&amp;saddr; /* socket (TCP/IP) API initialization: */ if ( socket_setup() &lt; 0 ) return -1; /* * Connect to the instrument: */ /* set destination IP address and TCP port: */ memset(addr_in,0,sizeof(struct sockaddr_in)); addr_in-&gt;sin_family = AF_INET; addr_in-&gt;sin_port = htons(PORT); addr_in-&gt;sin_addr.s_addr = inet_addr(HOST); /* create socket: */ *s = socket_create(); if ( !*s ) return -1; #if debug fprintf(stderr,"socket_connect() ...\n"); #endif if ( socket_connect(*s,(struct sockaddr *)&amp;saddr,sizeof(saddr)) &lt; 0 ) return 1; #if debug fprintf(stderr,"socket_connect(): done\n"); #endif return 1; } int recon(socket_t *s){ shutdownCon(*s); init(s); return 0; } void printTime(){ struct _timeb timebuffer; char *timeline; _ftime( &amp;timebuffer ); timeline = ctime( &amp; ( timebuffer.time ) ); printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &amp;timeline[20] ); } int main(int argc,char *argv[]) { socket_t s; char buffer[1024]; char oper[1024]; init(&amp;s); #if debug fprintf(stderr,"trying to get id from norma \n"); if ( socket_puts(s,"*IDN?") &lt; 0 ) return 1; if ( socket_gets(s,buffer) &lt; 0 ) return 1; puts(buffer); #endif //##################CONF FROM FLUKE##################### /* Bring the instrument into a default state: */ //socket_puts(s,"*RST"); /* Select three wattmeter configuration: */ //socket_puts(s,"ROUT:SYST \"3W\""); /* SYNC source = voltage phase 1: */ //socket_puts(s,"SYNC:SOUR VOLT1"); /* Set voltage range on voltage channel 1 to 300 V: */ //socket_puts(s,"VOLT1:RANG 300.0"); /* Set current channel 1 to autorange: */ //socket_puts(s,"CURR1:RANG:AUTO ON"); /* Set averaging time to 1 second: */ //socket_puts(s,"APER 1.0"); /* Select U, I, P measurement: */ //socket_puts(s,"FUNC \"VOLT1\",\"CURR1\",\"POW1:ACT\""); /* Run continuous measurements: */ //socket_puts(s,"INIT:CONT ON"); //###################################################### socket_puts(s,"SYST:KLOC REM"); socket_puts(s,"*RST"); socket_puts(s,"ROUT:SYST \"3w\""); socket_puts(s,"APER 0.025"); socket_puts(s,"INP1:COUP DC"); socket_puts(s,"FUNC \"VOLT1\",\"CURR1\",\"POW1\",\"POW1:APP\",\"POW1:ACT\",\"PHASE1\""); socket_puts(s,"INIT:CONT ON"); Delay(5.0); /* Wait 2 seconds */ int opstat = -1; while(1){ //if(opstat != -1) // recon(&amp;s); opstat = 0; while((opstat &amp; 0x400) == 0){ if(socket_puts(s,"STAT:OPER:EVEN?")==-1){ recon(&amp;s); continue; } memset(oper,0,sizeof(oper)); if(socket_gets(s,oper) == -1){ recon(&amp;s); continue; } opstat = atoi(oper); } if(socket_puts(s,"DATA?") == -1){/* Query the measurement */ recon(&amp;s); continue; } memset(buffer,0,sizeof(buffer)); /* Clear buffer */ if(socket_gets(s,buffer) == -1){/* read values */ recon(&amp;s); continue; } puts(buffer); /* Print the value on the screen */ //Delay(1.0); /* Wait 2 seconds */ printTime(); } return 0; } /**************************************************************************** **/ </code></pre> <p>best regards baot</p>