This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: recv and errno during a connection reset/closed by peer


I boiled this down to nothing(see below).  I must be missing something
basic.  I tried the suggestions made so far and it never gets to:

	printf(" >>> ERRNO %i\n", errno);

I would expect that on a disconnect (I use putty in telnet or raw mode) it
would return -1 whether it is doing MSG_PEEK or an actual retrieval.  No
luck.


#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
   int lfd=-1, afd=-1, temp=0;
   char buf[20];

   lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   if (-1 == lfd)
      printf("Error at socket(): %i\n", errno);

	// joris' suggestion
#if 1
   temp = 1;
   setsockopt(lfd, SOL_SOCKET, SO_KEEPALIVE,(char *)&temp,  sizeof(temp)) ;
#endif

   sockaddr_in svc;
   svc.sin_family = AF_INET;
   svc.sin_addr.s_addr = inet_addr("192.168.1.245");
   svc.sin_port = htons(27015);

   if (-1 == bind(lfd, (struct sockaddr *) &svc, sizeof(svc)))
      printf("bind() failed.\n");

   if (-1 == listen(lfd, 1 ))
      printf("Error listening on socket.\n");

   printf("Waiting for client to connect...\n");
   while( -1 == afd ){
      afd = accept( lfd, NULL, NULL );
   }
   printf("Client connected.\n");

   int ret_val = 0;
   do{
     ret_val = recv(afd, buf, 20, MSG_PEEK);
     if(0 > ret_val)
       printf(" >>> ERRNO %i\n", errno);
     else if(ret_val > 0){
       ret_val = recv(afd, buf, 20, 0);
       buf[ret_val]='\0';
       printf("(%i) > %s", ret_val, buf);
     }

     usleep(250);
   }while(ret_val>=0);

   return 0;
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]