You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地127.0.0.1可运行的C语言Socket服务,公网IP无法连通求助

公网环境下C语言Socket客户端与服务端无法连通问题

我编写的C语言Socket服务端与客户端程序,在本地使用127.0.0.1测试时可正常运行,但切换为公网IP后,无论我或朋友作为服务端都无法连通。我们怀疑是防火墙问题,但已确认防火墙处于关闭状态——我通过sudo ufw status检查,朋友使用sudo netstat -ntlp验证。

客户端代码(client.c)

// client.c
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

int main() {
    int port = 8080;
    const char *ip = "127.0.0.1"; // 测试时已改为实际公网IP

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        return 2;
    }
    struct sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(port);
    inet_pton(AF_INET, ip, &hint.sin_addr);
    int connResult = connect(sock, (struct sockaddr *)&hint, sizeof(hint));
    if (connResult == -1) {
        int status = shutdown(sock, SHUT_RDWR);
        if (status == 0) {
            status = close(sock);
        }
        return status;
    }

    while (1) {
        const char *msg = "ping";
        int sendResult = send(sock, msg, strlen(msg) + 1, 0);
        if (sendResult == -1)
        {
            return 3;
        }
        usleep(1000 * 2000);
    }
    /* 不可达代码 */
    int status = shutdown(sock, SHUT_RDWR);
    if (status == 0) {
        status = close(sock);
    }
    return status;
}

服务端代码(server.c)

// server.c
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define BUFF_SIZE 4096

int main() {
    int port = 8080;

    int status;
    while (1) {
        int listening = socket(AF_INET, SOCK_STREAM, 0);
        if (listening == -1) {
            return 1;
        }
        struct sockaddr_in hint;
        hint.sin_family = AF_INET;
        hint.sin_port = htons(port);
        hint.sin_addr.s_addr = INADDR_ANY;
        if (bind(listening, (struct sockaddr *)&hint, sizeof(hint)) == -1){
            return 1;
        }
        listen(listening, SOMAXCONN);

        struct sockaddr_in client;
        unsigned int clientSize = sizeof(client);
        int clientSocket =
            accept(listening, (struct sockaddr *)&client, &clientSize);

        int status = 0;
        status = shutdown(listening, SHUT_RDWR);
        if (status == 0) {
            status = close(listening);
        }

        char buff[BUFF_SIZE];
        while (1) {
            memset(buff, 0, BUFF_SIZE);

            int bytesReceived = recv(clientSocket, buff, BUFF_SIZE, 0);
            if (bytesReceived == -1) {
                return 2;
            }
            if (bytesReceived > BUFF_SIZE) {
                return 3;
            }
            if (bytesReceived > 0) {
                printf("客户端发送:%s\n", buff);
            } else {
                break;
            }
            usleep(1000 * 1000);
        }
        status = shutdown(clientSocket, SHUT_RDWR);
        if (status == 0) {
            status = close(clientSocket);
        }
    }
    return 0;
}

编译命令

gcc server.c -o server
gcc client.c -o client

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:05:56