I have such a vector<vector<float>> matrix n-th size. how to transfer it through sockets to the server. The vector is used because it is impossible to create a regular array of size n float. There is an implementation made by me, it passes each element separately through send (), but this process is too long. On the Internet did not find anything sensible. Thanks in advance to everyone who responds

  • You need to serialize vector vectors into an array. And the actual array can already be transferred. - KoVadim
  • Do not tell me, with the help of what can it be produced? - Andrei Sosnovsky

2 answers 2

There are many ways. the simplest is to transmit vectors - the vector is a continuous structure, therefore, you can transfer the size of the vector and then the whole vector.

schematically.

 // отправим размер вектора vector<vector<float>> v; send(v.size(), sizeof(v.size())); // в цикле отправляем каждый кусочек, с размером и данными for (auto& x : v) { send(x.size(), sizeof(x.size())); send(x.data(), x.size()*sizeof(float)); } 

send is a send function. Yes, there types will need to be accurately brought, but it will cope.

At the receiving side, the size is read, then the attached data is read.

somewhere like this:

 vector<vector<float>> data; size_t t; read(&t, sizeof(t)); data.resize(t); for (size_t i = 0; i < t; i++) { size_t ti; read(&ti, sizeof(ti)); data[i].resize(ti); read(data[i].data(), sizeof(float)*ti); } 

a warning:

  • There is no validation check.
  • If the platform / compiler is different, then there may be problems.
  • it is advisable to add a couple of bytes of the header / ending to check that the data is read correctly.

The second way is to make it all a string, for example, separated by a comma or something like json. And then everything is simple.

The third way is to use protobuf or the like.

  • I apologize for the stupidity, but here’s the mechanism for getting out of the program, I don’t quite understand how to realize it - Andrei Sosnovsky
  • I ran into such a problem. If the matrix is ​​not large, say 27 * 27 everything works. But as soon as its size passes for 1300 * 1300. When transmitting through 1 socket to the server of two such matrices, one by one. The second does not come. What can there be solutions to the problem? - Andrei Sosnovsky
  • To begin to rewrite the comment so that it would be clearer. And then the second sentence is something aside. Then take the code that reproduces the problem and create a new question - KoVadim
  • created a new topic - Andrey Sosnovsky

I almost went that way. Since the program considers only a square matrix. And we know initially the number of rows = the number of columns. Then I decided in each posting not to forward the size of the string, but to send the string directly.

Code sent from the client:

 bool sendMatrix(SOCKET socket, int masLength, vector<vector<float>> matrix) { bool result = true; for (auto& x : matrix) { send(socket, (char*)x.data(), x.size() * sizeof(float), 0); } return result; } 

Server side code:

 vector<vector<float>> recvMatrix(SOCKET socket) { vector<vector<float>> matrix; matrix.resize(masLength); for (int i = 0; i < masLength; i++) { matrix[i].resize(masLength); recv(socket, (char*)matrix[i].data(), sizeof(float)*masLength, 0); } return matrix; } 

The masLength parameter is received first on the server. This is exactly the number of lines. On the server using this parameter and create the necessary dimensions for the vector.