Ive just got back in to programming in the small way and have attempted the collosal rebuild of one of the programs I was working on, I seem to however hit a small snag.
I'm not sure if its multisync my code or another problem causing the client to be disconnected after about 30 seconds can someone check my two code snippets one for the client one for the server to see if there is any *obvious* flaw.
Client:
version as string = "0.002"
global serverIP as string = "92.17.42.117"
global localIP as string = "192.168.2.3"
`constants ---------------------------------------------
#constant MAXIMUM_PLAYERS 10
#constant MESSAGE_ID_NAME 0
#constant MESSAGE_ID_PLAYERJOINED 3
`types -------------------------------------------------
type player_data
exist as boolean
name as string
endtype
`Variables ---------------------------------------------
global dim player(MAXIMUM_PLAYERS) as player_data
global NAME as string
`core setup---------------------------------------------
connect()
loadscene()
sync on `enable text graphics
disable escapekey
`main loop ---------------------------------------------
do
HandleControls()
HandleCamera()
If net connected()
HandleNetwork()
else
CenterBox(screen width()/2,screen height()/2,200,26,"Server Connection Lost!")
endif
sync
if escapekey()=1 then EndGame()
loop
function connect()
print "Press 1 to connect to server via internet - IP "+serverIP
print "Press 2 to connect to server via LAN - IP "+localIP
print "Press 3 to connect to server via internet - input IP"
do
If inkey$()="1"
ip$ = serverIP
result = net connect (ip$)
if not result
print "Could not connect: "+chr$(34)+net get error()+chr$(34)+"."
print "Press any key to exit."
wait key
end
else
exit
endif
endif
If inkey$()="2"
ip$ = localIP
result = net connect (ip$)
if not result
print "Could not connect: "+chr$(34)+net get error()+chr$(34)+"."
print "Press any key to exit."
wait key
end
else
exit
endif
endif
If inkey$()="3"
input "IP: ", ip$
result = net connect(ip$)
if not result
print "Could not connect: "+chr$(34)+net get error()+chr$(34)+"."
print "Press any key to exit."
wait key
end
else
exit
endif
Endif
`loop until key pressed
loop
print "Connection with server established."
input "Name: ", NAME
net put byte MESSAGE_ID_NAME
net put string NAME
net send
`wait for return signal with player ID
print "Awaiting Return Signal. >"
do
while net get message()
select net get byte()
case MESSAGE_ID_NAME
print "inside loop waiting for responce"
playerNum= net get byte()
player(playerNum).name = net get string()
myplayerNum= playerNum
if playerNum > 0 then exit
endcase
endselect
endwhile
if playerNum > 0 then exit
loop
print "Player ID. confirmed as: "+str$(playerNum)
print "Player Name Confirmed as: "+player(playerNum).name
print "Signal Returned. Exiting Login."
wait 30
endfunction
function loadscene()
rem setup display
set display mode 640,480,16
sync on: sync rate 60: cls 0
sync off
autocam off
set ambient light 70
make matrix 1,1000,1000,10,10
`skysphere
make object sphere 50,-3000
position object 50,500,0,500 :`positions half size of matrix
`rem temporary internal textures
`grass
create bitmap 1, 100,100
for i=1 to 100
for t=1 to 100
color=rnd(255)
ink rgb(0,color,0),0
box i-1,t-1,i+1,t+1
next t
next i
get image 1,0,0,100,100
set current bitmap 0
prepare matrix texture 1,1,1,1
update matrix 1
`sky
create bitmap 1, 100,100
for i=1 to 100
for t=1 to 100
color=rnd(255)
ink rgb(0,0,color),0
box i-1,t-1,i+1,t+1
next t
next i
get image 2,0,0,100,100
set current bitmap 0
texture object 50,2
CreateAvatar(1)`fix this to be my player number
endfunction
function CreateAvatar(x)
`load temp avatar
load object "media\dalek1.x",x
`load image "media\dalekcolours.bmp",102
`texture object x,102
scale object x,300,300,300
position object x,500,15,500
endfunction
function EndGame()
CenterBox(screen width()/2, screen height()/2, 300, 50,"QUIT game? Y/N")
sync
do
if inkey$()="y" then end
if inkey$()="Y" then end
if inkey$()="n" then exit
if inkey$()="N" then exit
loop
endfunction
function CenterBox(x as integer, y as integer, xSize as integer, ySize as integer, textString as string)
xPos = x-(xSize/2)
yPos = y-(ySize/2)
ink rgb(200, 200, 200), 0
box xPos, yPos, xPos+xSize, yPos+ySize
ink rgb(255, 255, 255), 0
center text x, y-(text height("A")/2), textString
endfunction
function HandleCamera()
xPos# = object position x(1) + cos(270-object angle y(1))*50
zPos# = object position z(1) + sin(270-object angle y(1))*50
position camera xPos#,40,zPos#
point camera object position x(1),20,object position z(1)
endfunction
function HandleControls()
if upkey() then move object 1,1
if downkey() then move object 1,-1
if leftkey() then yrotate object 1, wrapvalue(object angle y(1)-1)
if rightkey() then yrotate object 1, wrapvalue(object angle y(1)+1)
endfunction
function HandleNetwork()
endfunction
and server
version as string = "0.002"
`constants ---------------------------------------------
#constant MAXIMUM_PLAYERS 10
#constant MESSAGE_ID_NAME 0
#constant MESSAGE_ID_PLAYERJOINED 3
`types -------------------------------------------------
type player_data
exist as boolean
name as string
endtype
`Variables ---------------------------------------------
global dim player(MAXIMUM_PLAYERS) as player_data
global NAME as string
`core --------------------------------------------------
setup()
serveronline()
`main loop ---------------------------------------------
do
handlenetwork()
rem diplay info
text 12,90,"SERVER : "+Entry$(1)
text 0,445,"There are "+str$(net get player amount())+" players connected. Server IP : "+GetMyIp()
text 0 ,460,"FPS : "+str$(screen fps())+" Server Version V"+version
n=0
for x = 1 to MAXIMUM_PLAYERS
text 0,110+n,"Player"+str$(x)+":"+player(x).name
n=n+20
next x
sync
loop
`-------------------------------------------------------
function handlenetwork()
`automatic network handling ------------------------------
`player joining
result = net player joined()
while result >0
make object cube result,10
position object result,500,5,500
player(result).exist =1
result =0
endwhile
`player leaving
result = net player left()
while result>0
ResetPlayer(result)
result=0
endwhile
`data processing ------------------------------------------
while net get message()
playerNum = net message from()
`slects byte number and processes
select net get byte()
case MESSAGE_ID_NAME
playerNum = net message from()
player(playerNum).name = net get string()
NAME = player(playerNum).name
`return signal
net put byte MESSAGE_ID_NAME
net put byte playerNum
net put string NAME
net send playerNum `,1 ?> not sure if this is doing anything
`confirmed back to client
endcase
endselect
endwhile
endfunction
function setup()
set display mode 640,480,32
sync on: sync rate 0:cls 0
sync off
autocam off
make matrix 1,1000,1000,10,10
position camera 500,1000,500
point camera 500,0,500
sync on
endfunction
function serveronline()
result = net host(MAXIMUM_PLAYERS)
if not result
print "could not start server: "+chr$(34)+net get error()+chr$(34)+"."
print "press any key to exit."
wait key
end
endif
endfunction
function ResetPlayer(playerNum as integer)
if object exist(playerNum) then delete object playerNum
player(playerNum).exist = 0
player(playerNum).name = ""
endfunction
please note that the files probably wont run even if you have multisync installed unless you change the model filenames.
The best way I've found to test it is to make them into windowed mode and the client into an exe so that multiple copys can be run. then you should see *if its the same as mine* the clients disconnect error and the server lose the names too.
'Ooh 'eck chief'...'crumbs'