开发者

accept fails with EFAULT

I have a socket server running on multiple machines. It works like a charm besides on one machine.

The server binds correctly but returns an error (EFAULT) when the client attempts to connect.

Perhaps someone has an idea what the source of the problem might be. Thanks a lot in advance!

Some informatio开发者_如何转开发n about the machine: Linux version 2.6.18.3 gcc version 3.3.5 (Debian 1:3.3.5-13)

The socket server source is pretty straight forward.

...
...
struct sockaddr_in server_addr;
struct sockaddr* client;
socklen_t alen;


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
    ...
}

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);

if(bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0){
    ...
}

if(listen(sockfd,BACKLOG) == -1){
    ...
}

alen = sizeof(client);

new_fd = accept(sockfd, client, &alen);
if (new_fd == -1) {
        /*
        * this part of the code is executed
        * errno is set to 14
        */
}

Thank you for pointing me in the right direction.


Use this:

struct sockaddr_in client;
...
alen = sizeof(client);
new_fd = accept(sockfd, (struct sockaddr *) &client, &alen);

accept expects a pointer to an existing buffer which it'll fill in. You have two errors, you set alen to the size of a pointer, and you pass an uninitialized pointer to accept.


From the accept(2) man page:

  EFAULT The addr argument is not in a writable part of the user  address
          space.

Check to make sure that you've allocated client appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜