接受阻塞套接字上的连接
希望接受网络连接(充当服务器)的 AC 程序应首先创建一个绑定到地址“INADDR_ANY”的套接字,并在其上调用 listen
。然后,它可以调用服务器套接字上的 accept
来阻止,直到客户端连接。
//Create the server socket
int servsock = socket(AF_INET, SOCK_STREAM, 0);
if(servsock < 0) perror("Failed to create a socket");
int enable = 1;
setsockopt(servsock, SOL_SOCKET, SO_REUSEADDR, (char*)&enable, sizeof(int));
//Bind to "any" address with a specific port to listen on that port
int port = 12345;
sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if(bind(servsock, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
perror("Error binding to socket");
listen(servsock, 5);
//Accept a client
struct sockaddr_storage client_addr_info;
socklen_t len = sizeof client_addr_info;
int clientsock = accept(servsock, (struct sockaddr*)&client_addr_info, &len);
//Now you can call read, write, etc. on the client socket
传递给 accept
的 sockaddr_storage
结构可用于检索有关连接的客户端的信息。例如,以下是如何确定客户端的 IP 地址:
char client_ip_str[INET6_ADDRSTRLEN + 1];
if(client_addr_info.ss_family == AF_INET) {
// Client has an IPv4 address
struct sockaddr_in *s = (struct sockaddr_in *)&client_addr_info;
inet_ntop(AF_INET, &s->sin_addr, client_ip_str, sizeof(client_ip_str));
} else { // AF_INET6
// Client has an IPv6 address
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&client_addr_info;
inet_ntop(AF_INET6, &s->sin6_addr, client_ip_str, sizeof(client_ip_str));
}