EFAULT error in accept socket call

EFAULT error in accept socket call

Post by Rooki » Tue, 28 Sep 2004 11:52:49



Hi,

I was writing a simple program using sockets. The program is supposed to be
a TCP server that receives a string sent by the client. For this purpose I
defined a char array called readString which was initially defined as
readString[6]. The program worked fine. I then increased the array size to
readString[100] and the accept socket call started giving an error
(errno=14;EFAULT). This gave me the impression that the clientAddr structure
was causing this problem, so I declared it as a global variable(see
below).Now the code works fine. Can someone tell me why I am getting this
error? Your help would be greatly appreciated. Thanks. The following is a
brief outline of the code:

struct sockaddr_in clientAddr; //Declaring this here works when I increase
readString from readString[6] to readString[100]
int main()
{
  char readString[100],*tempPtr;
  struct sockaddr_in serverAddr;//,clientAddr; /*Declaring clientAddr here
does not work when I increase readString from readString[6] to
readString[100]. It gives an EFAULT error for accept. This works fine for
readString[6]*/
.
.
.
  if((serverSockFd=socket(AF_INET,SOCK_STREAM,0))<0)
.
.
.
  if(bind(serverSockFd,(struct sockaddr*)&serverAddr, sizeof(serverAddr))<0)
.
.
.

  if(listen(serverSockFd,5)<0)
.
.
.

  if((clientSockFd=accept(serverSockFd,(struct
sockaddr*)&clientAddr,&clientAddrSize))<0)
.
.
.

Quote:}

Here is the full text of the code, in case the above does not suffice:

//struct sockaddr_in clientAddr;
int main()
{
  int serverSockFd,clientSockFd,noOfBytesReadTotal,noOfBytesReadOneRead;
  char readString[100],*tempPtr;
  socklen_t clientAddrSize;
  struct sockaddr_in serverAddr;
  struct sockaddr_in clientAddr;

  strcpy(readString,"Hello World!");
  printf("Before receiving from client readString = %s\n",readString);

  if((serverSockFd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
      printf("Socket failed\n");
      exit(1);
    }
  else
    printf("Socket succeeded.\n");

  serverAddr.sin_family=AF_INET;
  serverAddr.sin_port=htons(5002);
  serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  if(memset(serverAddr.sin_zero,'\0',8)<0)
    {
      printf("Error in memset\n");
      close(serverSockFd);
      exit(1);
    }

  if(bind(serverSockFd,(struct sockaddr*)&serverAddr, sizeof(serverAddr))<0)
    {
      printf("Bind failed\n");
      close(serverSockFd);
      exit(1);
    }
  else
    {
      printf("Bind succeeded.\n");
    }

  if(listen(serverSockFd,5)<0)
    {
      printf("Listen failed\n");
      close(serverSockFd);
      exit(1);
    }
  else
    printf("Listen succeeded.\n");

  if((clientSockFd=accept(serverSockFd,(struct
sockaddr*)&clientAddr,&clientAddrSize))<0)
    {
      printf("Accept failed\n");
      close(serverSockFd);
      extern int errno;
      printf("errno=%d\n",errno);
      exit(1);
    }
  else
    printf("Accept succeeded.\n");

 noOfBytesReadTotal=noOfBytesReadOneRead=0;
 tempPtr=readString;
 while(noOfBytesReadTotal<47)
   {
     noOfBytesReadOneRead=read(clientSockFd,tempPtr,47-noOfBytesReadTotal);
     if(noOfBytesReadOneRead<0)
       {
  printf("Error occurred in Read after reading %d
bytes\n",noOfBytesReadTotal);
  close(serverSockFd);
  close(clientSockFd);
  extern int errno;
  printf("Error no=%d\n",errno);
  exit(1);
       }
     noOfBytesReadTotal++;
     tempPtr++;
   }

 printf("After receiving from client string is %s\n",readString);

 close(serverSockFd);
 close(clientSockFd);

Quote:}

 
 
 

EFAULT error in accept socket call

Post by Doug Smit » Wed, 29 Sep 2004 02:29:54



> ... snippage ...
> defined a char array called readString which was initially defined as
> readString[6]. The program worked fine. I then increased the array size to
> readString[100] and the accept socket call started giving an error
> (errno=14;EFAULT). This gave me the impression that the clientAddr structure
> was causing this problem, so I declared it as a global variable(see
> below).Now the code works fine. Can someone tell me why I am getting this
> error? Your help would be greatly appreciated. Thanks. The following is a
> brief outline of the code:

 > ... considerable snippage ...

I would hazard a guess that you have an alignment problem.  Changing the
size of your array or moving the clientAddr struct out to global storage
sort of 'solved' the problem for you. ("Masked" the problem would be a
better choice of words.)

Maybe you could new or malloc your sockaddr_in structs -- allocating the
memory dynamically would give you properly aligned data types regardless
of what other local data you allocate.

If my theory proves out, you'll want to read a little about byte
alignment issues, especially as regards system level calls. Maybe
compiler options.  Stuff like that..

-Doug