C ++ - 仍然获得本地IP而不是客户端

时间:2016-01-20 17:44:54

标签: c++ sockets ip client local

我在使用套接字获取客户端IP地址时出现问题。 我仍然得到' 127.0.0.1'作为客户端IP。但为什么?那问题呢?

这是序列化的片段(第一步):

int Serializement::serialize(std::string srvip, unsigned short portno)
{
wsaError = WSAStartup(wVersionRequested, &wsaData);
if (wsaError != 0)
{
    printf("Server: The Winsock dynamic link library (dll) was not found!\n");
    retval = 100;
    return 100;
}
else 
    if (ConfigLoaderH::ConfigLoader::ServerDisplayMessages() != 0)
    {
        printf("Server: The Windsock dll was found!\n");
        printf("Server: The status: %s.\n", wsaData.szSystemStatus);
    }

if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
    printf("Server: The dll does not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
    WSACleanup();
    retval = 101;
    return 101;
}
else
    if (ConfigLoaderH::ConfigLoader::ServerDisplayMessages() != 0)
    {
        printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
        printf("Server: The hightest version this dll can support: %u.%u!\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
    }


m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (m_Socket == INVALID_SOCKET)
{
    printf("Server: Error at socket(): %ld.\n", WSAGetLastError());
    WSACleanup();
    retval = 102;
    return 102;
}
else
    if (ConfigLoaderH::ConfigLoader::ServerDisplayMessages() != 0)
        printf("Server: socket() is OK!\n");


service.sin_family = AF_INET;
service.sin_addr.S_un.S_addr = inet_addr(srvip.c_str());
service.sin_port = htons(portno);

int yes = 1;

if (setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) != SOCKET_ERROR)
{
    if (ConfigLoaderH::ConfigLoader::ServerDisplayMessages() != 0)
        printf("Server: Reuse Address was successful.\n");
}
else
{
    printf("Server: Reusing address failed...\n");
    retval = 103;
    return 103;
}
if (bind(m_Socket, (sockaddr *)&service, sizeof(service)) == SOCKET_ERROR)
{
    printf("Server: bind() failed, %ld.\n", WSAGetLastError());
    closesocket(m_Socket);
    retval = 104;
    return 104;
}
else
    if (ConfigLoaderH::ConfigLoader::ServerDisplayMessages() != 0)
        printf("Server: bind() is OK!\n");

if (listen(m_Socket, 10) == SOCKET_ERROR)
{
    printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
    retval = 105;
    return 105;
}
else
    printf("***Hint: Server is ready... Application form can now be run...***\n");

retval = 106;
return 106; }

这是一个倾听的片段(第二步):

...sockaddr_in client_info;...
... try
    {
        printf("Server: Waiting for a client to connect...\n");
        client_info = { 0 };
        socklen_t c_len = sizeof(client_info);

        AcceptSocket = accept(m_Socket, (sockaddr *)&client_info, &c_len);
        if (AcceptSocket == INVALID_SOCKET)
            printf("Server: accept() failed.\n");
        else
        {
            char * connected_ip = inet_ntoa(client_info.sin_addr);
            printf("Connected client-ip: %s\n", connected_ip);
        }

        printf("Server: Getting client-info is done!\n");

我希望有人会找到导致这个错误的行。 谢谢你将来!

0 个答案:

没有答案