我的标准套接字在 Ubuntu 20.04.4 上不再起作用 - 未找到 <netinet.h>。这会导致 sockaddr 的定义错误。
s.c:22:14: error: ‘struct sockaddr’ has no member named ‘sin_family’; did you mean ‘sa_family’?
22 | serverAddr.sin_family = AF_INET;
| ^~~~~~~~~~
| sa_family
例如
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet.h>
#include <unistd.h>
int main()
{
int sockfd, clientfd;
struct sockaddr serverAddr, cliAddr;
socklen_t addr_size;
int len = sizeof(cliAddr);
char serverMessage[256] = "Hello from server\n";
sockfd = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(9002);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
// listen
if(listen(sockfd,5)==0)
clientfd = accept(sockfd, (struct sockaddr *) &cliAddr, &len);
send(clientfd,serverMessage,sizeof(serverMessage),0);
close(sockfd);
return 0;
}
(取自https://www.prodevelopertutorial.com/linux-system-programming-creating-tcp-sockets/)
但实际上任何标准的伯克利套接字代码都可以。这似乎是一个非常灾难性的问题。
如何在 Ubuntu 20.04 上替换 netinet.h?