468,785 Members | 1,536 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

EFAULT error in accept socket call

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,noOfB ytesReadOneRead;
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);
}
Nov 14 '05 #1
2 4726

In article <cj**********@gist.usc.edu>, "Rookie" <do***********@rediffmail.com> writes:

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 mi************@microfocus.com

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)
Nov 14 '05 #2
On Sun, 26 Sep 2004 19:49:27 -0700, in comp.lang.c , "Rookie"
<do***********@rediffmail.com> wrote:
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)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rune | last post: by
2 posts views Thread by Christopher J. Bottaro | last post: by
1 post views Thread by Rookie | last post: by
9 posts views Thread by Henrik | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.