谁能指导我或建议如何使客户端使用HTTP上的实时媒体服务器流的套接字,因为我尝试了很多,但没有成功。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
//************All Declarations**********//
#define DEFAULT_BUFLEN 512000
#define DEFAULT_PORT "8000"
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
getch();
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
getch();
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
getch();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
getch();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
getch();
return 1;
}
// Send an initial buffer
/*iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
getch();
return 1;
}*/
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
getch();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
// cleanup
getch();
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
我尝试了上面的代码,它在我创建的服务器上工作得很好。问题是我正在通过http从实况媒体服务器流式传输一个传输流文件,但是当我试图从我的客户端代码接收数据时,我能够连接到特定的url,但是不能接收任何东西。
发布于 2012-04-17 19:06:11
对于您来说,最简单的解决方案是使用liveMedia中的openRTSP客户端作为起点。这在很大程度上可以归结为实现整个RTSP堆栈,以及通过TCP连接交错媒体。通过将"-T“作为命令行参数传入,您可以将openRTSP配置为通过TCP上的RTSP进行流式传输。然后,您可以基于openRTSP编写自己的应用程序,然后可以根据需要在其中处理传入的媒体样本。
我建议不要让自己从套接字级别实现这个功能。您需要实现RTSP、RTP、RTCP、基于HTTP隧道的RTSP、SDP、各种RTP有效载荷格式,例如用于H.264。上面与套接字相关的代码段并没有开始触及表面。
如果您想查看协议交换是什么样子,可以使用wireshark嗅探从openRTSP到RTSP服务器的流量。您还可以在liveMedia上找到RTSP服务器。
发布于 2012-04-17 18:40:38
如果它是通过HTTP传输的,那么首先必须发送一个HTTP请求(然后解析响应)。Example。
您没有收到任何东西,因为服务器正在等待请求。
https://stackoverflow.com/questions/10187896
复制相似问题