当我尝试将UDP套接字绑定到具有SFML的端口时,为什么会出现错误?

时间:2017-09-25 20:51:10

标签: c++ sockets network-programming udp sfml

我目前正在使用UDP为SFML构建网络原型。我创建了一个应用程序,您可以选择成为服务器或客户端。所以,通常我启动应用程序的2个实例,让一个是服务器而另一个是客户端。我正在连接到我的本地IP 127.0.0.1。

大约20分钟前,这个精确的代码工作,我没有遇到任何问题。我没有更改任何内容,但此代码不再起作用,当我为服务器调用socket.bind()时,程序崩溃了。客户端,没有错误显示。

这是在C ++中完成的。

有谁知道可能的原因?据我所知,端口也没有使用。

我的客户代码:

void runUdpClient(unsigned short port){
    sf::IpAddress server;
    do
    {
        std::cout << "Type the address or name of the server to connect to: ";
        std::cin >> server;
    } while (server == sf::IpAddress::None);

    sf::UdpSocket socket;

    sf::Packet clientSendPacket;
    std::string packetSendContent = "Packet data from the client";
    clientSendPacket << packetSendContent;
    if (socket.send(clientSendPacket, server, port) != sf::Socket::Done)
        return;
    std::cout << "Message sent to the server: \"" << packetSendContent << "\"" << std::endl;

    // Maak variabelen
    sf::IpAddress sender;
    unsigned short senderPort;
    sf::Packet clientReceivePacket;
    std::string clientReceiveContent;
    if (socket.receive(clientReceivePacket, sender, senderPort) != sf::Socket::Done)
        return;

    clientReceivePacket >> clientReceiveContent;
    std::cout << "Message received from " << sender << ": \"" << clientReceiveContent << "\"" << std::endl;

}

我的服务器代码:

void runUdpServer(unsigned short port){
    sf::UdpSocket socket;

    if (socket.bind(port) != sf::Socket::Done)
        std::cout << "Error binding socket for server";
        return;

    std::cout << "Server is listening to port " << port << ", waiting for a message... " << std::endl;

    sf::Packet serverReceivePacket;
    std::string serverReceiveContent;
    sf::IpAddress sender;
    unsigned short senderPort;

    if (socket.receive(serverReceivePacket, sender, senderPort) != sf::Socket::Done)
        std::cout << "Error receiving packet for server";
        return;
    serverReceivePacket >> serverReceiveContent;
    std::cout << "Message received from client " << sender << ": \"" << serverReceiveContent << "\"" << std::endl;

    sf::Packet serverSendPacket;
    std::string serverSendContent = "Packet data from server";
    serverSendPacket << serverSendContent;
    if (socket.send(serverSendPacket, sender, senderPort) != sf::Socket::Done)
        std::cout << "Error sending packet from server";
        return;

    std::cout << "Message sent to the client: \"" << serverSendContent << "\"" << std::endl;
}

3 个答案:

答案 0 :(得分:0)

您的服务器很可能在未正确关闭端口的情况下关闭。 如果您等待几分钟,端口将可以再次使用(在我的Ubuntu 16.04系统上大约需要2-3分钟,根据您使用的操作系统可能会有所不同)

或者,您可以更改您正在使用的端口。

答案 1 :(得分:0)

在您的服务器代码中,if语句缺少大括号,因此只要return退出,您就会一直执行socket.bind(),无论是成功还是失败。这段代码:

if (socket.bind(port) != sf::Socket::Done)
    std::cout << "Error binding socket for server";
    return;

与此代码相同:

if (socket.bind(port) != sf::Socket::Done)
    std::cout << "Error binding socket for server";
return;

这在逻辑上等同于此代码:

if (socket.bind(port) != sf::Socket::Done) {
    std::cout << "Error binding socket for server";
}
return;

空格格式对C和C ++并不重要。要在if块的主体内执行多个语句,您需要显式括号:

if (socket.bind(port) != sf::Socket::Done)
{
    std::cout << "Error binding socket for server";
    return;
}

与服务器调用ifsocket.receive()的{​​{1}}语句相同。

如果socket.send()socket.send()失败,您的客户端代码不会输出任何错误消息,那些socket.receive()语句在其正文中执行单个语句(即if) ,所以大括号是可选的。

答案 2 :(得分:-1)

问题在于ifreturn的格式化。点击 Ctrl + Shift + K 后,代码再次格式化并且有效。