delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/03/25/17:14:08

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: "Peter Stephens" <ptfoof AT sbcglobal DOT net>
To: <cygwin AT cygwin DOT com>
Subject: RE: recv and errno during a connection reset/closed by peer
Date: Fri, 25 Mar 2005 17:18:40 -0500
Message-ID: <!~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAAUKQItienSEKG+9226yKd5cKAAAAQAAAAdHONqHuj7UCiZ/oSiFrXiwEAAAAA@sbcglobal.net>
MIME-Version: 1.0

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019