delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/02/08/13:35:13

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/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: "J. J. Ekstrom" <jekstrom AT ekstrom DOT us>
To: cygwin AT cygwin DOT com
Subject: accept() seg faults using non-global address for buffer or socket structure
Date: Fri, 7 Feb 2003 22:34:54 -0700
Message-ID: <000c01c2cf34$053566b0$1801a8c0@EkstromDell>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106


I've looked at every mailing list and FAQ for limitations on the Cygwin
accept() I can't find this one.
The following program illustrates the problem, simply moving the struct
sockaddr_in sin, and char buf definition lines to be local variable of
the main will cause the accept to fail with a "bad address" error.  It
seg faults in the library under gdb.  connect works fine on local
variables.  Is this a bug or did I miss something?  The program simple
echos what the client types back to it.
The problem still exists on the update I downloaded from cygwin.com
today.
------------------------------------------------------------------------
-------------

/**************************************************************/
/* Program echoserver.c                                       */
/****************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define SERVER_PORT 8085
#define MAX_PENDING 5
#define MAX_LINE 256

struct sockaddr_in sin; // making these local to main causes a "bad
address" return from the perror after the accept!
char buf[MAX_LINE];   //  It gets a seg fault somewhere in the library.

int main()
{
    int len;
    int s, new_s;

  /* build address data structure */
  bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = INADDR_ANY;
       sin.sin_port = htons(SERVER_PORT);

  /* setup passive open */
  if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
      {
           perror("Simplex-talk: socket");
          exit (1);
      }
    if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0)
        {
            perror("Simplex - talk bind");
            exit(1);
        }
    listen(s, MAX_PENDING);
    printf("Echo is listening on port %u\n",SERVER_PORT);

   /* wait for connection, then receive, print, and send text message
back to client*/
        while (1)
         {
          if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0)
                {
                  perror("simplex-talk: accept");
                  exit(1);
                }
          printf("Server connected %x\n", new_s);
          send(new_s,"------- Echo Server ----- \n",29,0);
        while( (len = recv(new_s, buf, sizeof(buf), 0)) >0)
        {       printf("got:<%s>\n",buf);
                send(new_s, buf, len,0);
        }
      close(new_s);
      printf("Server connection %d closed\n",new_s);
     }
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.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