Z
Z
Zakhar Alekseenko2014-07-15 12:36:05
C++ / C#
Zakhar Alekseenko, 2014-07-15 12:36:05

How to generate a set of bytes to send via a Windows socket?

Hello.
I work with the device using the SCPI protocol. Unfortunately, it is not implemented in the device according to the standard and requires each message to be preceded by its length. This is not described in the documentation, so I had to get to this with the help of a sniffer.
The sniffer tells me that the original program from the device manufacturer sends the message length in this format
Send: Return Code: 0x00000000
00000000 00 00 00 06
Copied from the SocketSniff program. Those. in 8 byte format.
Now the question is how to form char arr[] so that these 8 bytes would be in it.
I tried doing like this

std::stringstream ss;
ss << std::hex << 0x0000000000000006;
printf("x=%s\n", ss.str().c_str());

It turned out
Send: Return Code: 0x00000000
6
Tried
char ss[4] = {0};
sprintf(ss,"%16X",6);
printf("ss=%s\n", ff);

It turned out
It turned out
Send: Return Code: 0x00000000
0000000000000006
The device did not react to such recording methods.
Thus, the following entry is required
Send: Return Code: 0x00000000
00000000 00 00 00 06
So far I have not been able to form and send the required set of bytes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xandox, 2014-07-15
@Zaher220

std::uint64_t length = GetMessageLength(); // ну тут наверное разбирешся
length = host_to_bigendian(length); // смотри для своей платформы как перевести в big endian
const char* lengthBytes = static_cast<const char*>(&length); // в памяти все и так как нужно уже лежит 
send(socket, lengthBytes, sizeof(length));

B
bambrman, 2014-07-15
@bambrman

apparently, you are passing a string, i.e. not 8 bytes but 20,
you can form like this for example
char ss[20];
sprintf(ss,"00000000 00 00 00%.2X",6);
printf("ss=%s\n", ss);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question