Getting this error on Serializing a double value in the header

48 views
Skip to first unread message

Hassam Mughal

unread,
May 31, 2020, 1:15:06 PM5/31/20
to ns-3-users
Greetings everyone!

I am trying to use double value in header. But, this error is appearing. Can anyone guide in this regard? I have attached the files as well.

Thanks
Best Regards,
reply-packet-header.cc
reply-packet-header.h

Hassam Mughal

unread,
May 31, 2020, 1:25:21 PM5/31/20
to ns-3-users

Hassam Mughal

unread,
May 31, 2020, 1:52:35 PM5/31/20
to ns-3-users
I did the following changes in my code:
but still getting this error.

Tom Henderson

unread,
May 31, 2020, 1:54:16 PM5/31/20
to ns-3-...@googlegroups.com, Hassam Mughal
The error is what the error message suggests: You have set the
GetSerializedSize method to return 12 bytes, but you are trying to
serialize and deserialize more than 12 bytes.

- Tom

Hassam Mughal

unread,
May 31, 2020, 2:13:58 PM5/31/20
to ns-3-users
Thanks a lot, fixed the issue by changing the size instead of changing the whole code as others did. However, is it ok to use i.WriteHtonU64 instead of :

Tom Henderson

unread,
May 31, 2020, 2:54:43 PM5/31/20
to ns-3-...@googlegroups.com, Hassam Mughal
On 5/31/20 11:13 AM, Hassam Mughal wrote:
> Thanks a lot, fixed the issue by changing the size instead of changing
> the whole code as others did. However, is it ok to use i.WriteHtonU64
> instead of :

You are trying to write a floating-point double value into the header,
which is mainly set up to handle integers because that is what is
usually used in real protocols.

You could consider to maintain your variable as an integer, and use the
integer-based methods.

You could also use these methods in class Buffer to serialize a double
into the buffer:

/**
* \param buffer a byte buffer to copy in the internal buffer.
* \param size number of bytes to copy.
*
* Write the data in buffer and advance the iterator position
* by size bytes.
*/
void Write (uint8_t const*buffer, uint32_t size);

/**
* \param buffer buffer to copy data into
* \param size number of bytes to copy
*
* Copy size bytes of data from the internal buffer to the
* input buffer and advance the Iterator by the number of
* bytes read.
*/
void Read (uint8_t *buffer, uint32_t size);

using 'sizeof (double)' as the size. You would need to pass a pointer
to your member variable; something like this:

i.Write (&m_currProSpeed, sizeof (double));

Hassam Mughal

unread,
May 31, 2020, 3:04:57 PM5/31/20
to ns-3-users
Thanks for your explanation Tom.

Hassam Mughal

unread,
May 31, 2020, 3:15:39 PM5/31/20
to ns-3-users
However, if I am using the proper way then getting this following error:

Tom Henderson

unread,
May 31, 2020, 4:55:32 PM5/31/20
to ns-3-...@googlegroups.com, Hassam Mughal
On 5/31/20 12:15 PM, Hassam Mughal wrote:
> However, if I am using the proper way then getting this following error:
>

The compiler is telling you the error: you are trying to assign the
return value of Read() to a member variable, but Read () returns void.

- Tom

Hassam Mughal

unread,
May 31, 2020, 4:56:36 PM5/31/20
to ns-3-users
Yes, so how can we use these methods then?

Tom Henderson

unread,
May 31, 2020, 5:16:05 PM5/31/20
to ns-3-...@googlegroups.com, Hassam Mughal
On 5/31/20 1:56 PM, Hassam Mughal wrote:
> Yes, so how can we use these methods then?

Your member variable is updated by the Read() method because you passed
a pointer to it as an argument. No later assignment is needed.

- Tom

Hassam Mughal

unread,
May 31, 2020, 5:26:43 PM5/31/20
to ns-3-users
Thank you so much Tom. It's working perfectly. I have one question related to the other thread, does each application needs to run on different port number? or two applications can run on same port number on each interface? For example interface a is having port number 9 and interface b is having port number 80. So, if I run two applications, i.e. app A and app B. Do I need to give them a different port number? or I can use the same port number of interface a for running app A and app B on interface a and likewise.
Thanks
Reply all
Reply to author
Forward
0 new messages