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