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

red floyd

Rookie said:
Hi,

[redacted]
You're OT here, you might try comp.unix.programmer
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)

You *are* setting serverAddr before this bind() call, arent' you?
.

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

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

Again, you *are* setting clientAddrSize to sizeof(clientAddr) before the
call to accept, aren't you?

But as I said, you're OT here.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top