This is the mail archive of the 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]

accept() seg faults using non-global address for buffer or socket structure

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

/* 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");
    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");
          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);
      printf("Server connection %d closed\n",new_s);

Unsubscribe info:
Bug reporting:

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