简体   繁体   中英

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. Every time I try to connect it display 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. 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() .

You are passing the address of your SOCKET client variable that you are about to assign the result of accept() to. C++ allows a variable's address to be taken when declaring and initializing the variable in the same statement. 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;
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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