本文介绍了为客户端 tcp 套接字绑定端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于为客户端 tcp 套接字绑定本地端口"的问题.
代码如下:

I have a problem about 'binging a local port for a client tcp socket'.
The code is as below:

void tcpv4_cli_connect(const char *srvhost, in_port_t srvport,
                       const char *clihost, in_port_t cliport)
{
    struct sockaddr_in srvaddr, cliaddr;
    struct in_addr     inaddr;
    int sockfd;

    bzero(&srvaddr, sizeof(srvaddr));
    inet_aton(srvhost, &inaddr);
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_addr   = inaddr;
    srvaddr.sin_port   = htons(srvport);

    bzero(&cliaddr, sizeof(cliaddr));
    inet_aton(clihost, &inaddr);
    cliaddr.sin_family = AF_INET;
    cliaddr.sin_addr   = inaddr;
    cliaddr.sin_port   = htons(cliport);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));

    if (connect(sockfd, (struct sockaddr *) &srvaddr, sizeof(srvaddr)) != 0)
        perror("Something Wrong");
    return;
}


int main(int argc, char *argv[])
{
    // Wrong for "220.181.111.86", but ok for "127.0.0.1"
    tcpv4_cli_connect("220.181.111.86", 80, "127.0.0.1", 40888);
    return 0;
}

当我在主函数中执行 tcpv4_cli_connect("220.181.111.86", 80, "127.0.0.1", 40888) 时,(220.181.111.86 是 Internet 上的地址),会显示错误up:出了点问题:无效的论点.

When I do tcpv4_cli_connect("220.181.111.86", 80, "127.0.0.1", 40888) in main function, (220.181.111.86 is an address on Internet), an error will show up: Something Wrong: Invalid argument.

如果我在代码中注释 bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)),一切都会好起来的,客户端套接字使用随机端口.

And if I comment bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) in the code, things will be fine and a random port is used for client socket.

但是当我做 tcpv4_cli_connect("127.0.0.1", 80, "127.0.0.1", 40888) 时,无论是否将端口绑定到客户端套接字都可以.

But it is alright when I do tcpv4_cli_connect("127.0.0.1", 80, "127.0.0.1", 40888) whether or not binding a port to client socket.

无效参数对于连接操作意味着什么?请问是不是只允许绑定一个特定的端口让客户端连接到本地地址?客户端只能使用随机端口连接到外部服务器?
我有什么误解吗?

What does Invalid argument mean for a connect operation? I wonder if it is only allowed to bind a specific port for the client to connect to local address? Clients can only use random port to connect to a external server?
Is there someting I misunderstood?

/br

推荐答案

当你 bind()127.0.0.1 (INADDR_LOOPBACK),您绑定到一个无法访问外部世界,只能访问其自身的环回接口,因此您不能connect() 到除 127.0.0.1 之外的任何 IP.如果您想在 connect() 到外部服务器时 bind() 到本地接口,则必须绑定到连接到的接口的实际 IP可以访问该服务器的网络.

When you bind() to 127.0.0.1 (INADDR_LOOPBACK), you are binding to a loopback interface that does not have access to the outside world, only to itself, so you cannot connect() to any IP other than 127.0.0.1. If you want to bind() to a local interface when you connect() to an outside server, you have to bind to the actual IP of an interface that is connected to a network that can reach that server.

如果您只想将 bind() 绑定到特定端口,但允许操作系统为您选择合适的接口,则绑定到 0.0.0.0(INADDR_ANY) 代替.

If all you want to do is bind() to a specific port, but allow the OS to pick an appropriate interface for you, then bind to 0.0.0.0 (INADDR_ANY) instead.

这篇关于为客户端 tcp 套接字绑定端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:24