|
Home > Archive > Unix Programming > April 2005 > Socket EFAULT on Accept()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Socket EFAULT on Accept()
|
|
| ccdrbrg@yahoo.com 2005-04-19, 6:06 pm |
| 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, 6:06 pm |
|
ccdrbrg@yahoo.com wrote:
> 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.
--
Eric.Sosman@sun.com
| |
| ccdrbrg@yahoo.com 2005-04-19, 6:06 pm |
| How shameful. Thank You.
Chad
|
|
|
|
|