EFAULT error in accept socket call

R

Rookie

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)
..
..
..
}



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);
}
 
M

Michael Wojcik

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 is because your program calls the accept function incorrectly,
and the code within the accept function is then invoking undefined
behavior. After that anything can happen. In this case, "anything"
happens to depend on how you have arranged things in memory, but you
are merely changing the symptoms. The underlying problem remains.

Everything else is off-topic for comp.lang.c. accept is not a C
library function. It is a POSIX / SUS function, so this would be
on-topic in comp.unix.programmer, for example. It would also be
on-topic in comp.protocols.tcp-ip, where the sockets API is
frequently discussed.

OT: Your code does not initialize the third parameter to accept
before making the call. Per the definition of accept (see SUSv3 or
an equivalent source), the "addrlen" parameter (for which you are
passing the address of the clientAddrSize variable) must be set to
the size of the area pointed to by the "addr" parameter. In the
case of your program, that would be sizeof clientAddr.

There may be other errors in your code; I didn't bother checking
further.

--
Michael Wojcik (e-mail address removed)

The surface of the word "profession" is hard and rough, the inside mixed with
poison. It's this that prevents me crossing over. And what is there on the
other side? Only what people longingly refer to as "the other side".
-- Tawada Yoko (trans. Margaret Mitsutani)
 
M

Mark McIntyre

Hi,

I was writing a simple program using sockets.

Sockets is offtopic in CLC, You need to ask again in comp.unix.programmer
where sockets are topical.

(and where you are more likely to get the right answer)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top