UDP 服务器没有响应

发布于 2025-01-12 13:13:12 字数 1783 浏览 0 评论 0原文

我不是 C++ 专家,也不是套接字通信专家。

我需要连接到服务器,并且该服务器应该使用 XML 格式流响应我的请求。

我已经创建了客户端(受到其他程序的启发),我尝试通过发送 RTSP 调用来连接到正确的 IP 地址和端口。连接似乎很好,但问题是当我运行 recvfrom() 时,我没有收到任何数据。我尝试制作 UDP 客户端,recvfrom() 返回 -1。

这是UDP客户端的代码:

void main(int argc, char* argv[]) {
 
    WSADATA data;
    WORD version = MAKEWORD(2, 2);

    int wsOk = WSAStartup(version, &data);
    if (wsOk != 0) {
        cout << "can't start winsock!" << wsOk << endl;
        return;
    }
    //create a hint structure for the server
    sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(80);

    inet_pton(AF_INET, "192.0.0.1", &server.sin_addr);

    //socket creation
    SOCKET out = socket(AF_INET, SOCK_DGRAM, 0);

    // write out to that socket
    
    string s = "rtsp://pluto:[email protected]/media.amp?video=0&audio=0&event=on";
    
    int sendOk = sendto(out, s.c_str(), s.size() + 1, 0, (sockaddr*)&server, sizeof(server));
    
    if (sendOk == SOCKET_ERROR) {
        cout << "That didn't work!" << WSAGetLastError() << endl;
    }
    else
    {
        cout << "connection from server is ok: " << sendOk << endl;
    }
    int len = sizeof(server);
    char buffer[1024];
    ZeroMemory(buffer, sizeof(buffer));
    int recOk = 0;
    
    recOk = recvfrom(out, buffer, sizeof(buffer), 0, (sockaddr*)&server, &len);
    if (recOk != SOCKET_ERROR)
    {
        printf("Receive response from server: %s\n", buffer);
           
    }
  

    //close the socket    
    closesocket(out);
    WSACleanup();
}

I am not an expert in C ++ nor in socket communication.

I need to connect to a server and this server should respond to my request with an XML format stream.

I have created the client (inspired by other programs) with which I try to connect to the correct IP address and port by sending a RTSP call. The connection seems to be fine, but the problem is that when I run recvfrom() I don't get any data. I tried to make a UDP client and recvfrom() returns -1.

This is the code of the UDP client:

void main(int argc, char* argv[]) {
 
    WSADATA data;
    WORD version = MAKEWORD(2, 2);

    int wsOk = WSAStartup(version, &data);
    if (wsOk != 0) {
        cout << "can't start winsock!" << wsOk << endl;
        return;
    }
    //create a hint structure for the server
    sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(80);

    inet_pton(AF_INET, "192.0.0.1", &server.sin_addr);

    //socket creation
    SOCKET out = socket(AF_INET, SOCK_DGRAM, 0);

    // write out to that socket
    
    string s = "rtsp://pluto:[email protected]/media.amp?video=0&audio=0&event=on";
    
    int sendOk = sendto(out, s.c_str(), s.size() + 1, 0, (sockaddr*)&server, sizeof(server));
    
    if (sendOk == SOCKET_ERROR) {
        cout << "That didn't work!" << WSAGetLastError() << endl;
    }
    else
    {
        cout << "connection from server is ok: " << sendOk << endl;
    }
    int len = sizeof(server);
    char buffer[1024];
    ZeroMemory(buffer, sizeof(buffer));
    int recOk = 0;
    
    recOk = recvfrom(out, buffer, sizeof(buffer), 0, (sockaddr*)&server, &len);
    if (recOk != SOCKET_ERROR)
    {
        printf("Receive response from server: %s\n", buffer);
           
    }
  

    //close the socket    
    closesocket(out);
    WSACleanup();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2025-01-19 13:13:12

在评论中,您说 recvfrom() 报告错误 10054,即 WASECONNRESETrecvfrom() MSDN 上的文档对此进行了以下说明:

<表类=“s-表”>
<标题>

错误代码
含义


<正文>

WSAECONNRESET
远程端执行硬关闭或中止关闭而重置了虚拟电路。应用程序应关闭套接字;它不再可用。 在 UDP 数据报套接字上,此错误表示先前的发送操作导致 ICMP 端口无法访问 消息。

换句话说,您将请求发送到未侦听的端口对于 UDP 消息。这是有道理的,因为您将请求发送到端口 80,该端口通常用于通过 TCP 的 HTTP 流量,而不是通过 UDP 的 RTSP 流量。

就此而言,您发送的内容甚至不是有效的 RTSP 请求。我建议您阅读 RTSP 协议 的实际工作原理(提示:您不知道) t 发送 rtsp:// URL)。

In comments, you say that recvfrom() is reporting error 10054, which is WASECONNRESET. The recvfrom() documentation on MSDN says the following about that:

Error codeMeaning
WSAECONNRESETThe virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket; it is no longer usable. On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message.

In other words, you sent your request to a port that is not listening for UDP messages. Which makes sense, as you sent your request to port 80, which is typically used for HTTP traffic over TCP, not RTSP traffic over UDP.

For that matter, what you sent wasn't even a valid RTSP request to begin with. I suggest you read up on how the RTSP protocol actually works (hint: you don't send a rtsp:// URL with it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文