Also, do -NOT- count on your header coming through in one piece. Don''t even count on that first "message size" data coming through in one piece. You could get a one-byte packet, which is not large enough to extract the message size (in my example, anyway). Thats what the buffer and size checks are for.
You may have already known this, but someone may have missed it.
Winsock: How to send a struct?
Note that the alignment problem discussed above will only occur if the server and client executables are built with different compiler options or on different machines, and may not even appear with those conditions met, though it''s still a good idea to be aware. Problem three in a Winsock Programmer''s FAQ article (http://tangentsoft.net/wskfaq/articles/effective-tcp.html) discusses structure padding and the compiler keywords (for Visual C++ and Borland) you can use to eliminate the extra space. Otherwise you could send the struct members one at a time to transparently sidestep the problem.
RapscallionGL - arriving soon.
RapscallionGL - arriving soon.
________________________________________________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
November 27, 2002 12:42 AM
I''m not sure if this makes a difference in WinSock, but when I''ve seen structs sent in code, each field is first made from host order into network order, then sent. On a recieve, translate from network order to host order. This affects the data''s endian (little endian or big endian). I think big endian means big numbers come first, so 100 > 001 on a big endian machine; 100<001 on a little endian machine. Whatever it''s called, Windows the same one for host order and network order. This makes a difference if you''re using another OS. This is one of the few differences and contstraint to using Windows. Research and use hton* functions. It will make your network code platform neutral. There''s no need for Windows specific code here.
About the C/C++ structs, here''s something I''ve been toying with (the syntax is probably wrong but anyhow).
typedef struct {
int x,y;
unsigned flags;
union {
char * ptr;
}
} CHANGEPOSITION;
typedef struct {
float angle;
unsigned flags;
union {
char * ptr;
}
} CHANGEANGLE;
This allows you to typecast either a CHANGEPOSITION or CHANGEANGLE packet into a char * in send() and recv(). Your buffer will likely be a char buffer[MAXBUFFSIZE] anyhow, so Bob''s yer uncle!
(Yes the synax is likely wrong, please no whining!)
About the C/C++ structs, here''s something I''ve been toying with (the syntax is probably wrong but anyhow).
typedef struct {
int x,y;
unsigned flags;
union {
char * ptr;
}
} CHANGEPOSITION;
typedef struct {
float angle;
unsigned flags;
union {
char * ptr;
}
} CHANGEANGLE;
This allows you to typecast either a CHANGEPOSITION or CHANGEANGLE packet into a char * in send() and recv(). Your buffer will likely be a char buffer[MAXBUFFSIZE] anyhow, so Bob''s yer uncle!
(Yes the synax is likely wrong, please no whining!)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement