发送UDP数据会挂起接收应用程序

时间:2019-06-12 07:07:16

标签: c++ c++11 visual-c++

我正在将UDP数据包发送到Hercules进行测试,每当我发送数据时,它就会挂在Hecrcules上。

我不确定我的代码是否有问题。

请查看我的代码,如果有任何问题,请告诉我。

void UDPConnect::Connect(unsigned short local_port, const char* local_addr)
{
    WSADATA wsa;
    int err;
    err = WSAStartup(MAKEWORD(2, 2), &wsa);
    if (err != 0)
    {
        //std::cout << "Failed. Error Code : " << err << std::endl;
        QMessageBox Msgbox;
        Msgbox.setText("Udp Connection Failed.");
        Msgbox.exec();
        exit(EXIT_FAILURE);
    }

    //Create a socket
    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
    {
        //  err = WSAGetLastError();
        //  std::cout << "Could not create socket : " << err << std::endl;
        QMessageBox Msgbox;
        Msgbox.setText("Could not create socket :");
        Msgbox.exec();
        exit(EXIT_FAILURE);
    }

    //Prepare the sockaddr_in structure
    struct sockaddr_in server = {};
    server.sin_family = AF_INET;
    server.sin_port = htons(local_port);
    if (local_addr)
    {
        server.sin_addr.s_addr = inet_addr(local_addr);
        if (server.sin_addr.s_addr == INADDR_NONE)
        {
            //  std::cout << "Invalid local address specified" << std::endl;
            QMessageBox Msgbox;
            Msgbox.setText("Invalid local address specified.");
            Msgbox.exec();
            closesocket(s);
            exit(EXIT_FAILURE);
        }
    }
    else
        server.sin_addr.s_addr = INADDR_ANY;

    //Bind
    if (::bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
    {
        //  err = WSAGetLastError();
        //  std::cout << "Bind failed with error code : " << err << std::endl;
        QMessageBox Msgbox;
        Msgbox.setText("Bind failed.");
        Msgbox.exec();
        closesocket(s);
        exit(EXIT_FAILURE);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int UDPConnect::SendPacket(byte *buffer, unsigned int buf_size, const  char* remote_addr, unsigned short remote_port)
{
    struct sockaddr_in si_other = {};
    int send_len;
    //Prepare the sockaddr_in structure
    si_other.sin_family = AF_INET;
    si_other.sin_addr.s_addr = inet_addr(remote_addr);
    si_other.sin_port = htons(remote_port);

    if ((send_len = sendto(s, (char*)buffer, buf_size, 0, (struct sockaddr *) &si_other, sizeof(si_other))) == SOCKET_ERROR)
    {

    }
    return send_len;
}

当我使用boost库发送数据时,我没有问题。

bool UDPConnect::send_udp_message(const std::string& message, const std::string& destination_ip, const unsigned short port)
{
    boost::asio::io_service io_service;
    boost::asio::ip::udp::socket socket(io_service);

    // Create the remote endpoint using the destination ip address and
    // the target port number.  This is not a broadcast
    auto remote = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(destination_ip), port);
    try {

        // Open the socket, socket's destructor will
        // automatically close it.
        socket.open(boost::asio::ip::udp::v4());

        // And send the string... (synchronous / blocking)
        socket.send_to(boost::asio::buffer(message), remote);
    }
    catch (const boost::system::system_error& ex) {
        // Exception thrown!
        // Examine ex.code() and ex.what() to see what went wrong!
        return false;
    }

    return true;
}

0 个答案:

没有答案