3 Replies - 156 Views - Last Post: 04 December 2017 - 03:30 PM Rate Topic: -----

#1 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 149
  • Joined: 26-August 12

Converting char* to std::vector<char>

Posted 04 December 2017 - 02:23 PM

Hello,

I'm working on receiving data using sockets. The way I'm formatting packets is:

byte 0: data length (0-255)
byte 1: packetID (used to construct packet classes)
2+: data contained in the packet.

Then, the code will initialise the correct packet type and populate its data using bytes 1 and 2+.

The issue I'm having is allocating the correct amount of space in a char array depending on the data length byte. The compiler is complaining that I have to use a constant value, so I've used std::vector<char> instead, but now I cannot cast from char*.

char length_byte[1];
int read = recv(clientSocket, length_byte, 1, 0); // read length byte
Sleep(10);

char packetID_byte[1];
read = recv(clientSocket, packetID_byte, 1, 0); // read packetID byte
Sleep(10);

int data_length = length_byte[0];

std::vector<char> data_bytes(data_length);
read = recv(clientSocket, data_bytes, data_length, 0); // read data <-- * ERROR on data_bytes *
Sleep(10);



While I'm on the topic, this is for a game server. I'm concerned that the Sleep(10)s will effectively increase the client's latency by 30ms, which is unacceptable. Is there a better approach?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting char* to std::vector<char>

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5952
  • View blog
  • Posts: 20,405
  • Joined: 05-May 12

Re: Converting char* to std::vector<char>

Posted 04 December 2017 - 02:43 PM

Pass the address of data_bytes[0] for older version of C++, or use C++11's std::vector<T>::data() method.
Was This Post Helpful? 0
  • +
  • -

#3 Rixterz  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 149
  • Joined: 26-August 12

Re: Converting char* to std::vector<char>

Posted 04 December 2017 - 03:07 PM

I now have:

std::vector<char>* data_bytes(data_length);
recv(clientSocket, data_bytes->data(), data_length, 0); // read data



but the compiler is moaning again:

a value of type "int" cannot be used to initialize an entity of type "std::vector<char, std::allocator<char>> *"


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5952
  • View blog
  • Posts: 20,405
  • Joined: 05-May 12

Re: Converting char* to std::vector<char>

Posted 04 December 2017 - 03:30 PM

Why did you make a pointer to a vector? Just use the local instance.

Simply use the dot operator:
std::vector<char> data_bytes(data_length);
recv(clientSocket, data_bytes.data(), data_bytes.size(), 0); // read data



Or for older C++:
std::vector<char> data_bytes(data_length);
recv(clientSocket, &data_bytes[0], data_bytes.size(), 0); // read data


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1