I am doing networking programming in C\C++ and the assignment asks me to send a formatted message.
The format is below:
bits: 0.....15 16....31
update# port#
Sever-IP
As you seen first line ask me to:
-
Convert int into 16 bits so 2 ints make up 32 bits.
-
Convert xxx.xxx.xxx.xxx into 32 bits.
I am using UDP, so I need get these info into a char[], and that is the hard part. In my program, I have the update# and port # as int, and server IP address as a string. How could I convert them into these bits?
Here is what I have tried:
-
Casting them into char. But, I need get one int into two char.
-
Convert them into a string, and then string.c_str(), but it gives me a 4 bytes pointer.
The easiest way to do this is to create a struct for your header like this:
And use it like this:
*To convert the ip string to the unsigned long refer to inet_addr().