wxDatagramSocket how obtain count of recived data bytes? Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
Dmitry_K
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Nov 04, 2021 3:33 pm

wxDatagramSocket how obtain count of recived data bytes?

Post by Dmitry_K »

Hello.

I am new to this topic. My problem is how do I get the number of bytes received? When a device sends a data packet, I don't know the number of bytes.
I have the ability to set the number of bytes manually, like this.

char* buf;
buf = (char*)malloc(500);

M_UDP_socket->RecvFrom(raddr, buf, 100);
or
M_UDP_socket->RecvFrom(raddr, buf, 200);

But the package can be larger or smaller. What to do? It's not so scary if it is smaller, it will be possible to extract useful data, but what if it is larger?

What I would like is to first find out the number of bytes received and then rewrite them into the buffer.

For example like this.

wxUint32 data_length = M_UDP_socket->GetLastIOReadSize();

buf = (char*)malloc(data_length);

M_UDP_socket->RecvFrom(raddr, buf, data_length);

Does the socket have a method that returns the currently received number of bytes in the buffer?
In the documentation, I could not find, experiments did not give anything meaningful.


Regards, Dmitry.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: wxDatagramSocket how obtain count of recived data bytes?

Post by T-Rex »

My problem is how do I get the number of bytes received?
wxDatagramSocket is derived from wxSocketBase that has LastReadCount() method.
Dmitry_K
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Nov 04, 2021 3:33 pm

Re: wxDatagramSocket how obtain count of recived data bytes?

Post by Dmitry_K »

Helo.
Thanks for the answer.
The method you suggested works, only it need to read the data first. And I would like to know the volume before reading. Before calling:

M_UDP_socket->RecvFrom(raddr, buf, data_length);

I want to know how much memory needs to be reserved (malloc).

If I understood the documentation correctly, the socket cannot give information about the number of bytes received without performing the read procedure. Is it so?

Regards, Dmitry.
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: wxDatagramSocket how obtain count of recived data bytes?

Post by T-Rex »

You should not allocate the entire memory in advance because the size of UDP packet is 64k and you don't know in advance how much memory you need while you are reading the current packet. If you have sent 5M of data you may get one or more packates placed to the queue and become available for reading while you are processing the first packet. You can use the fixed chunk size for reading and wxMemoryBuffer for storing the whole data until you get the reading queue empty. You can append the chunk to the memory buffer with AppendData(). Don't forget to use the actual number of bytes read instead of the whole size of the preallocated chunk.
Dmitry_K
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Nov 04, 2021 3:33 pm

Re: wxDatagramSocket how obtain count of recived data bytes?

Post by Dmitry_K »

Helo.
Thanks for the answer.
I think so far the question is more or less closed.

Regards, Dmitry.
Post Reply