Hello,
In my current project, when you host a multiplayer game, you connect to yourself (so the server is a listen server, not a dedicated one). Everything is fine, until the first UDP package is sent. When I do that, it just disconnects.
This is the code I use:
If Timer() > LastUpdate + TickRate
` Send our position/angle to the server via UDP
mn Add Int SendPacket,OP_POSITIONPLAYER
mn Add Int SendPacket,OwnUserID
mn Add Float SendPacket,Camera Position X()
mn Add Float SendPacket,Camera Position Y()
mn Add Float SendPacket,Camera Position Z()
mn Add Float SendPacket,Camera Angle X()
mn Add Float SendPacket,Camera Angle Y()
mn Add Float SendPacket,Camera Angle Z()
mn Send UDP INST_CONNECT,SendPacket,0,0,1 // If i comment this, it stays connected.
LastUpdate = Timer()
Print "SENT"
Endif
Can anyone help me?
Thanks!
Jeff
EDIT: This is the server creation code:
Rem ***** Included Source File *****
Function ShowServerBrowser()
` Filled later by server or client
Global MaximumClients as integer
Global MaximumOperations as integer
` Stores the mode we are in (connect, host or broadcast)
Global iMode as integer
` Variable to hold the result of a MikeNet command
Global Result as integer
` Class used for list of servers on LAN (MODE_BROADCAST)
`TYPE clLANServer
` IP as string
` Name as string
` Port as integer
` ClientClock as dword
`ENDTYPE
` The client will wait for this amount of time (seconds)
` before giving up on a connection attempt
Global Timeout as integer
Timeout = 4
` Used in INST_BROADCAST and INST_CONNECT
` Stores information on where to connect to (to be filled later)
Global ConnectPort as integer
ConnectPort = 0
Global ConnectIP as string
ConnectIP = ""
` INST_HOST Information (to be filled later)
Global ServerPort as integer
ServerPort = 0
Global LocalIP as string
LocalIP = ""
Global ServerName as string
ServerName = ""
Global BroadcastPacket as double integer
BroadcastPacket = 0
Mn Start 3, 0
mn Disable Debug
LocalIP = mn Get Local IP(0)
Global SendPacket
Global RecvPacket
` Setup packets
RecvPacket = mn Create Packet()
SendPacket = mn Create Packet()
mn Set Memory Size SendPacket,1024
` Select interface to use (a local IP)
iNoInter = mn Get Local IP Amount()
Print "Host name: ";
Print mn Get Host Name()
Print "Press the Y key to select a local IP from the list below"
for n = 0 to iNoInter-1
Print str$(n);
Print ": ";
Print mn Get Local IP(n)
next n
SYnc
do
Wait 1
` If the 'y' key is pressed then find out what server the user wants
if KeyState(21) = 1
while KeyState(21) = 1
Wait 1
endwhile
Print "Interface number: "
Sync
Input "",iInterface
if iInterface < iNoInter
` Get IP
LocalIP = mn Get Local IP(iInterface)
CLS
exit
endif
endif
loop
` Find out whether to host a server or just connect to one
Print "Select one of the below options by pressing a number key:"
Print "0: Host - create a server and connect to it"
Print "1: Connect - connect to a server that we know the IP and port of"
Print "2: LAN - connect to a server that is on our local area network"
Sync
do
` If '0' key is pressed
if KeyState(11) = 1
iMode = MODE_HOST
while KeyState(11) = 1
Wait 1
endwhile
exit
endif
` If '1' key is pressed
if KeyState(2) = 1
iMode = MODE_CONNECT
while KeyState(2) = 1
Wait 1
endwhile
exit
endif
` If '2' key is pressed
if KeyState(3) = 1
iMode = MODE_BROADCAST
while KeyState(3) = 1
Wait 1
endwhile
exit
endif
Wait 1
loop
CLS
` Setup broadcasting which is used by:
` -the client to generate a list of available servers on the LAN
` -the server to broadcast its availability to clients
if iMode = MODE_HOST or iMode = MODE_BROADCAST
` Setup the broadcast instance, only UDP is relevant
` We receive (mnSetLocal) on port 5888 and broadcast (mnStartBroadcast) to port 5888
if iMode = MODE_HOST
mn Set Local INST_CONNECT,"",0,LocalIP,5888
mn Start Broadcast INST_BROADCAST,5888,1,0
endif
if iMode = MODE_BROADCAST
mn Set Local INST_BROADCAST,"",0,LocalIP,5888
mn Start Broadcast INST_BROADCAST,5888,0,1
endif
` Generate a list of available servers on the LAN
` == DELETED FOR NAO ==
endif
if iMode = MODE_CONNECT
Sync Off
` Get IP address and port to connect to via UDP and TCP
Input "Enter the IP that you would like to connect to: ",ConnectIP
Input "Enter the port that you would like to connect to: ",ConnectPort
Sync On : Sync : Sync
endif
if iMode = MODE_HOST
` Server information
MaxClients = 50
Online.MaxPlayers = MaxClients
MaxOperations = 1
UdpMode = 2
NoThreads = 0
Sync Off : Sync
Input "Enter the local port that you would like to use: ",ServerPort
Input "Enter the name to call your server: ", ServerName
Input "Map to load: ", MapToLoad$
Sync On : Sync : Sync
CLS
` Create packet to broadcast
BroadcastPacket = mn Create Packet()
mn Set Memory Size BroadcastPacket,1024
mn Add String BroadcastPacket,LocalIP,0,1
mn Add Int BroadcastPacket,ServerPort
mn Add String BroadcastPacket,ServerName,0,1
` Ensure that we connect to ourselves
ConnectIP = LocalIP
ConnectPort = ServerPort
` Set server local address information
mn Set Local INST_HOST,LocalIP,ServerPort,LocalIP,ServerPort
` Start the server
mn Start Server INST_HOST,MaxClients,MaxOperations,UdpMode
endif
` Disable escape key so that escape cancels to the connection process
Disable EscapeKey
` Start connection process
CLS
Print "Connecting, please wait..."
Print "Press the escape key to cancel"
Sync : Wait 100
mn Set Local INST_CONNECT,LocalIP,0,LocalIP,0
mn Connect INST_CONNECT,ConnectIP,ConnectPort,ConnectIP,ConnectPort,Timeout,0
Connect as integer
repeat
` Ensure that connection is accepted
` mnClientJoined must be here otherwise connection will not be accepted
if iMode = MODE_HOST
Result = mn Client Joined(INST_HOST)
endif
` Check to see if connection has completed yet
Connect = mn Poll Connect(INST_CONNECT)
` If the connection was successful
if Connect = 1
` Find maximum number of clients and operations
` necassary for udp receiving
MaximumClients = mn Get Max Clients(INST_CONNECT)
MaximumOperations = mn Get Max Operations(INST_CONNECT)
endif
` If the connection timed out
if Connect = 0
CLS
Print "Connection timed out. The server may not be available."
Print "Press any key to exit"
Sync
Wait Key
end
endif
` If an error occurred during connection
if Connect = -1
CLS
Print "An error occurred whilst trying to connect."
Print "Press any key to exit"
Sync
Wait Key
end
endif
` Check for escape key
if EscapeKey() = 1
while EscapeKey() = 1
Wait 1
endwhile
CLS
Print "Escape key pressed: connection process aborted"
Print "Press any key to exit"
Sync
Wait Key
mn Stop Connect INST_CONNECT
end
endif
Nice Wait 1
until Connect <> 2 ` While connection is in progress
Print "Connected!" : Sync
Wait 1000
Enable Escapekey
If iMode = MODE_CONNECT
// Request map etc from host
mn Add Int SendPacket, OP_JOINDATA
mn Add Int SendPacket, 0
mn Send TCP INST_CONNECT, SendPacket, 0, 0, 0
While tDone = 0
Set Cursor 0,0
` Check for new TCP messages
iTcpPackets = mn Recv TCP(INST_CONNECT,RecvPacket,0)
` If there is a new TCP message
if iTcpPackets > 0
` Get operation of new message
` and player that it applies to
Operation = mn Get Int (RecvPacket)
` If the server is telling us that a new player has joined
` then create a cube for that player
Select Operation
Case OP_JOINDATA
Global OwnUserID : OwnUserID = mn get Int(RecvPacket)
MapToLoad$ = mn Get String(RecvPacket, 0, 1)
Online.MaxPlayers = mn Get Int(RecvPacket)
tDone = 1
EndCase
EndSelect
endif
Print "Waiting for map...", Timer()
Sync
EndWhile
Print "MapToLoad$ = ", MapToLoad$ : Sync : Wait 1000
Else
Online.CurrentMap = MapToLoad$
Global OwnUserID : OwnUserID = 1
Endif
Global Dim Player(Online.MaxPlayers) AS tPlayer
LoadMap(MapToLoad$)
Endfunction
And this is the online handling code:
Rem ***** Included Source File *****
_HandleOnline:
/////////////////
// CLIENT SIDE //
/////////////////
If iMode = MODE_CONNECT
// Send the server we're done loading
If PastThisLine = 0
Global PastThisLine : PastThisLine = 1
mn Add Int SendPacket, OP_DONELOADING
mn Send TCP INST_CONNECT, SendPacket, 0, 0, 1
LastUpdate = Timer()
// If everything goes well, we'll now receive additional data
Endif
Endif
If Timer() > LastUpdate + TickRate
` Send our position/angle to the server via UDP
mn Add Int SendPacket,OP_POSITIONPLAYER
mn Add Int SendPacket,OwnUserID
mn Add Float SendPacket,Camera Position X()
mn Add Float SendPacket,Camera Position Y()
mn Add Float SendPacket,Camera Position Z()
mn Add Float SendPacket,Camera Angle X()
mn Add Float SendPacket,Camera Angle Y()
mn Add Float SendPacket,Camera Angle Z()
mn Send UDP INST_CONNECT,SendPacket,0,0,1
LastUpdate = Timer()
Print "SENT"
Endif
///////////////
// HOST SIDE //
///////////////
If iMode = MODE_HOST
// Deal with new clients
Joined = mn Client Joined(INST_HOST)
// Check for packets
For i = 1 to 5 `Online.MaxPlayers
print i," connected?: ",mn Client Connected(INST_HOST,i)
// TCP //
TCPPackets = mn Recv TCP(INST_HOST, RecvPacket, i)
If TCPPackets > 0
Operation = mn Get Int(RecvPacket)
Select Operation
Case OP_JOINDATA
mn Add Int SendPacket, OP_JOINDATA
// Send userid
mn Add Int SendPacket, i
// Send Mapname
mn Add String SendPacket, Online.CurrentMap, 0, 1
// Send MaxPlayers
mn Add Int SendPacket, Online.MaxPlayers
mn Send TCP INST_HOST, SendPacket, i, 0, 0
EndCase
Case OP_DONELOADING
Player(i).DoneLoading = 1
EndCase
EndSelect
Endif
// UDP //
UDPPackets = mn Recv UDP(INST_HOST, RecvPacket, i, 0)
If Player(i).DoneLoading
If UDPPackets > 0
Operation = mn Get Int(RecvPacket)
Select Operation
Case OP_POSITIONPLAYER
// Get player's position etc
PlayerToUpdate = mn Get Int(RecvPacket)
Player(PlayerToUpdate).X = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Y = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Z = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Xro = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Yro = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Zro = mn Get Float(RecvPacket)
Player(PlayerToUpdate).Health = mn Get Int(RecvPacket)
Print "Got X: ", Player(PlayerToUpdate).X
`mn Add Int SendPacket, OP_POSITIONPLAYER
EndCase
EndSelect
Endif
Endif
Next i
endif
Return
