【问题标题】:TCP connection failsTCP 连接失败
【发布时间】:2013-11-17 19:37:27
【问题描述】:

我写了服务器和客户端代码客户端可以正常连接服务器但只能在本地地址上我不知道如何通过互联网连接

int main()
{
    struct addrinfo host_info;
    struct addrinfo * host_list;
    struct sockaddr sa;
    struct sockaddr_in ip4addr;

    memset(&host_info, 0, sizeof host_info);

    host_info.ai_family = AF_INET;     // IP version not specified. Can be both.
    host_info.ai_socktype = SOCK_STREAM;
    getaddrinfo(NULL, "5555", &host_info, &host_list);
    int s;
    s=socket(host_list->ai_family,host_list->ai_socktype,host_list->ai_protocol);
    if(s==-1) printf("error\n");

    int status = connect(s, host_list->ai_addr, host_list->ai_addrlen);
    if (status == -1)  printf("connect error\n");

    char *message="Hi there!";
    send(s, message, 10, 0);
    return 0;
}

我尝试将getaddrinfo中的NULL更改为ip地址,但总是无法连接

【问题讨论】:

    标签: c++ tcp connection client


    【解决方案1】:

    您尝试通过 Internet 连接到哪个 TCP/IP 地址?

    getaddrinfo() 应该有某种 IP 地址,否则它不知道在哪里连接。这就像试图在没有地址的邮件中发送一封信。

    此外,您可能无法连接的原因有很多。您的防火墙可能正在阻止某些东西……另一端可能有防火墙正在阻止某些东西……反病毒软件甚至可能检测到它认为“奇怪”的连接并阻止它……IP地址可能错了。

    谈谈您已完成的故障排除以及您所看到的情况会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多