Blitz TCP\UDP

Blitz3D Forums/Blitz3D Programming/Blitz TCP\UDP

xtremegamr(Posted 2009) [#1]
I'm trying to make a simple net-game in B3D. It's a 2-player game, so while testing it, I have to run the server program, and the client program twice.

The problem is, once the 2 clients connect to the server program via TCP and UDP, they cheerfully procede to eat up all of my CPU cycles, and slow my computer to a crawl. I've already tried using CPU breathing code on both the client programs and the server program (it hasn't worked).

My guess is that I need use a 3rd party userlib for TCP and UDP. Any suggestions? I want something fast and free, if that's not too much to ask. :)


Midimaster(Posted 2009) [#2]
did you add a command "delay 5" in the main loop of both programs right before "flip"?

This may help, because it jumps back to the operation system for 5 msecs each flip-circle and gives the operation system the chance to change to the other player task


Rroff(Posted 2009) [#3]
Try to avoid TCP as much as possible for game stuff - I notice it will quite often lockup the task for periods of time (longer than useable for game frame updates) using 100% CPU while doing whatever its doing.

UDP shouldn't be a problem... but you will need to create your own routines to make sure a packet is recv'd ok.


S_Seth(Posted 2009) [#4]
I haven't had a problem with UDP, its pretty easy once you learn it

One of the 3d examples has nice code that maxes the FPS to 30 also so it doesn't eat as much CPU


Mahan(Posted 2009) [#5]
As usual i recommend the eNet userlib.

fast, free, based on UDP. allows for packages with guaranteed delivery etc.

This is imho the best solution there is if you want semi-low-level networking for gaming.

Also enet handles package-sizing on it's own. It does not matter if you send 1000 packages of 1 byte each or if you send 1 x 1000 byte package, enet will manage the actual sending / receiving on its own and you will get exactly what you sent on the other end. (i.e. it has the ability to "re-package" the users packages to more suitable sizes that are better for network transfer)

http://blitzmax.com/Community/posts.php?topic=49076

edit: clarification of the packaging.


xtremegamr(Posted 2009) [#6]
@Midimaster:
I tried adding a delay 5, and it didn't work. I tried some CPU breathing code instead, but that didn't work either. :(

@everyone else:
The only problem with UDP is that it requires the Integer IP of the computer you are trying to send the message to. Right now, the only way I know how to do that is to open a TCP stream, exchange the integer IP's, then close the stream. And if my server program uses both TCP and UDP, I think it'll slow the program down even more (if that's even possible ;) ).

@Mahan:
eNet looks kinda cool; I'll take a look at it as soon as I solve my other UDP related issues.

Thanks for the help guys; it's very much appreciated.


S_Seth(Posted 2009) [#7]
How often are you sending data? It kinda sounds like you might be sending too much.

Granted how much data you send changes on the different type of game so let me just tell you what I do.

Say a user presses down a key to move, it sends the data packet over UDP that tells all others that they key is now down. When the user lifts the key, it sends the packet that the key is now up. I do this for all the WASD keys I use for movement.

But also due to lag, every 3 seconds I send the X,Y,Z, coordinates and the rotational values for correction.

This works fine for me for the 4 players I've tested with.


xtremegamr(Posted 2009) [#8]
@Seth:

Well, come to think of it, you're probably right. My client program runs at about 60 fps, and it sends the server information about client input (what WASD keys are down, is the spacebar down, etc.) once every loop. :)

When I get the chance, I'll make the changes to my code and see what happens.

Thanks for the help, guys!


RifRaf(Posted 2009) [#9]
Pseodo code to send one integer for all keys , only when a key state changes
OLDKEYCODE=0
Wkey_down=2
Akey_down=4
Skey_down=8
Dkey_down=16
spacebar_down=32
Wkey_up=64
Akey_up=128
Skey_up=256
Dkey_up=512
spacebar_up=1024
;somewhere also set constants for scancode key values

;reset the keycode
Keycode=0

If keydown(W_key) then keycode=keycode OR wkey_down else keycode=keycode OR wkey_up

If keydown(S_key) then keycode=keycode OR skey_down else keycode=keycode OR skey_up

If keydown(A_key) then keycode=keycode OR akey_down else keycode=keycode OR akey_up

If keydown(D_key) then keycode=keycode OR dkey_down else keycode=keycode OR dkey_up

If keydown(space_key) then keycode=keycode OR spacebar_down else keycode=keycode OR spacebarup_up

IF keycode<>oldkeycode then
 ;send the keycode across network here
 ;you can also add a time condition here , so yo dont send to frequently

 ;set the oldkeycode value to equal the current for the next comparison
 oldkeycode=keycode
endif