简体   繁体   English

获取客户端 IP 地址和端口?

[英]Getting client IP address and port?

I'm writing a file sharing program and the jist of it is that there is one main server which all clients connect to.我正在编写一个文件共享程序,其要点是所有客户端都连接到一个主服务器。 However, the clients are the machines hosting the files so when one client requests a file from another, it must create a connection with it.但是,客户端是托管文件的机器,因此当一个客户端从另一个客户端请求文件时,它必须与其建立连接。 When the client initially connects with the main server I get it's IP and port (I think) with:当客户端最初与主服务器连接时,我得到它的 IP 和端口(我认为):

    int client_len = sizeof(client);
    int new_sd = accept(sd, (struct sockaddr *)&client, &client_len);

    char str[INET_ADDRSTRLEN]; 
    inet_ntop(AF_INET, &(client.sin_addr), str, INET_ADDRSTRLEN);
    //printf("%s\n", str);
    int port = (int) ntohs(client.sin_port);        
    //printf("Port is: %d\n", (int) ntohs(client.sin_port));

The printf's give me 127.0.0.1 and 40XXX. printf 给我 127.0.0.1 和 40XXX。 The main server is using 127.0.0.1 and port 3000. So I'm pretty sure I have the correct client IP and port BUT when I try to set up a connection between 2 clients using 127.0.0.1 and 40XXX, it does not work.主服务器使用 127.0.0.1 和端口 3000。所以我很确定我有正确的客户端 IP 和端口,但是当我尝试使用 127.0.0.1 和 40XXX 在 2 个客户端之间建立连接时,它不起作用。 The connect function keeps returning an error.连接函数不断返回错误。 Here's the code trying to establish a connection between two clients:这是尝试在两个客户端之间建立连接的代码:

    if ((cd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Can't creat a socket\n");
        exit(1);
    }
    bzero((char *)&cServer, sizeof(struct sockaddr_in));
    cServer.sin_family = AF_INET;
    cServer.sin_port = htons(cPort);
    if (inet_pton(AF_INET, cAddress, &cServer.sin_addr) == -1) {
        printf("inet_pton error occured\n");
        exit(1);
    } 

    /* Connecting to the server */
    if (connect(cd, (struct sockaddr *)&cServer, sizeof(cServer)) == -1) {
        fprintf(stderr, "Can't connect to server\n");
        exit(1);
    }

cPort is an int while cAddress is a char array. cPort 是一个 int 而 cAddress 是一个字符数组。

Here's the code from the client attempting to create a connection for incoming connections.这是客户端尝试为传入连接创建连接的代码。 The code to get the IP Address and port return 0.0.0.0 and 0 respectively.获取 IP 地址和端口的代码分别返回 0.0.0.0 和 0。

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Can't creat a socket\n");
    exit(1);
}

/* Bind an address to the socket    */
bzero((char *)&clientServer, sizeof(struct sockaddr_in));
clientServer.sin_family = AF_INET;
clientServer.sin_port = 0;
clientServer.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr *)&clientServer, sizeof(clientServer)) == -1){
    fprintf(stderr, "Can't bind name to socket\n");
    exit(1);
}

char str[INET_ADDRSTRLEN]; 
inet_ntop(AF_INET, &(clientServer.sin_addr), str, INET_ADDRSTRLEN);
printf("%s\n", str);
int clientServerPort = (int) ntohs(clientServer.sin_port);      
printf("Port is: %d\n", (int) ntohs(client.sin_port));

/* queue up to 10 connect requests  */
listen(sockfd, 10);

All help is greatly appreciated!非常感谢所有帮助!

You can't use the client port for a new connection.您不能将客户端端口用于新连接。 That port is only being used to connect to your server, it's not listening for incoming connections.该端口仅用于连接到您的服务器,它不侦听传入连接。 The client needs to bind another port, listen on it, and send that port number to you.客户端需要绑定另一个端口,侦听它,然后将该端口号发送给您。 You can then send it to another client, and it can connect to the port.然后您可以将它发送到另一个客户端,它可以连接到该端口。

Note also that this may not work at all if the first client is behind a NAT router.另请注意,如果第一个客户端位于 NAT 路由器后面,则这可能根本不起作用。 The router won't know that it should forward this new port to the client.路由器不会知道它应该将此新端口转发给客户端。 Peer-to-peer applications with dynamic port numbers are impractical when NAT is involved.当涉及 NAT 时,具有动态端口号的对等应用程序是不切实际的。 Instead, you should define a dedicated port for this purpose;相反,您应该为此目的定义一个专用端口; then users can configure port forwarding on their routers for this port.然后用户可以在他们的路由器上为这个端口配置端口转发。

That's what I initially tried.这就是我最初尝试的。 Using the same code above to get the IP Address and port number after a bind but it returned 0.0.0.0 and 0. I updated my main post.使用上面相同的代码在绑定后获取 IP 地址和端口号,但它返回 0.0.0.0 和 0。我更新了我的主要帖子。 Also would a NAT router affect me even if I'm sticking strictly to local IP's?即使我严格遵守本地 IP,NAT 路由器也会影响我吗?

Multiple interface issue.多接口问题。 Do ifconfig -a, find the right interface.做ifconfig -a,找到合适的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM