How to send non-'char*' information with winsock

Pages: 12
If you don't want to use threading, you may use non blocking sockets or if you use the select function you may then even use blocking sockets.
I've already used threads, but if I'd like to expand the program to take like 20 clients or more it would most probably cause lag.

About the blocking sockets, I've googled, and I came up with this:
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_4.shtm

this is pretty much just a constant stream to a single socket, did you mean something else?
given the probrem you asked is concerning type dependancy i would propose that if you are using unix char sa data this allows you to give the data a formart
Rowan836
How 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


EssGeEich
From 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.


tofiffe
lets 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/
Last edited on
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:

That question has already been solved, and even the last one you "answered".
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.

Maybe you mean a char? A int's size is 4 bytes, a char's size is 1 byte.
I learnt bool wasn't 1 bit these days, because "sizeof(char)" returns 1, and is an unsigned integer, so what should "sizeof(bool)" return? Being an integer it cannot be (1 / 8). So probably it's a byte, and not four.
Finally, don't use CreateThread, use _beginthreadex instead.

From what i know, if you use _beginthreadex, you cannot use WaitForSingleObject / WaitForMultipleObjects.
No, I didn't mean char. Logical operations are of type int in C (and early C++). You'll find that in many cases, bool is implemented as an int. So you can't make assumptions about its size.

_beginthreadex returns the thread handle. You have to cast the value to HANDLE of course.
Hm, sorry then.
Topic archived. No new replies allowed.
Pages: 12