Unix socket programming

Status
Not open for further replies.
Since you guys are so helpful, I would like to ask another question :D

In this program that I am writing, I have a 16-bit sequence number as well as a CRC-16 checksum that will be sent along with the data.

I am having trouble finding a suitable container to hold that 16-bit sequence num. I know that in visual studio, the type "short int" is 16-bits long, but is it implementation dependent? Or is the size fixed in the C standard?

GNU C library has several int types of fixed sizes (int16_t in particular), should I use it instead of "short int"?

Thanks once again :)
 

John S

Ars Scholae Palatinae
1,464
Subscriptor
Last I checked the standard made no mention of the size of a "short int", tends to be vague like that for maximum portability. The size in VS is only set at 16 cause there's only really one platform for Windows (although they'll be making forays into 64-bit platforms again w/ Itanium and probably x86-64). Anywho, a uint16 should work for your purposes, moreso than it's signed variant, I'd think.
 

Gorth

Ars Praefectus
4,336
Subscriptor
Originally posted by azure:
Have you tried an array of two unsigned chars? I like to read in the info from the packet in to an unsigned char array, and then parse it out into my local stucts. Far from zero copy, but it makes converting endiness, and other localizations easy.

I agree. Use: char crc[2]; Then you can do all your fun ANDing and ORing to get and check the right values...
 
Originally posted by LoneGumMan:

In this program that I am writing, I have a 16-bit sequence number as well as a CRC-16 checksum that will be sent along with the data.
Originally posted by Gorth:
I agree. Use: char crc[2]; Then you can do all your fun ANDing and ORing to get and check the right values...
The char crc[2] will work for ANDing and ORing, but don't try to declare the sequence number like that. When you do math on a signed number, such as adding one, you will get weird answers.

Use unsigned char seq[2].
 
I know i can read in CRC and sequence no. from the byte stream using one char, one int, and some bit shifting...so no problem there.

However, when I compute CRC and seq. no., using char[2] in calculations ain't too simple. ANDs and ORs with CRC are ok, they are just bit operations afterall. However, incrementing seq. no. held in an unsigned char[2] involves way too much checkings (bit won't overflow to high byte once seq. no. exceeds 255)

I am now thinking of doing everything on an unsigned int, and just memcpy() the low 2 bytes to my packet (basically a char[]). I know it is crude, but seems easier than unsigned char[]
 

earl

Well-known member
3,066
Um, Prototyped's suggestion is completely necessary. If you are developing on x86, then none of your numbers are network safe until the byte ordering is switched. Which means that nothing will be able to interpret your packet -- as all good software on x86 will localize received packets assuming you "networkized" them, leading to corrupted data.

Edit:
err, which shouldn't be a problem unless you expect your software to interact with anything else. And if this is for a class, your professor should strike lots of points for failing to obey network byte ordering.

-earl-
 
I am writing a sender/receiver pair that uses UDP socket to send/receive packets, while ensuring reliable transfer (re-transmission on timeout and the like). The transfers are all done between two programs.

Since the entire packet (CRC, seq and all) is for my own interpretation on both end, network byte order shouldn't matter, right? :confused: But for completeness sake, I shall do it anyway, it is only one function call afterall.
 

Prototyped

Ars Legatus Legionis
11,126
If you build your source on a little-endian (e.g. x86) and a big-endian (e.g. SPARC) system and want the software to interoperate between computer architectures, you'll need byte order translation, even if the fields are for your internal use. Besides which, anything that is going to be seen by a system with a different architecture (including persistent data stores like disk files) ought to be serialized and stored in a standardized byte order.
 
hmm...I see.

I have always wondered why the "save" function in MFC is called Serialize instead of some form of "save". It kinda makes sense now.

This brings up another question. If I call write() to put a structure of 100 bytes (say, a structure containing only char[100]) into a file, shouldn't the 100 bytes be ordered exactly as it is layout in the sructure (plus a few padding bytes perhaps) ? Is this where the read() write() functions in C abstracts away the internal structure of the system? So conceptually I would think that the bytes are layout as I send them, but in reality they might be stored in reverse order.
 
Status
Not open for further replies.