TCP vs. UDP

Blitz3D Forums/Blitz3D Beginners Area/TCP vs. UDP

Nate the Great(Posted 2008) [#1]
I just wanted to know the ups and downs for TCP and UDP in an action game.


deps(Posted 2008) [#2]
TCP: The packets always arrive, and in order.
UDP: Maybe they arrive, or maybe they dont. And they might arrive in the wrong order. Who knows?

UDP is a bit faster, but if you are new to networking I would suggest TCP since it will save you a lot of problems. If you use UDP you will probably end up writing code to make sure the packets arrive in order, and maybe even make sure they arrive at all. (and TCP already do that...)


EDIT: And to answer the question:
In an action game you will probably need every packet, but you usually doesn't have to get them in the right order. If you go the UDP route, add a timestamp to your packet, and make the receiving end ignore packets that have an older timestamp than the last one it accepted.
I would use TCP. If you find/write some code that uses UDP and lets you specify if the packet should arrive in order/at all, or both, then by all means use it.


Nate the Great(Posted 2008) [#3]
ok i ran into a problem and here it is. Why doesn't the appolo x example not send two messages when you tell it to?

; This code is in two parts, and needs to be run seperately on the same machine

; --- Start first code set ---
; Create a server and listen for push

svrGame=CreateTCPServer(8080)

If svrGame<>0 Then 
Print "Server started successfully."
Else
Print "Server failed to start."
End
End If

rcvnextmsg = 0
While Not KeyHit(1)
strStream=AcceptTCPStream(svrGame)
If strStream Then 
Print ReadString$(strStream)
Else
Print "No word from Apollo X yet ..."
Delay 1000
End If 
Wend

End

; --- End first code set ---

; --- Start second code set ---
; Copy this code to another instance of Blitz Basic
; Run the above code first, then run this ... they will 'talk'

; Create a Client and push data

strmGame=OpenTCPStream("127.0.0.1",8080)

If strmGame<>0 Then 
Print "Client Connected successfully."
Else
Print "Server failed to connect."
WaitKey 
End
End If

; write stream to server
WriteString strmGame,"Mission Control, this is Apollo X ..."
WriteString strmGame,"Bye"
Print "Completed sending message to Mission control..."

; --- End second code set ---







BIG BUG(Posted 2008) [#4]
If you want to use TCP, use it via DLL, not the Blitzbasic variant.
In Blitzbasic TCP uses only "blocked" mode, which means every time there is a lag in the connection, your complete game is stopped, even the graphical part.
So the original TCP is only useful in fast and reliable networks which means LAN. But in this environment there won't be any packet loss with UDP either.

If you're an experienced programmer, you can go with UDP and handle possible packet loss by yourself. Otherwise you can use an external DLL which handles the network stuff...

----
Your code:
TCP builds up a steady connection only once. AcceptTCPStream is only used to accept new clients.
If you want to check if there is data available, use ReadAvail. EOF might work too.


Wings(Posted 2008) [#5]
heres a working example of using TCP/IP with blitz3d built in.
its working good. fast and nice.

i have finded out if use tcpip dont send more than 8k to stream if you send more than 8k client and server will stop. :(

i am limiting my engine to have tcpip ques.. maby its time to use a DLL.


anyway heres a good example of using TCPIP.


http://www.blitzbasic.com/codearcs/codearcs.php?code=2182



There is verry verry many TCPIP and UDP coder in code arcive all free to use.