views:

82

answers:

4

Hey guys, Iv'e noticed that when I send a complete packet (collect it's data in a buffer and send) it is much slower than sending the packet byte by byte. Will it be okay if I make an online game using this method?

A: 

That sounds bizarre. There is much more overhead in sending data byte by byte. Your transport headers will far outweigh the payload! Not to mention O(n) send calls (where n is the number of bytes).

You're doing something wrong if that's what you experience.

Alex
Slowness is possibly about the collection of data, not the sending of it?
Tony van der Peet
Possibly, he didn't state what part was slow. And sending byte by byte is almost never the solution.
Alex
+1  A: 

I think you need to define what are your measurement points (what exactly are you measuring). By the way is this TCP or UDP?

Anyway Winsock has its own internal buffers that you can modify by calls to setsockopt.

Dan Cristoloveanu
A: 

What I didn't really measure anything, I'm pretty sure it has something to do with sending data and not collecting it.. I'm using C# for server-side and C++ for client side, in the server side I wrapped the socket with a BinaryWriter and BinaryReader, and in the client I just used send and recv to send every byte.

Tamir
Edit the main question and add this.
Martin York
+1  A: 

Sounds like a naggling-related problem.

You have to disable naggling for latency-demanding applications. (See setsockopt, TCP_NODELAY).

Explanation:

TCP stack behaves differently for small chunks, trying to combine them in bizare ways on the way to IP datagrams. This is a performance optimization suggested by J.Nagle (hence nagling). Keep in mind that enabling NODELAY will make every send() call a kernel-mode transition, so you may wish to pack streams into chunks yourself by means of memory copying, before feeding them into send() if performance is an issue for what you are doing.

Pavel Radzivilovsky
I don't believe that this behavior should be termed "bizarre".
jldupont
The "bizarreness" is in the assumption: TCP has a human at one of the ends. It was invented at the old times Telnet was a major part of the communication. I believe this shouldn't have been a default. It is a dangerous pitfall I've seen many time-critical systems falling into.
Pavel Radzivilovsky