0

I wonder which data type is the most suitable for representing bytes I send via Windows API winsock.

I'm currently working on receiving and sending data frames on network sockets, as well as a CAN data stream. Basically I'm receiving data on a CAN stream from a measurement system (eg. bloodpressure, ecg..). Because of the CAN stream API I rely on, the data is received in BYTE (typedef unsigned char BYTE in Windows API minwindef.h).

Now, I'm looking to send the bytes via winsock2 API function WSASendTo, which expects the type LPWSABUF (char*), defined in winsock2.h.

Between receiving (from CAN stream) and sending (with WSA API), I'm doing several operation on the bytes buffer in order to create a suitable frame that can be sent. This includes "cutting" the received buffer, basically splitting the buffer using only payload bytes (e.g. bytes 3-7) and id bytes (e.g. bytes 1-2).

What kind of data type should the byts during extraction of information (creation of suitable frame with id and payload) have? I'm thinking of:

  • char*
  • std::vector<std::char>
  • std::unique_ptr<byte>
  • std::vector<std::byte>
  • BYTE from rpcndr.h
5
  • 2
    I usually use std::uint8_t and std::vector<std::uint8_t>, since that's guaranteed to be unsigned and 8 bits (char can be either signed/unsigned depending on the platform). Instead of using the winapi, you will get a much better C++ experience if you use boost::asio instead. (The Winapi to be honest is a bit of beast... and more oriented toward "C" anyway) Commented Nov 2, 2023 at 11:24
  • @PepijnKramer thanks for the advise, I'll take a look at boost::asio. So far, I had a fairly okay experience with winsock, although this whole multithreading with OVERLAPPED is more confusing that it should be IMHO. Commented Nov 2, 2023 at 11:26
  • Indeed the async model of boost is a lot easier to use. Commented Nov 2, 2023 at 11:27
  • 2
    dont confuse a pointer with where it points to. In your list you mix up pointers and actual data structures that store data Commented Nov 2, 2023 at 11:39
  • Why was this downvoted? A simple edit will make this question meet the requirements of SO. This is a good question. Commented Nov 2, 2023 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.