gpt4 book ai didi

sockets - 无法连接到 VPN 中的本地服务器

转载 作者:行者123 更新时间:2023-12-04 11:14:55 24 4
gpt4 key购买 nike

我在多个 iOS、MacOS 中尝试过以下代码。
这是服务器代码

void *run_server(void *thread_id) {
int server_fd, new_socket;
struct sockaddr_in server, client;
int opt = 1;
int addrlen = sizeof(server);

// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket");
return NULL;
}

// Forcefully attaching socket to the port
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
return NULL;
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = ip("10.10.10.20"); // bind IP
server.sin_port = htons(27042);

// Forcefully attaching socket to the port
if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind");
return NULL;
}

if (listen(server_fd, 3) < 0) {
perror("listen");
return NULL;
}

printf("Server is running with %u:%d\n", server.sin_addr.s_addr, htons(server.sin_port));
while (1) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&client, (socklen_t*)&addrlen))<0) {
perror("accept");
return NULL;
}

printf("Client connected with %u:%d\n", client.sin_addr.s_addr, client.sin_port);
}
}
这是客户端代码
void scan(unsigned int ip, int port) {
int sock = 0;
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\nSocket creation error\n");
return;
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = ip;

printf("IP %u, port %d\n", ip, port);
int connected = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (connected != 0) {
char errorMsg[256];
strerror_r(errno, errorMsg, 256);
printf("Error %s\n", errorMsg);
}

if (connected < 0) {
close(sock);
return;
}
}

void *run_scan(void *thread_id) {
while (1) {
scan(ip("10.10.10.20"), 27042);
usleep(1000000);
}

return NULL;
}
和主要功能
int main(int argc, const char * argv[]) {
pthread_t thread1;
if (pthread_create(&thread1, NULL, run_server, (void *)1)) {
printf("Can't create thread run_server");
}

pthread_t thread2;
if (pthread_create(&thread2, NULL, run_scan, (void *)2)) {
printf("Can't create thread run_scan");
}

while (1) sleep(1);
return 0;
}
问题描述:
此代码与本地 IP(DHCP 分配的 IP)一起正常工作。但是,当我连接到 VPN 并将服务器 IP 绑定(bind)到 PPP 网络接口(interface) IP 时,它是 10.10.10.20(如上面的示例代码)。我无法打开与该 IP 的连接。错误是“连接超时”。请注意,另一台主机仍然可以连接到 IP 为 10.10.10.20 的服务器。
感谢有关这种情况的任何知识共享。

最佳答案

找到了答案,基本上这是Linux内核强加的限制。
更多信息:https://www.softether.org/4-docs/1-manual/B._Troubleshooting_and_Supplemental/11.1_Troubleshooting

关于sockets - 无法连接到 VPN 中的本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68077596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com