Transmitting video from the camera to the OPENCV c++ tcp ip client

Good time of the day. I’m new to OpenCV. My task is to write a client-server program that will record video to a buffer until the client connects to the camera, and then transfer this buffer with video to him via TCP IP.

The code still doesn’t work as it should. It records the video to a 5.7 kB file on the client with a duration of 0 seconds, i.e. either the client receives the data incorrectly, or the server generates and sends them incorrectly. Let me remind you once again that my task is to develop a server that will record video until the client connects to it, then the recording stops and the data is transmitted to the client, where it is already saved as a file. I will be glad of any help!

Server (VIDEO CAMERA)

#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <fcntl.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

using namespace std;
using namespace cv;

int c,new_socket;
std::vector<uchar> buffer;
std::vector<uchar> arrak;
int i=0,k=0;
int bbytee;
Mat image;
int bytes=0;

int main() {
    int sock,listener;
    std::vector<uchar> buf;
    std::vector<int> params(2);
    params[0] = cv::IMWRITE_PNG_COMPRESSION;
    params[1] = 10;
    struct sockaddr_in addr,client;

    sock = socket(AF_INET, SOCK_STREAM, 0); 
    if(sock < 0) {
        perror("socket");
        exit(1);
    }
    fcntl(sock, F_SETFL, O_NONBLOCK); // set non-blocking mode

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(3425);
    addr.sin_addr.s_addr = htonl(INADDR_ANY); 

    VideoCapture cap(2); 

        if(!cap.isOpened()) { 
            cout<< "Could not open the camera" <<  endl;
            close(sock);
            return -1;
        }
    if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 
        perror("[server] binding failed!");
        exit(2);
    }

    Mat frame = Mat::zeros(1080, 1920, CV_8UC3); 
    int imgSize = frame.cols*frame.rows*3; 

    listen(sock, 1); // listen for connections

    c = sizeof(struct sockaddr_in); // structure size

    while (true) {

        puts("Waiting for incoming connections...");
        new_socket = accept(sock, (struct sockaddr *)&client, (socklen_t*)&c);// receive socket
        cout << "New_socket: " << new_socket << endl;
        cap >> frame;//read from cap to frame
        imencode(".jpg", frame, buffer,params); //transcode to buffer buffer

               if(frame.empty()) {
                   cerr<<"[client] VideoCapture(0) error!"<<endl;
               }

               cout<< ++i << ":"<< frame.isContinuous()<<"," <<frame.size()<<endl;

        if(new_socket == -1) {//if there is no client connection, then...
            puts("Connection not accepted");
            cout << "new_socket = " << new_socket << "\n";
        } else {//if the client is connected, then ..
             int size = buffer.size()*sizeof buffer[0];//define buffer size
             send(new_socket, &size, sizeof(size), 0);

             cout << "Razmer" << size << endl;
            cout << "Transmission started" << endl;

           bytes=send(new_socket,buffer.data(), buffer.size()*sizeof buffer[0], 0);// pass the buffer size to the client
           cout << "BYTES: "<<bytes<<endl;


            break;
        }
    }
    cout << "END" << endl;
    close(sock); 
    return 0;
}

Client(PC):

#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int recv_size;
Mat frame;
std::vector<uchar> buffer;
int size = 0;

int main() {
    int sock;
    struct sockaddr_in addr;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("socket");
        exit(1);
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(3425);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_addr.s_addr = inet_addr("192.168.3.11");

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { // connect to the server
        perror("connect");
        exit(2);
    }

    if ((recv_size = recv(sock, (char*)&size, sizeof(uint32_t), 0)) != sizeof(uint32_t)) { //get buffer size
        perror("recv");
        exit(3);
    }

    cout << "SIZE FRAME: " << size << endl;


    VideoWriter outputVideo;
    Size S = Size((int)1920, (int)1080);//image size
    outputVideo.open("receive.avi", VideoWriter::fourcc('H', '2', '6', '4'), 25, S, true);//opening the receive.avi file for writing

    buffer.resize(size);//change the client's buffer to match the size of the server's buffer
    int bytes_received = 0;
while (bytes_received < size) {
    int chunk_size = min(size - bytes_received, 4096); // accept up to 4096 bytes at a time
    int recv_size = recv(sock, &buffer[bytes_received], chunk_size, 0);//receiving data from the server
    if (recv_size <= 0) {//if received is less than or equal to 0, then exit from the loop
        perror("recv");
        exit(3);
    }
    bytes_received += chunk_size;//increase the amount of received
}
    cout << "IDET DO IMDECODE " << recv_size << endl;

    frame = imdecode(buffer, IMREAD_COLOR);//convert data to Mat format
    outputVideo << frame;//write the buffer to the receive.avi file with the specified parameters
    close(sock);
    outputVideo.release();//release outputVideo

    return 0;
}