Quick TCP question...

BlitzPlus Forums/BlitzPlus Beginners Area/Quick TCP question...

Sauer(Posted 2009) [#1]
What's the best way to speed up a TCP connection? I have the game running correctly on a variety machines, but it's very slow... I have three theories:

1. Use less Read/Write commands
2. Send smaller data (bytes instead of strings)
3. Send/recv data less often

For my initial prototype I used all strings and WriteLine, which I knew would be costly for performance but was the easiest to work with. I switched some of the data that was below 255 to a WriteByte, but will this improve performance?

Currently I use about 10-15 Read/Write commands per iteration of the game. Perhaps I should write a parser and combine some of the elements into a single command?

And I was reading something about UDP on the forums that suggested to send data every ten millisecs... should I do this?

Which is the best? Or is it all of the above?


Matty(Posted 2009) [#2]
I've done some multiplayer game programming using UDP and a small amount with TCP.

Some queries first:
Are you working on an action game (ie frequent updates from server->client - lots of net traffic)? (UDP usually more suitable but TCP can be used)
Are you planning on this game working only on a LAN or also on the Internet?
Is it a turn based game or other slower paced game?

Difficult to determine what is best for your situation without knowing some of the answers to these questions.

Regardless of TCP/UDP being used - you should not be expecting the Internet to handle messages being sent every frame if you are running at a frame rate of 30/60 frames per second...it's just too quick.

Try to keep message lengths to less than 500bytes is something I've heard regardless of UDP/TCP.

If you are doing an action game then it can be worth compressing the data before sending...and a very easy way for example is to use the appropriate sized 'element' per data type. For example don't use a 4 byte float to store an angle that goes from 0-360, instead scale it back to 0-255 and use a byte, discarding values after the decimal point.

That's a couple of ideas.


Sauer(Posted 2009) [#3]
I have already made sure I use bytes/ints/string data types according to necessity, but I didn't notice a huge difference. I guess the next thing is to try to send/receive on not every frame.

I should probably use UDP... but the point of this game was to hone my TCP skills.

But is it ok if I have multiple write commands, like:
WriteByte(out, HP)
WriteByte(out,STR)
WriteLine(out,item$)
WriteByte(out, room)
WriteByte(out,att)
WriteLine(out,monster$)
WriteByte(out, level)
WriteByte(out,xp)
WriteLine(out,player$)


Hmm perhaps WriteString would speed things up too?

Sorry this post has gotten a bit incoherent. And thanks for replying, I wasn't sure anyone was going to.


Matty(Posted 2009) [#4]
Like I said, if you are doing an action game or fast paced game you don't want to send on every single frame ie 60 messages per second for example would be too many.

If you convert your ints/floats/shorts/bytes to their string representation (and I don't mean sending 1.234 as "1.234"*) then you can combine all your messages each frame into a single message, or split it per 500bytes. That way you are only sending a small number of packets from your server.

* - for example to convert a float into a string (of 4 bytes in length) you would create a bank of 4 bytes, write the float to the bank, then retrieving the bytes 1 at a time and adding them to the string to be sent.

Can I ask what you are trying to create?


Sauer(Posted 2009) [#5]
If you convert your ints/floats/shorts/bytes to their string representation (and I don't mean sending 1.234 as "1.234"*)


Yeah, I know what you're talking about, similar to Python's struct module . I'll try to write a packer I guess.

Can I ask what you are trying to create?


I'm making a Multi User Dungeon (MUD) but its more of a learning experience than a serious game idea. Having said that, I did play it with a friend and it was rather fun, so I may continue working on it.

UDP would probably be the best bet, as the server is constantly sending information about what items, monsters, and players are in the room as well as player stats. Just about everything is server side too... but I think it is feasible with TCP if I write a good packer and send data less.