Discussion:
Socket EFAULT on Accept()
(too old to reply)
c***@yahoo.com
2005-04-19 20:11:44 UTC
Permalink
This is my first attempt with sockets & TCP/IP.

I'm trying to compile some example code I
read on LinuxMag.com's Compile Time.
http://www.linux-mag.com/content/view/881/2082/

I'm having particular trouble with the server code.

The accept() function call is returning
with errno=14 (EFAULT) Bad Address.

Sockfd & addr are both used previously in bind()
and listen() which return without error.

I do not know enough to diagnose the problem.
Any help would be much appreciated.

Chad


#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

int main
(
void
)

{
int sockfd;
struct sockaddr_in addr;
int client;
int addr_len;
char buffer[10];
int i;

sockfd = socket(PF_INET, SOCK_STREAM, 0);

memset(&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_port = htons(9999);
addr.sin_addr.s_addr = INADDR_ANY;

if (bind(sockfd, (struct sockaddr *) &addr, sizeof (struct
sockaddr)) < 0 )
perror("bind");

if (listen(sockfd, 10) < 0)
perror("listen");

client = accept(sockfd, (struct sockaddr *) &addr, &addr_len);
if (client < 0) {
perror("accept");
printf("errno = %d\n", errno);
}

read(client, buffer, 9);
buffer[9] = '\0';
printf("server received %s from client\n", buffer);

write(client, buffer, 9);

close(client);
close(sockfd);

return EXIT_SUCCESS;
}
Eric Sosman
2005-04-19 20:27:01 UTC
Permalink
Post by c***@yahoo.com
This is my first attempt with sockets & TCP/IP.
I'm trying to compile some example code I
read on LinuxMag.com's Compile Time.
http://www.linux-mag.com/content/view/881/2082/
I'm having particular trouble with the server code.
The accept() function call is returning
with errno=14 (EFAULT) Bad Address.
[...]
client = accept(sockfd, (struct sockaddr *) &addr, &addr_len);
`addr_len' has not been initialized; it holds "random
garbage." Remember: it's a "value/result" parameter, not
just an output.
--
***@sun.com
c***@yahoo.com
2005-04-19 21:16:22 UTC
Permalink
How shameful. Thank You.

Chad

Loading...