0

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;
}
MatGeneral
  • 57
  • 6
  • The second parameter to `accept` looks wrong. – 1201ProgramAlarm Jul 09 '19 at 02:40
  • Side issue, you'll be pleased to know, your English isn't bad. It's not perfect, but neither is most native speakers' English, so... –  Jul 09 '19 at 02:41
  • @1201ProgramAlarm maybe but the returned value of the function is not equal to NULL so it's supposed to be good (I think). – MatGeneral Jul 09 '19 at 02:52
  • @Chipster haha thank you! – MatGeneral Jul 09 '19 at 02:55
  • @1201ProgramAlarm omg I just saw my mistake thank you. – MatGeneral Jul 09 '19 at 02:58
  • 1
    Things like `204.204.204.204` should alert all your alarm bells next time you see it: 204 is in hex. `0xcc`. Have a look at [SO: In Visual Studio C++, what are the memory allocation representations?](https://stackoverflow.com/a/127404/7478597) to see what I mean. ;-) – Scheff's Cat Jul 09 '19 at 06:43
  • regarding the calls to `WSACleanup()` This does not terminate the application. Therefore, the application keeps running, resulting in the code following the call to `WSACleanup()` will be executed. This is an error when functions like `socket()`, bind()`, listen()`, etc have failed. Suggest reading: [WSACleanup()](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsacleanup) – user3629249 Jul 09 '19 at 15:28
  • `C` and `C++` are two different languages. The posted code is `C++`. Suggest removing the `c` tag – user3629249 Jul 09 '19 at 15:32
  • @user3629249 Ok sorry. – MatGeneral Jul 09 '19 at 15:39
  • Thank you for all your answers, I managed to get it working thanks to you. – MatGeneral Jul 09 '19 at 15:42

1 Answers1

1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770