-
Sockets and buffer size
I'm writing a C++ app that uses sockets and I have no problem sending an recieving, but am having trouble separating the differnt types of messages i'm sending.
I have several classes from which I need to send data using overloaded stream operators.
when I say
Code:
socket << myObject;
...
operator<<(Socket &socket, myObject mo) {
socket << mo.myInt; // 0
socket << mo.myString; // "hi world"
}
I recieve this: "0hi world", but I want to recieve "0", then "hi world".
Is there any way to force the sockets to read and write in sync? without encoding value delimiters or forceing fixed-width data.
I was thinking encoding delimiters may be more efficient at least on the per-object transmissions, but there will still be a point when I need to force read and write to sync between sending objects. (this is also multi-threaded with lots of user input jumping around)
fixed-width data is out of the question becase there are so many types of clasess with so many types of data that need to be sent.
Thanks!
-
Thanks. I will probably use some kind of encoding to format each object, but I am still concerned that more than one object will read/write at a time.
There should be some way to signal the buffer dynamically, rather than specifying by fixed width.
The problem is I dont know these sockets too well. And I dont know if its being buffered on the sending or recieving end. Is there somthing I can set on the socket for this?
I've looked through the <sys/socket.h> header and around on Google, but I couldn't find anything to describe in detail what i'm looking for.
Any suggestions?