SFML TCP问题

时间:2015-01-21 17:07:33

标签: c++ sfml

以下代码段有什么问题?

以下是客户端程序代码的片段:

   while (!done)
    {
        std::cout << "Creating connection" << std::endl;
        sf::TcpSocket socket;
        sf::Socket::Status status = socket.connect("192.168.0.5", 53000);
        if (status != sf::Socket::Done)
        {
            std::cout << "Client Error" << std::endl;
        }
        std::cout << "Created" << std::endl;
        char data[100] = { 'H', 'e', 'l', 'l', 'o', '\0' };
        system("pause");
        if (socket.send(data, 100) != sf::Socket::Done)
        {
            std::cout << "Client Error : Couldn't send data" << std::endl;
        }
    }

此代码以交替步骤正确发送数据。

我该如何解决?

此外,system(pause)用于生成延迟。

以下是服务器程序代码的片段:

while (!done)
{
    sf::TcpListener listener;

    // bind the listener to a port
    if (listener.listen(53000) != sf::Socket::Done)
    {
        // error...
    }

    // accept a new connection
    sf::TcpSocket client;
    if (listener.accept(client) != sf::Socket::Done)
    {
        // error...
        std::cout << "Error" << std::endl;
    }
    else
    {       
        std::cout << "New client connected: " << client.getRemoteAddress() << std::endl;
        char data[100];
        std::size_t received;

        // TCP socket:
        if (client.receive(data, 100, received) != sf::Socket::Done)
        {
            // error...
        }
        std::cout << "Received " << received << " bytes: " << data << std::endl;
        std::cout << "Ready" << std::endl;
    }

}

0 个答案:

没有答案