任何人都可以指导我或建议如何为通过HTTP流式传输实时媒体服务器制作客户端



谁能指导我或建议如何使用套接字通过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-namen", argv[0]);
        getch();
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %dn", 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: %dn", 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: %ldn", 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: %dn", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        getch();
        return 1;
    }*/
    printf("Bytes Sent: %ldn", 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: %dn", 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: %dn", iResult);
        else if ( iResult == 0 )
            printf("Connection closedn");
        else
            printf("recv failed with error: %dn", WSAGetLastError());
    } while( iResult > 0 );

    // cleanup
    getch();
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}

尝试了上面的代码,它适用于我创建的服务器。问题是我正在通过http从实时媒体服务器流式传输传输流文件,但是当我尝试从客户端代码接收数据时,我能够在特定的URL连接,但无法接收任何内容。

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

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

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

如果是通过HTTP,您首先必须发送HTTP请求(然后解析响应)。例。

您不会收到任何消息,因为服务器正在等待请求。

相关内容

  • 没有找到相关文章

最新更新