OpenBSD套接字编程
OpenBSD Socket Programming
有一个动态 IP 地址(public 我家的 IP)我想跟踪(即,将其作为我的 OpenBSD 服务器上的文件)。我想出了这个简单的服务器:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
int
main(int argc, char *argv[])
{
int socket_desc,
new_socket,
c;
struct sockaddr_in server,
client;
char *message;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(1234);
if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
return 2;
}
listen(socket_desc, 3);
for (;;) {
c = sizeof(struct sockaddr_in);
new_socket =
accept(socket_desc, (struct sockaddr *) &client,
(socklen_t *) & c);
if (new_socket < 0) {
return 3;
}
int client_port = ntohs(client.sin_port);
char *client_ip = inet_ntoa(client.sin_addr);
for (int i = 0; i < 100; ++i) {
if (client_ip[i] == 0)
break;
printf("%c", client_ip[i]);
}
printf("\n");
write(new_socket, client_ip, strlen(client_ip));
write(new_socket, "\n", 1);
}
return 0;
}
我在 Raspberry Pi(我的主计算机)运行 Raspberry Pi OS Buster(基本上是 Debian GNU/Linux Buster)上开发了它。它与 gcc(1)
和 tcc(1)
都能很好地编译。在我的 x86_64 Artix Linux 笔记本电脑上也是如此。但是,当我在 OpenBSD 上使用 cc(1)
(即 Clang)尝试它时,我收到:
iplog4.c:14:24: error: variable has incomplete type 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:15:21: error: variable has incomplete type 'struct sockaddr_in'
client;
^
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:26:30: error: use of undeclared identifier 'INADDR_ANY'
server.sin_addr.s_addr = INADDR_ANY;
^
iplog4.c:39:13: error: invalid application of 'sizeof' to an incomplete type 'struct sockaddr_in'
c = sizeof(struct sockaddr_in);
^ ~~~~~~~~~~~~~~~~~~~~
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
4 errors generated.
您需要以下包含才能编译:
#include <netinet/in.h>
有一个动态 IP 地址(public 我家的 IP)我想跟踪(即,将其作为我的 OpenBSD 服务器上的文件)。我想出了这个简单的服务器:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
int
main(int argc, char *argv[])
{
int socket_desc,
new_socket,
c;
struct sockaddr_in server,
client;
char *message;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(1234);
if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
return 2;
}
listen(socket_desc, 3);
for (;;) {
c = sizeof(struct sockaddr_in);
new_socket =
accept(socket_desc, (struct sockaddr *) &client,
(socklen_t *) & c);
if (new_socket < 0) {
return 3;
}
int client_port = ntohs(client.sin_port);
char *client_ip = inet_ntoa(client.sin_addr);
for (int i = 0; i < 100; ++i) {
if (client_ip[i] == 0)
break;
printf("%c", client_ip[i]);
}
printf("\n");
write(new_socket, client_ip, strlen(client_ip));
write(new_socket, "\n", 1);
}
return 0;
}
我在 Raspberry Pi(我的主计算机)运行 Raspberry Pi OS Buster(基本上是 Debian GNU/Linux Buster)上开发了它。它与 gcc(1)
和 tcc(1)
都能很好地编译。在我的 x86_64 Artix Linux 笔记本电脑上也是如此。但是,当我在 OpenBSD 上使用 cc(1)
(即 Clang)尝试它时,我收到:
iplog4.c:14:24: error: variable has incomplete type 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:15:21: error: variable has incomplete type 'struct sockaddr_in'
client;
^
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
iplog4.c:26:30: error: use of undeclared identifier 'INADDR_ANY'
server.sin_addr.s_addr = INADDR_ANY;
^
iplog4.c:39:13: error: invalid application of 'sizeof' to an incomplete type 'struct sockaddr_in'
c = sizeof(struct sockaddr_in);
^ ~~~~~~~~~~~~~~~~~~~~
iplog4.c:14:12: note: forward declaration of 'struct sockaddr_in'
struct sockaddr_in server,
^
4 errors generated.
您需要以下包含才能编译:
#include <netinet/in.h>