简体   繁体   English

WinSock c ++ inet_ntop始终显示204.204.204.204(并且accept()并未失败)

[英]WinSock c++ inet_ntop always displays 204.204.204.204 (and accept() didn't failed)

I'm trying to make a winsock server and I want to display the client's ip on the server when he connects but that's where there is a problem. 我正在尝试制作Winsock服务器,并且我想在客户端连接时在服务器上显示客户端的ip,但这就是问题所在。 Every time I try to connect it display 204.204.204.204. 每次尝试连接时,都会显示204.204.204.204。 I tried to connect with another computer but the result was the same. 我尝试连接另一台计算机,但结果相同。 result in localhost 导致本地主机

After this, I started looking for people having the same problem as me on this website and I found several people who had the same as me but they all had either the accept or the inet_ntop function that wasn't working correctly. 之后,我开始在这个网站上寻找与我有相同问题的人,我发现有几个与我有相同问题的人,但是他们所有人的accept或inet_ntop函数均无法正常工作。 So I check and none of this two functions return an error. 因此,我检查了这两个函数,均未返回错误。 Maybe I'm stupid but I really can't figured out what's the problem. 也许我很傻,但是我真的不知道出了什么问题。 (btw english is not my first language so please tell me if you noticed or if my english isn't too bad) (顺便说一句英语不是我的母语,所以请告诉我您是否注意到我的英语,或者我的英语还不错)

the part of the code that isn't working 代码中不起作用的部分

sockaddr_in from;
    int clientlen = sizeof(from);
    // accept
    SOCKET client = accept(server, (SOCKADDR*)&client, &clientlen);
    if (client == INVALID_SOCKET)
    {
        std::cout << "Error in accept(): " << WSAGetLastError << std::endl;
        WSACleanup();
    }
    else
    {

        char clientIp[17];
        if (inet_ntop(AF_INET, &from.sin_addr, clientIp, 17) == NULL)
        {
            std::cout << "Can't get the client's ip: " << WSAGetLastError() << std::endl;
        }

        std::cout << "ip connected: " << clientIp << std::endl;

the whole code if you need it 整个代码(如果需要)

#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <string>

#pragma comment(lib, "ws2_32.lib")

int main()
{
    std::cout << "--- Tcp/ip Server ---" << std::endl;
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
    if (server == INVALID_SOCKET)
    {
        std::cout << "error in SOCKET(): "<< WSAGetLastError() << std::endl;
        WSACleanup();
    }
    sockaddr_in s;
    s.sin_family = AF_INET;
    s.sin_addr.s_addr = INADDR_ANY;
    s.sin_port = htons(52000);

    // bind
    if (bind(server, (sockaddr*)&s, sizeof(s)) == SOCKET_ERROR)
    {
        std::cout << "Error: bind()" << std::endl;
    }
    //listen
    if (listen(server, SOMAXCONN) == SOCKET_ERROR)
    {
        std::cout << "Error in listen(): " << WSAGetLastError() << std::endl;
        WSACleanup();
    }
    sockaddr_in from;
    int clientlen = sizeof(from);
    // accept
    SOCKET client = accept(server, (SOCKADDR*)&client, &clientlen);
    if (client == INVALID_SOCKET)
    {
        std::cout << "Error in accept(): " << WSAGetLastError << std::endl;
        WSACleanup();
    }
    else
    {

        char clientIp[17];
        if (inet_ntop(AF_INET, &from.sin_addr, clientIp, 17) == NULL)
        {
            std::cout << "Can't get the client's ip: " << WSAGetLastError() << std::endl;
        }

        std::cout << "ip connected: " << clientIp << std::endl;

        // the code isn't finished yet

        system("pause");
        WSACleanup();
    }
    return 0;
}

You are passing the address of the wrong variable in the second parameter of accept() . 您在accept()的第二个参数中传递了错误变量的地址。

You are passing the address of your SOCKET client variable that you are about to assign the result of accept() to. 您正在传递将要向其分配accept()结果的SOCKET client变量的地址。 C++ allows a variable's address to be taken when declaring and initializing the variable in the same statement. 当在同一条语句中声明和初始化变量时,C ++允许使用变量的地址。 But that is not what you want in this case. 但这不是您想要的。 You need to pass the address of your sockaddr_in from variable instead: 您需要改为通过变量传递sockaddr_in from的地址:

sockaddr_in from;
int clientlen = sizeof(from);
// accept
SOCKET client = accept(server, (SOCKADDR*)&from, &clientlen); // <-- &from instead of &client

You are leaving your from variable uninitialized, and your compiler fills uninitialized variables with 0xCC (decimal 204) bytes in debug mode, so that is why you end up seeing 204.204.204.204 (hex 0xCC 0xCC 0xCC 0xCC ) from inet_ntop() when you don't initialize your from variable properly. 你离开你from变量初始化,和你的编译器填充未初始化的变量具有0xCC (十进制204)字节在调试模式下,所以这就是为什么你最终看到204.204.204.204 (十六进制0xCC 0xCC 0xCC 0xCC )从inet_ntop()时,你不from正确初始化from变量。

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

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