2

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

4

2 回答 2

2

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

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

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

于 2012-04-17T11:06:11.407 回答
0

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

您没有收到任何东西,因为服务器正在等待请求。

于 2012-04-17T10:40:37.557 回答