views:

791

answers:

4

Hello,

I have to send mesh data via TCP from one computer to another... These meshes can be rather large. I'm having a tough time thinking about what the best way to send them over TCP will be as I don't know much about network programming.

Here is my basic class structure that I need to fit into buffers to be sent via TCP:

class PrimitiveCollection
{
    std::vector<Primitive*> primitives;
};

class Primitive 
{
    PRIMTYPES primType; // PRIMTYPES is just an enum with values for fan, strip, etc...
    unsigned int numVertices;
    std::vector<Vertex*> vertices;
};


class Vertex
{
    float X;
    float Y;
    float Z;
    float XNormal;
    float ZNormal;
};

I'm using the Boost library and their TCP stuff... it is fairly easy to use. You can just fill a buffer and send it off via TCP.
However, of course this buffer can only be so big and I could have up to 2 megabytes of data to send.

So what would be the best way to get the above class structure into the buffers needed and sent over the network? I would need to deserialize on the recieving end also.

Any guidance in this would be much appreciated.

EDIT: I realize after reading this again that this really is a more general problem that is not specific to Boost... Its more of a problem of chunking the data and sending it. However I'm still interested to see if Boost has anything that can abstract this away somewhat.

+2  A: 

Without anything fancy, from what I remember in my network class:

  • Send a message to the receiver asking what size data chunks it can handle
  • Take a minimum of that and your own sending capabilities, then reply saying:
    • What size you'll be sending, how many you'll be sending
  • After you get that, just send each chunk. You'll want to wait for an "Ok" reply, so you know you're not wasting time sending to a client that's not there. This is also a good time for the client to send a "I'm canceling" message instead of "Ok".
  • Send until all packets have been replied with an "Ok"
  • The data is transfered.

This works because TCP guarantees in-order delivery. UDP would require packet numbers (for ordering).

Compression is the same, except you're sending compressed data. (Data is data, it all depends on how you interpret it). Just make sure you communicate how the data is compressed :)

As for examples, all I could dig up was this page and this old question. I think what you're doing would work well in tandem with Boost.Serialization.

GMan
+2  A: 

I would like to add one more point to consider - setting TCP socket buffer size in order to increase socket performance to some extent.

There is an utility Iperf that let test speed of exchange over the TCP socket. I ran on Windows a few tests in a 100 Mbs LAN. With the 8Kb default TCP window size the speed is 89 Mbits/sec and with 64Kb TCP window size the speed is 94 Mbits/sec.

skwllsp
A: 

In addition to how to chunk and deliver the data, another issue you should consider is platform differences. If the two computers are the same architecture, and the code running on both sides is the same version of the same compiler, then you should, probably, be able to just dump the raw memory structure across the network and have it work on the other side. If everything isn't the same, though, you can run into problems with endianness, structure padding, field alignment, etc.

In general, it's good to define a network format for the data separately from your in-memory representation. That format can be binary, in which case numeric values should be converted to standard forms (mainly, changing endianness to "network order", which is big-endian), or it can be textual. Many network protocols opt for text because it eliminates a lot of formatting issues and because it makes debugging easier. Personally, I really like JSON. It's not too verbose, there are good libraries available for every programming language, and it's really easy for humans to read and understand.

One of the key issues to consider when defining your network protocol is how the receiver knows when it has received all of the data. There are two basic approaches. First, you can send an explicit size at the beginning of the message, then the receiver knows to keep reading until it's gotten that many bytes. The other is to use some sort of an end-of-message delimiter. The latter has the advantage that you don't have to know in advance how many bytes you're sending, but the disadvantage that you have to figure out how to make sure the the end-of-message delimiter can't appear in the message.

Once you decide how the data should be structured as it's flowing across the network, then you should figure out a way to convert the internal representation to that format, ideally in a "streaming" way, so you can loop through your data structure, converting each piece of it to network format and writing it to the network socket.

On the receiving side, you just reverse the process, decoding the network format to the appropriate in-memory format.

My recommendation for your case is to use JSON. 2 MB is not a lot of data, so the overhead of generating and parsing won't be large, and you can easily represent your data structure directly in JSON. The resulting text will be self-delimiting, human-readable, easy to stream, and easy to parse back into memory on the destination side.

swillden
Wow, lot of information in here. I was going to upvote you for the comment to separate the protocol serialization from the in-memory representation, but... JSON, for what amounts to a big blob of scientific data? No way, man.
Tom
For 2 MB, there's no point in doing anything else. So it expands to 5 or 6 MB, so what? You're still talking a tiny fraction of a second to transfer it. JSON is easier and safer than mucking around with binary formats. In many cases, piping it through a compressor/decompressor will result in a smaller transfer size than a binary format anyway. If we were talking about large volumes of data, I'd give a very different answer, but for this, make it easy.
swillden
+1  A: 

Have you tried it with Boost's TCP? I don't see why 2MB would be an issue to transfer. I'm assuming we're talking about a LAN running at 100mbps or 1gbps, a computer with plenty of RAM, and don't have to have > 20ms response times? If your goal is to just get all 2MB from one computer to another, just send it, TCP will handle chunking it up for you.

I have a TCP latency checking tool that I wrote with Boost, that tries to send buffers of various sizes, I routinely check up to 20MB and those seem to get through without problems.

I guess what I'm trying to say is don't spend your time developing a solution unless you know you have a problem :-)

--------- Solution Implementation --------

Now that I've had a few minutes on my hands, I went through and made a quick implementation of what you were talking about: http://boost.teeks99.com/temp/DataChunker.zip There are three big parts:

The serializer/deserializer, boost has its own, but its not much better than rolling your own, so I did.

Sender - Connects to the receiver over TCP and sends the data

Receiver - Waits for connections from the sender and unpacks the data it receives.

I've included the .exe(s) in the zip, run Sender.exe/Receiver.exe --help to see the options, or just look at main.

More detailed explanation: Open two command prompts, and go to DataChunker\Debug in both of them. Run Receiver.exe in one of the Run Sender.exe in the other one (possible on a different computer, in which case add --remote-host=IP.ADD.RE.SS after the executable name, if you want to try sending more than once and --num-sends=10 to send ten times). Looking at the code, you can see what's going on, creating the receiver and sender ends of the TCP socket in the respecitve main() functions. The sender creates a new PrimitiveCollection and fills it in with some example data, then serializes and sends it...the receiver deserializes the data into a new PrimitiveCollection, at which point the primitive collection could be used by someone else, but I just wrote to the console that it was done.

teeks99
Yeah I guess I'm trying to use the Boost TCP stuff as efficiently as possible... there don't seem to be many examples using it effectively.
Polaris878
Now that I've had a few minutes on my hands, I went through and made a quick implementation of what you were talking about:http://boost.teeks99.com/temp/DataChunker.zipThere are three big parts:The serializer/deserializer, boost has its own, but its not much better than rolling your own, so I did.Sender - Connects to the receiver over TCP and sends the data Receiver - Waits for connections from the sender and unpacks the data it receives. I've included the .exe(s) in the zip, run Sender.exe/Receiver.exe --help to see the options, or just look at main.
teeks99