EFAULT error in accept socket call

Discussion in 'C++' started by Rookie, Sep 27, 2004.

  1. Rookie

    Rookie Guest

    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)
    ..
    ..
    ..
    }
     
    Rookie, Sep 27, 2004
    #1
    1. Advertising

  2. Rookie

    red floyd Guest

    Re: [OT] EFAULT error in accept socket call

    Rookie wrote:
    > 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.
     
    red floyd, Sep 27, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rookie

    EFAULT error in accept socket call

    Rookie, Sep 27, 2004, in forum: C Programming
    Replies:
    2
    Views:
    689
    Mark McIntyre
    Sep 27, 2004
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,926
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    992
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    566
    Laszlo Nagy
    Feb 1, 2009
  5. Lee Skillen
    Replies:
    1
    Views:
    198
Loading...

Share This Page