我正在努力理解 accept(2)
的原型(prototype)。
我有一个简单的服务器,它接受连接并向客户端返回“Hello world!\n”。系统调用 accept(2)
使用指向 struct sockaddr
的指针和指向 socklen_t
的指针来存储结构的长度。但是 sockaddr
已经有一个名为 sa_len
的字段,似乎就是为此而做的。
此外,我有这个简单的服务器(在 OSX 下编译,希望在 Linux 下也能)打印出我自己的 socklen_t
传递给 accept,然后是 sa_len
的值:它们在 OSX 上是相同的,在本例中为 28。
编辑:经过更多测试后,似乎 sa_len
不一定与指针中存储的长度相同。
为什么 accept(2)
需要长度作为一个单独的指针?
作为引用,我在下面发布了示例服务器。你可以编译:
gcc -Wall -Wextra main.c
然后运行:
./a.out
在另一个终端中,连接到它:
telnet 127.0.0.1 3000
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
#include <strings.h>
// default port if none if passed in the program arguments
#define FTP_PORT_DEFAULT ("3000")
// number of connections allow to wait for a call to accept
#define FTP_BACKLOG (5)
int main(int ac, char **av)
{
struct addrinfo hints; // hints used to get address information
struct addrinfo *sai; // server address information
int ss; // server socket
char *port;
port = (ac >= 2) ? av[1] : FTP_PORT_DEFAULT;
bzero(&hints, sizeof(hints));
// using AF_INIT6 instead of PF_INET6
// http://stackoverflow.com/questions/6729366/
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
// intended for use with `bind`
hints.ai_flags = AI_PASSIVE;
// passing `NULL` as the hostname tells `getaddrinfo` to use
// any available local IP address
if (getaddrinfo(NULL, port, &hints, &sai) != 0)
return (-1);
if ((ss =
socket(sai->ai_family, sai->ai_socktype, sai->ai_protocol))== -1)
return (-1);
if (bind(ss, sai->ai_addr, sai->ai_addrlen) == -1 ||
listen(ss, FTP_BACKLOG) == -1)
{
close(ss);
return (-1);
}
while (1)
{
int cs;
struct sockaddr csockaddr;
unsigned csockaddr_len;
bzero(&csockaddr, sizeof(csockaddr));
cs = accept(ss, &csockaddr, &csockaddr_len);
// handle "fatal" errors that I don't think I can recover from
// other than by relaunching the server
if (cs == EBADF || cs == ECONNABORTED || cs == ENOTSOCK ||
cs == EOPNOTSUPP)
{
break ;
}
printf("my len %u / internal len %u\n",
csockaddr_len, csockaddr.sa_len);
if (fork() == 0)
{
write(cs, "Hello world!\n", 13);
close(cs);
}
break;
}
close(ss);
return 0;
}
最佳答案
sa_len
不可移植。这是added to BSD only in 1988 , 4.3BSD 几年后,但它不在 POSIX 中而且它不在 Linux 中。
关于c - 为什么 `accept(2)` 需要 `sockaddr` 长度作为单独的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626805/