Rowan836How would you send and receive 'double's or 'int's across a socket because the functions send and recv only accept 'char *' buffers? |
It depends on how general you want to be. If the two endpoints are on the same kind of hardware, you can get away with passing the address of the values. For example:
1 2
|
double d = f();
send(s, (char*)&d, sizeof(d), 0);
| |
and
1 2
|
double d = 0.0;
recv(s, (char*)&d, sizeof(d), 0);
| |
If you want a platform independent solution, you need some coding scheme that deals with float formats and byte ordering such as XDR,
http://en.wikipedia.org/wiki/External_Data_Representation
EssGeEichFrom what i know, a bool uses 1 bit of size. |
bool was introduced to allow overloading. It wasn't introduced to save space. It can be, and often is, the same size as an int.
tofiffelets say i'd need to send a string or at least a char*, those are terminated by \0, how could you send that? |
TCP is a stream oriented protocol. That means there is no record marker. Data transmitted with multiple sends may turn up in a single recv and vice versa. It also means you have to provide your own markers in the stream to seperate records. You may send the length of a string first, or interpret what you've received and treat a null as an end of field marker. For example HTTP uses two consequetive end of line markers to indicate end of record. You have to devise your own scheme and stick to it.
If you're sending whole structures with a fixed and portable layout, you need to guarantee that the alignment is the same on the sender and receiver, byte alignment is ideal because you're not sending packing noise along with your data.
Finally, don't use CreateThread, use _beginthreadex instead.
http://www.cplusplus.com/forum/general/61879/