Ask Your Question

pwk3's profile - activity

2020-09-18 02:22:59 -0500 received badge  Famous Question (source)
2019-10-17 05:57:51 -0500 received badge  Notable Question (source)
2019-05-23 14:21:33 -0500 received badge  Popular Question (source)
2018-08-24 08:03:28 -0500 commented answer Send/Receive vector <Mat> over Socket C/C++

That fixed it!! Thank you for your help @opalmirror. You're the man!

2018-08-23 16:00:43 -0500 edited question Send/Receive vector <Mat> over Socket C/C++

Send/Receive vector <mat> over Socket C/C++ Hello, I'm trying to send a vector full of Mat images from one

2018-08-23 16:00:09 -0500 commented answer Send/Receive vector <Mat> over Socket C/C++

I went ahead and updated the code with what I have at the moment. I'll try and implement the htonl/ntohl soon.

2018-08-23 15:58:33 -0500 edited question Send/Receive vector <Mat> over Socket C/C++

Send/Receive vector <mat> over Socket C/C++ Hello, I'm trying to send a vector full of Mat images from one

2018-08-23 15:58:03 -0500 received badge  Editor (source)
2018-08-23 15:58:03 -0500 edited question Send/Receive vector <Mat> over Socket C/C++

Send/Receive vector <mat> over Socket C/C++ Hello, I'm trying to send a vector full of Mat images from one

2018-08-22 20:28:09 -0500 commented answer Send/Receive vector <Mat> over Socket C/C++

Thanks for reply @opalmirror. Yeah, I get the size of the entire uchar buffer and send it over the socket before I send

2018-08-22 10:33:16 -0500 commented answer Send/Receive vector <Mat> over Socket C/C++

Sorry guys, I'm just now getting back to this. I only have one problem that I'm facing. What is the best way for the loo

2018-08-14 14:05:21 -0500 marked best answer Send/Receive vector <Mat> over Socket C/C++

Hello, I'm trying to send a vector full of Mat images from one computer to another over a TCP/IP sockets C/C++ connection. Right now I can get the vector of Mat images and send it to another function to save them, then sends .png files over the connection. However, I want to be able to send just the vector over the socket and save the images (with imwrite) on the other system. I can make the connection and send data between them, because I've already sent an array, of type double, over and all the data was correct. When I tried the vector, I get segmentation fault (core dumped) which makes me think it's something to do with the size of the vector being passed incorrectly. I'm pretty new to socket programming, so I'm probably just making simple mistakes. Here is my code so far:

Client ('images' is the vector):

    int sock;
    struct sockaddr_in server;
    #define MB 2591 * 1944

    for(int y = 0; y < EXPECTED_IMAGES; y++)
    {
       //Creates the socket to be used
       sock = socket(AF_INET, SOCK_STREAM, 0);
       if(sock == -1)
       {
           printf("Could not create socket");
       }

       puts("Socket created");

       server.sin_addr.s_addr = inet_addr(IP);
       server.sin_family = AF_INET;
       server.sin_port = htons (port_num);

       //Connects to the remote server
       if(connect(sock ,(struct sockaddr *)&server, sizeof(server)) <0)
       {
            perror("Connect failed. Error");
            return 1;
       }

       if(y == 0)
       {
          send(sock, &expected_images, sizeof(expected_images), 0);
       }

       vector <uchar> buffer;
       buffer.resize(MB);
       imencode(“.jpg”, images[y], buffer);
       int buffer_size = buffer.size();

       send(sock, &buffer_size, sizeof(buffer_size), 0);
       send(sock, buffer.data(), buffer.size(), 0);

       close(sock);
    }

Server:

    int socket_desc , client_sock , c , read_size;
    struct sockaddr_in server , client;

    int buffer_size;
    int i = 0;
    int expected_images = 0;
    int num = 5;

    vector <uchar> buffer;
    vector <Mat> images;
    Mat frame;

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
            printf("Could not create socket");
    }

    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(port_num);

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
            //print the error message
            perror("bind failed. Error");
            return 1;
    }

    puts("bind done");

    //Listen for incoming clients
    listen(socket_desc , 3);

    puts("Waiting for incoming connections...\n");

    while(i < num)
    {

            int buffer_size = 0;
            int len = 0;
            int remain = 0;

            c = sizeof(struct sockaddr_in);

            //Accept connection from an incoming client
            client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
            if (client_sock < 0)
            {
                    perror("accept failed");
                    return 1;
            }

            if(i == 0)
            {
                    recv(client_sock, &expected_images, sizeof(expected_images), 0);
                    num = expected_images;
            }

            //Recieves the byte size of the client file.
            recv(client_sock, &buffer_size, sizeof(int),0);
            buffer.resize(buffer_size);

            remain = buffer_size;

            while(((len = recv(client_sock, buffer.data(), buffer_size, 0)) > 0) && (remain > 0))
            {

                    //Issue right here for saving the buffer
                    remain -= len;
            }

            //frame = imdecode(buffer, CV_LOAD_IMAGE_UNCHANGED);

            //images.push_back(frame);

            i += 1;

            close(client_sock);

    }

Any help would be appreciated. Thank you.

2018-08-14 14:05:21 -0500 received badge  Scholar (source)
2018-08-14 11:24:51 -0500 commented answer Send/Receive vector <Mat> over Socket C/C++

Thank you for the reply @berak. I'll give it a shot.

2018-08-14 11:12:19 -0500 commented question Send/Receive vector <Mat> over Socket C/C++

Ok, Thank you.

2018-08-14 11:07:21 -0500 commented question Send/Receive vector <Mat> over Socket C/C++

So Mat data type can't be sent over a socket in any way?

2018-08-14 10:58:32 -0500 asked a question Send/Receive vector <Mat> over Socket C/C++

Send/Receive vector <mat> over Socket C/C++ Hello, I'm trying to send a vector full of Mat images from one