-
Send a numeric
Hi to all,
I need to send from server to client a numeric information. If the info is a string, I write
send(newsocketfd, MyMsg, sizeof(MyMsg),0);
If is numeric how can send the value? for example:
"The number of students are: " 10
Thank You and Best Regards
Nick
-
Send the string "10" ?
It's simple to implement, portable, and easy to trace using network debug tools.
You could do
send( newsocketfd, &myInt, sizeof(MyInt),0);
but there's a whole "can-o-worms" of potential problems with that, like what endian the machines use and how big an int is.
-
typedef's can help with the size issue. Setup a header with some #ifdef's to select the appropriate size on each platform you need to compile for. (There are a couple in stdint.h which is available on many systems - uint8_t, uint16_t, etc.)
After you've got size accounted for, there are some functions to help with endianness: htons() and htonl(), ntohs(), ntohl(). (Host-to-Network-short/long and Network-to-Host-short/long) So, on the sending size, use htons/l() and on the recv'ing side use ntohs/l(). See the man pages for more info.
As Salem partially hints, your question is vague as to how you intend to send your data. You can either send 10 in binary (using either 2 or 4 bytes) or you can send the text "10". Which are you trying to accomplish?