来自0.0.0.0:32772的意外套接字连接

时间:2016-10-12 15:54:26

标签: c++ sockets network-programming

int SocketDebugger::Init(unsigned short p)
{
    if (m_state != dsDisconnect)
    {
        return 0;
    }

    if (PISocket::Startup()) 
    {
        return -1;
    }

    m_port = p;
    m_debug_socket = PISocket::Socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
    if (m_debug_socket == INVALID_SOCKET) 
    {
        Log(LEVEL_PYDEBUGGER, "GetLastError: %d", PISocket::Errno());
        return -2;
    }

    sockaddr_in local;
    memset(&local, 0, sizeof(local));
    local.sin_family = PF_INET;
    local.sin_port = htons(m_port);

    int ret = PISocket::Bind(m_debug_socket, (sockaddr*)&local);
    if (ret == SOCKET_ERROR)
    {
        PISocket::Close(m_debug_socket);
        m_debug_socket = INVALID_SOCKET;
        return -3;
    }

    ret = PISocket::Listen(m_debug_socket, 0);
    if (ret == SOCKET_ERROR)
    {
        PISocket::Close(m_debug_socket);
        m_debug_socket = INVALID_SOCKET;
        return -4;
    }

    unsigned long nonblocking = 1;
    ret = PISocket::Ioctl(m_debug_socket, FIONBIO, &nonblocking);
    if (ret == SOCKET_ERROR)
    {
        PISocket::Close(m_debug_socket);
        m_debug_socket = INVALID_SOCKET;
        return -5;
    }

    m_state = dsListen;


    unsigned long t = (unsigned long)PITime() + 2000;

    do 
    {
        TryAccept();
    }

    while ((unsigned long)PITime() < t && m_state == dsListen);
    LogInfo("profiler socket init port: %d", port)
    return 0;
}

bool SocketDebugger::TryAccept()
{
    sockaddr_in local;
    //int len = sizeof(local);
    // set non blocking...  

    m_gui_socket = PISocket::Accept(m_debug_socket, (sockaddr*)&local);

    if (m_gui_socket == INVALID_SOCKET) 
    {       
        if (PISocket::Errno() == EWOULDBLOCK)
        {   
            return false;
        }
    }

    LogInfo("profiler connected! %s:%d", inet_ntoa(local.sin_addr), local.sin_port);
    m_state = dsConnect;
    m_send_stream.clear();
    m_recv_stream.clear();
    return true;
}

我使用远程探查器连接到我的程序。我可以在我的电脑上很好地运行这段代码,但在ios中,TryAccept总是从0.0.0.0:32772接收连接。日志是:

5   [23:43:04.368]   [UTILS] [INFO] profiler connected! 0.0.0.0:32772
6   [23:43:04.368]   [UTILS] [INFO] profiler socket init port: 7776, 0

为什么呢? ps:在init

之后也会在别处调用TryAccept

1 个答案:

答案 0 :(得分:2)

我看不出这段代码在任何地方都有用过。只要std::optional不等于m_gui_socketINVALID_SOCKET就会无条件地打印“profiler connected”,并显示未初始化的SocketDebugger::TryAccept的内容。那里什么都没有实际执行接受操作!