I'm playing around with the networking and I'm having trouble. I created the server app and ran on my computer. Here's the code:
rem
rem AGK Application
rem
type sHighScore
init$ as string = ""
score as Integer = 0
endtype
global dim HiScores [ 10 ] as sHighScore
iNetID = HostNetwork("myNetwork", "Server", 4100)
do
Print("INetID:" + Str(INetID))
for x = 1 to 10
Print("["+Str(x)+"]:" + HiScores[x].init$ + " " + Str(HiScores[x].score))
next x
// Clean up old connections
for x = 1 to 50
if x = 1
id = GetNetworkFirstClient(iNetID)
else
id = GetNetworkNextClient(iNetID)
endif
if id = 0 then exit
if GetNetworkClientDisconnected(iNetID, id) = 1
DeleteNetworkClient(iNetID, id)
endif
next x
// Check for Messages.
msg = GetNetworkMessage(iNetID)
if msg > 0
ClientID = GetNetworkMessageFromClient(msg)
Print ("ClientID:" + Str(ClientID))
do
print ("msg:" + Str(msg))
sync()
loop
if ClientID > 1
f# = GetNetworkMessageFloat(msg)
if f# = 1.0 // Send High Score List
newMsg = CreateNetworkMessage()
AddNetworkMessageFloat(newMsg, 1.0)
for x = 1 to 10
AddNetworkMessageString(newMsg, HiScores[x].init$)
AddNetworkMessageInteger(newMsg, HiScores[x].score)
next x
SendNetworkMessage(iNetID, ClientID, newMsg)
DeleteNetworkMessage(newMsg)
endif
if f# = 1.1
// Receive Score from Client
temp$ = GetNetworkMessageString(msg)
score = GetNetworkMessageInteger(msg)
// See if the submitted score is high enough for the top 10
// If it is, place it where it belongs and shuffle the rest
// of the scores down one position.
for x = 1 to 10
if score > HiScores[x].score then exit
next x
if x < 11
for y = 10 to x+1 step -1
HiScores[y].init$ = HiScores[y-1].init$
HiScores[y].score = HiScores[y-1].score
next y
HiScores[x].init$ = init$
HiScores[x].score = score
endif
endif
endif
DeleteNetworkMessage(msg)
endif
Sync()
loop
It creates an array of high scores and display them to the screen. It also checks for a network message. When a message comes, it should display the message and get stuck in the loop.
I have a sample app that I run that connects to the network, and if successful, creates a network message that includes a score and initials. Here's the code:
rem
rem AGK Application
rem
//InetID = JoinNetwork("myNetwork", 4100, "Client"+str(Random()))
iNetID = JoinNetwork("myNetwork", "Client"+str(Random()))
// If we don't connect within 4 seconds, print an error and exit
t# = timer()
do
c = GetNetworkNumClients(iNetID)
if c > 1 then exit
if timer() - t# > 4.0
exit
else
print("Connecting...")
endif
sync()
loop
If c > 1
ServerID = GetNetworkServerID(iNetID)
newMsg = CreateNetworkMessage()
AddNetworkMessageFloat(newMsg, 1.1)
// Add the packet identifier
AddNetworkMessageString(newMsg, "PR")
// Add the player's initials
AddNetworkMessageInteger(newMsg, 100) // Add the player's score
SendNetworkMessage(iNetID, ServerID, newMsg)
// Send it to the server
DeleteNetworkMessage(newMsg)
CloseNetwork(iNetID)
do
print("ServerID:" + Str(ServerID))
print("iNetID:" + Str(iNetID))
sync()
loop
else
do
print ("not connected.")
sync()
loop
endif
The app runs, and displays the ServerID the iNetID, so I know it is connecting. But it does not appear that the message is getting sent or received. Not sure which. But the server app never displays the message, so I'm not sure it's getting there. But I do know that it is connecting.
Anyone know what I might be doing wrong?