multiplayer, client-server

Blitz3D Forums/Blitz3D Beginners Area/multiplayer, client-server

Jsoren(Posted 2006) [#1]
hi, could anyone give me some really basic code on client/server. i am working on a multiplayer RPG (not really MMORPG size)and i was wondering if anyone could give me some *very* basic code on how a client-server relation works, how they comunicate, etc...

if u could that would be great!

JS


AJirenius(Posted 2006) [#2]
I actually don't think that there something that can be called "very basic network code" ... actually there is no such thing.

Network is hard.


b32(Posted 2006) [#3]
Maybe this example ? It uses the DirectPlay functions (TCP) that are in Blitz:



Jsoren(Posted 2006) [#4]
thanks, that was really helpful, i took that code and attemped to split it into a seperate client and server, tell me if u think this looks like it will run:

the client:
Graphics 640,480
SetBuffer BackBuffer()

gamename$="RPG"
gameIP$="0.0.0.0" ;insert IP here

Select JoinNetGame(gamename$,gameIP$)
	Case 1
		Print "Joined!"
	Default
		Print "failed to locate server"
		Print "press any key to close"
		WaitKey
End Select

player=CreateNetPlayer("player"+Rnd(1,100))

x=100
y=100

;main loop
While Not KeyDown(1)
Cls



;cursor keys to move around		
If KeyDown(203) Then x = x - 10
If KeyDown(205) Then x = x + 10
If KeyDown(200) Then y = y - 10
If KeyDown(208) Then y = y + 10


;send message
io = MilliSecs() / 50
If io > ioi Then 
	msgx$=x
	msgy$=y
	SendNetMsg 1,msgx$+msgy$, player, server, 1
	ioi = io
End If

;incoming messages
If RecvNetMsg() Then
	msgType	  = NetMsgType()
	msgFrom   = NetMsgFrom()
	msgData   = NetMsgData$()
	a# = Mid(msgdata,1,3)
	b# = Mid(msgdata,3,6)
End If


;draw player
Color 255, 255, 0
Oval x, y, 20, 20, True
;draw player
Color 255, 0, 0
Oval a, b, 20, 20, True

Flip
Wend

End


the server:
gamename$="RPG"

Select HostNetGame(gamename$)
Case 2 
	Print "Successfully started host game!" 
Default 
	Print "Game was unable to start!"
End Select

host=CreateNetPlayer("server")

While Not KeyDown(1)

;incoming messages
If RecvNetMsg() Then
	msgType	  = NetMsgType()
	msgFrom   = NetMsgFrom()
	msgData   = NetMsgData$()
	x$ = Mid(msgdata,1,3)
	y$ = Mid(msgdata,3,6)
	Print "receivinbg message from player " + msgfrom + "..."
	Print "message type = " + msgtype
	Print "player X = " + x
	Print "player Y = " + y
	;send message
	SendNetMsg 1,x$+y$, server, 0, 1
	Print "sending player coords....
End If



Wend


the server dosnt actually player the game, it just takes the info from each client connected to it, and sends it out too all of them

JS


b32(Posted 2006) [#5]
Well, it seems to be running. You can try it on one machine, from the blitz ide. First run the server code, then minimize the program and go to the client code and run it.


Boiled Sweets(Posted 2006) [#6]
You perhaps ought to take a look at k-netlib

http://www.krylarskreations.com/devtools.shtml

Features :

* Client/Server and Peer-to-Peer support

* Byte/Word/Long/Float/String/Array sends and receives
* Group controls

* Passive file transfers that allow your users to continue
chatting/playing while the file transfer in the background

* Built-in Bezier-Spline functionality to allow easy
calculation of smooth lines for multiplayer games

* Advanced error-handling routines to keep you informed
during your development cycle

* INI file commands to allow loading and saving of
initialization strings for your K-NetLib application

* Ban controls that allow one-time kicks, bans, and permanent bans stored in a file for your game

* ...and more!

Oh and it's free, robust, fast and supports Blitz3d!


Jsoren(Posted 2006) [#7]
ok, i've worked some mroe on my client server code, and it now works with more than 1 client conected to the server, here is the updated code for the client:
Graphics 640,480
SetBuffer BackBuffer()
AppTitle "client"

gamename$="RPG"
gameIP$="0.0.0.0" ;insert IP here

Select JoinNetGame(gamename$,gameIP$)
	Case 1
		Print "Joined!"
	Default
		Print "failed to locate server"
		Print "press any key to close"
		WaitKey
End Select

player=CreateNetPlayer("player"+Rnd(1,100))



;Global msgx$
;Global msgy$

;main loop
While Not KeyDown(1)
Cls



;cursor keys to move around		
If KeyDown(203) Then x = x - 5
If KeyDown(205) Then x = x + 5
If KeyDown(200) Then y = y - 5
If KeyDown(208) Then y = y + 5


;send message
io = MilliSecs() / 50
If io > ioi Then 

	msgx$ = x
	;expand so msgX and msgY are always 4 igits long
	If x<10
		msgx$="000"+x
	ElseIf x<100
		msgx$="00"+x
	ElseIf x<1000
		msgx$="0"+x
	EndIf
	
	msgy$ = y
	If y<10
		msgy$="000"+y
	ElseIf y<100
		msgy$="00"+y
	ElseIf y<1000
		msgy$="0"+y
	EndIf
	
	SendNetMsg 1,msgx$+msgy$, player, server, 1
	ioi = io
End If

;incoming messages
If RecvNetMsg() Then
	msgType	  = NetMsgType()
	msgFrom$   = NetMsgFrom()
	msgData$   = NetMsgData$()
	a = Left$(msgdata,4)
	b = Right$(msgdata,4)
	Print "recieving message"
End If


;draw player
Color 255, 0, 0
Oval a, b, 20, 20, True
;draw player
Color 255, 255, 0
Oval x, y, 20, 20, True


Flip
Wend

End


and here is the updated server:

AppTitle "server"
gamename$="RPG"

Select HostNetGame(gamename$)
Case 2 
	Print "Successfully started host game!" 
Default 
	Print "Game was unable to start!"
End Select

host=CreateNetPlayer("server")

While Not KeyDown(1)

;incoming messages
If RecvNetMsg() Then
	msgType	  = NetMsgType()
	msgFrom$   = NetMsgFrom()
	msgData$   = NetMsgData$()
	x$ = Left$(msgdata,4)
	y$ = Right$(msgdata,4)
	Print "receiving message from player " + msgfrom + "..."
	Print "message type = " + msgtype
	Print "player X = " + x$
	Print "player Y = " + y$
	;send message
	SendNetMsg 1,x$+y$, server, 0, 1
	Print "sending player coords....
End If


Wend


p.s. boiled sweets: that k-netlib looks really useful, ive downladed it, and im gonna se what i can do with it :)


Boiled Sweets(Posted 2006) [#8]
Jsoren,

I guess it's down to do you *want* to write all the network code or do you just want to use a robust network library that is designed for Blitz and get on with writing your game.

If you are not careful you will spend 99% of you time trying to get the network stuff working 100% and never actually finish your game.


AJirenius(Posted 2006) [#9]
As a newbie in Blitz3D but some experienced in other ... engines I'd love to second Boiled Sweets suggestions.

Writing newtworkcode is actually darn hard when it comes to smoothness and precalculations of movements and vectors + optimizing packets.
I would say there is no shame whatsoever to use libs (I mean, we ARE programming in Blitz3D, right) and 99.9% of the time (yes its a totally nonscientific number) you wont probably have so special needs that you don't have use for the already created wheel.

On the other hand knowledge is a bless and a whole development community out there are screaming for networkcoders so I wouldn't mind if you kept on going before I force you into my team :D


Jsoren(Posted 2006) [#10]
i am definitly going to use k-netlib for the MMORPG i am developing, however, i wanted to make some very simple client/server code for some small projects that r really just for myself. i have wanted to get a hold on the blitz3d networking commands for a while, so i did really what i wanted with my code...for everything else i will probly use K-netlib, it looks really nice!

ty for everything! really helpful

JS


chrisnorris007(Posted 2008) [#11]
can this be used on a mmorpg scale?


xtremegamr(Posted 2008) [#12]
can this be used on a mmorpg scale?


What do you mean; are you asking is k-netlib can be used on a mmorpg scale, or Blitz DirectPlay?


Mortiis(Posted 2008) [#13]
I can't find a K-Net lib Blitz3D userlib at the website?

Edit: Ooops, didn't see the Knet posts were 2 years old! You thread necromancers you!


Knight #51(Posted 2008) [#14]
I wouldn't bother using Blitz's DirectPlay at all. It is FAR too slow for a MMORPG. I would use TCP/IP for an MMORPG. As for a very basic example..here is something I'll write for you right now:




chrisnorris007(Posted 2009) [#15]
I can provide code if anyone is interested.


Gabriel(Posted 2009) [#16]
I wouldn't bother using Blitz's DirectPlay at all. It is FAR too slow for a MMORPG. I would use TCP/IP for an MMORPG.

DirectPlay uses UDP. TCP/IP is slower than UDP.


Matty(Posted 2009) [#17]
Directplay has another drawback which is that you cannot specify a port to use, meaning that people behind routers may not necessarily be able to play as some routers do not allow port forwarding easily. I know from my own experience that my router can only forward a small number of ports which are specified one at a time.


Wings(Posted 2009) [#18]
TCP is safer but the block trafic mode is pain in a....

so if you go for tcp you may ned to look at code archive...