gpt4 book ai didi

sockets - 谁能指导我或建议如何为通过 HTTP 的实时媒体服务器流式传输创建客户端

转载 作者:行者123 更新时间:2023-12-03 11:53:17 25 4
gpt4 key购买 nike

任何人都可以指导我或建议如何使用套接字创建客户端,以便通过 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 连接但无法接收任何内容。

最佳答案

最简单的解决方案是使用 openRTSP来自 liveMedia 的客户作为起点。它几乎可以归结为实现整个 RTSP 堆栈以及通过 TCP 连接交错媒体。通过将“-T”作为命令行参数传递,您可以将 openRTSP 配置为通过 TCP 上的 RTSP 进行流式传输。然后,您可以基于 openRTSP 编写自己的应用程序,然后您可以根据需要处理传入的媒体样本。

我会建议你反对从套接字级别自己实现此功能。您需要实现 RTSP、RTP、RTCP、RTSP over HTTP 隧道、SDP、各种 RTP 有效负载格式,例如对于 H.264。您上面与套接字相关的代码段并没有开始接触表面。

如果您想查看协议(protocol)交换的样子,请使用wireshark 嗅探从openRTSP 到RTSP 服务器的流量。您还可以在 liveMedia 上找到 RTSP 服务器。

关于sockets - 谁能指导我或建议如何为通过 HTTP 的实时媒体服务器流式传输创建客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com