IXWebSocket icon indicating copy to clipboard operation
IXWebSocket copied to clipboard

websocketserver works in listening 127.0.0.1, failed in localhost

Open Hedgezzz opened this issue 2 years ago • 1 comments

I have a websocket server which mostly from IXWebSocketServerTest.cpp compiled with g++ :

g++ --std=c++14 -O2 -I./include/ websocketserver.cpp -lixwebsocket -pthread -lz -lssl -lcrypto -o websocketserver.exe

it works fine , still I have a problem :

If I run ap like : ./websocketserver.exe 127.0.0.1 55688 would be fine but if ./websocketserver.exe localhost 55688 would get error : SocketServer::listen() error calling inet_pton at address localhost:55688 : Success

in my cpp :

    int port = atoi(argv[2]);
    ix::WebSocketServer server(port,argv[1]);

in IXSocketServer.cpp :

        struct sockaddr_in server;
        server.sin_family = _addressFamily;
        server.sin_port = htons(_port);

        if (ix::inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
        {
            std::stringstream ss;
            ss << "SocketServer::listen() error calling inet_pton "
               << "at address " << _host << ":" << _port << " : "
               << strerror(Socket::getErrno());

            Socket::closeSocket(_serverFd);
            return std::make_pair(false, ss.str());
        }

I wonder why this happened , any idea ?

Hedgezzz avatar Mar 01 '23 01:03 Hedgezzz

@Hedgezzz

https://man7.org/linux/man-pages/man3/inet_pton.3.html

inet_pton - convert IPv4 and IPv6 addresses from text to binary form

"localhost" is not a valid IP address

lanthora avatar Apr 27 '23 11:04 lanthora