UDP 服务器没有响应
我不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在评论中,您说
recvfrom()
报告错误10054
,即WASECONNRESET
。recvfrom()
MSDN 上的文档对此进行了以下说明:换句话说,您将请求发送到未侦听的端口对于 UDP 消息。这是有道理的,因为您将请求发送到端口 80,该端口通常用于通过 TCP 的 HTTP 流量,而不是通过 UDP 的 RTSP 流量。
就此而言,您发送的内容甚至不是有效的 RTSP 请求。我建议您阅读 RTSP 协议 的实际工作原理(提示:您不知道) t 发送
rtsp://
URL)。In comments, you say that
recvfrom()
is reporting error10054
, which isWASECONNRESET
. Therecvfrom()
documentation on MSDN says the following about that: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).