R
R
rtur2013-12-23 17:05:02
C++ / C#
rtur, 2013-12-23 17:05:02

How to read data into a variable (let it be an integer unsigned type, 2 bytes in size)?

Suppose you need to read data into a variable (let it be an unsigned integer type, 2 bytes in size).
How correct is it to do this:

unsigned short int packet_size = 0; //2 байта
recv(sockfd,(char*)&packet_size,2,MSG_WAITALL); //читаем в переменную


and can there be pitfalls?

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leclecovich, 2013-12-23
@rtur

Among the pitfalls may be the following:
1 The socket must be opened before and closed after
2 Calling recv with the MSG_WAITALL parameter is blocking, i.e. if only one byte arrives, then the calling thread will be blocked until the next
3 byte order arrives. Most likely it will be LE, but you never know...
4 Data is accepted from any source. If you need otherwise - recvfrom(...)
5 Under Windows, there are restrictions on the operation of sockets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question