使用套接字编程将帧从一个PC发送到另一个PC

时间:2018-07-23 07:07:55

标签: sockets opencv visual-c++ frame send

我面临断言失败的问题,我现在尝试在给定的图片中显示它,我应该怎么做才能进一步正确接收它。我给了视频路径客户端一个接收器,但是如何修复它。要播放整个视频。

代码中是否有任何错误?

我面临断言失败的问题,我现在尝试在给定的图片中显示它,我应该怎么做才能进一步正确接收它。我给了视频路径客户端一个接收器,但是如何修复它。要播放整个视频。

代码中是否有任何错误?

enter image description here

// server.cpp : Defines the entry point for the console application.
//
#include <future>
#include <iostream>
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <WinSock2.h>
#pragma comment (lib,"Ws2_32.lib")
#include<sys/types.h>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <Windows.h>
#include <opencv2\video\tracking.hpp>
#include "opencv2\video\background_segm.hpp"
using namespace cv;
using namespace std;
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
Mat cameraFeed;
int height, width, IM_HEIGHT, IM_WIDTH, imgSize;

using namespace std;

int main()
{
    VideoCapture cap("5-Minutes Video.avi");
    if (!cap.isOpened())
    {
        cout << "Error opening video stream" << endl;
        system("pause");
        return -1;
    }
    int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
    int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    VideoWriter video("recording.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(frame_width, frame_height));

    WSADATA wsa_data;
    SOCKADDR_IN server_addr, client_addr;
    WSAStartup(MAKEWORD(2, 2), &wsa_data);
    const auto server = socket(AF_INET, SOCK_STREAM, 0);

    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(5555);

    ::bind(server, reinterpret_cast<SOCKADDR *>(&server_addr), sizeof(server_addr));
    listen(server, 0);

    cout << "Listening for incoming connections..." << endl;
    system("pause");
    int client_addr_size = sizeof(client_addr);

    while (1)
    {
        SOCKET client;



        // Capture frame-by-frame 
        cap >> cameraFeed;

        // If the frame is empty, break immediately
        if (cameraFeed.empty())
            break;

        // Write the frame into the file 'outcpp.avi'
        video.write(cameraFeed);

        // Display the resulting frame    
        try
        {
        if ((client = accept(server, reinterpret_cast<SOCKADDR *>(&client_addr), &client_addr_size)) != INVALID_SOCKET)
        {

            while (true) {

                cap >> cameraFeed;
                if (!cap.read(cameraFeed)) {
                    cout << "Video Ended" << endl;
                    system("pause");
                    break;
                }
                height = cameraFeed.rows;
                width = cameraFeed.cols;
                IM_HEIGHT = FRAME_HEIGHT;
                IM_WIDTH = FRAME_WIDTH;
                resize(cameraFeed, cameraFeed, Size(IM_WIDTH, IM_HEIGHT));
                imgSize = cameraFeed.total()*cameraFeed.elemSize();
                send(client, reinterpret_cast<const char*>(cameraFeed.data), imgSize, 0);
                imshow("Server", cameraFeed);
                video.write(cameraFeed);
                if (waitKey(30) == 27) {
                    closesocket(client);
                    cout << "Client Disconnected";
                    system("pause");
                    WSACleanup();
                    return 0;
                }
            }
        }
        }
        catch (Exception e)
        {
            e.what();
            system("pause");
        }
        cap.release();
        video.release();
        const auto last_error = WSAGetLastError();
        if (last_error > 0)
        {
            cout << "Error: " << last_error << endl;
            system("pause");
        }
    }
}
  

Blockquote

0 个答案:

没有答案