CONST DefaultPort$
= "7993" CONST DefaultIP$
= "172.93.60.23" CONST True
= -1, False
= 0
'REQUIRED SERVER CODE ABOVE
_CONTROLCHR OFF 'not necessary, but used here to help diagnose stray characters passed back and forth
PRINT "Connecting to Server..";
Client = ConnectAsClient(DefaultPort, DefaultIP$)
PRINT "Press <ANY KEY> to continue..."
PRINT "Are you a new player? (Y/N)" INPUT "REGISTER NAME:"; n$
INPUT "REGISTER PASSWORD:"; p$
Send Client, "[REGISTER]" + n$ + "," + p$ 'Register a new account
INPUT "LOGIN PASSWORD:"; p$
Send Client, "[LOG IN]" + n$ + "," + p$ 'Login from an existing account
OK$ = Verify$
PRINT OK$
'don't just print the results here LOOP UNTIL LEFT$(OK$
, 4) = "[OK]" 'we want to actually make use of them. PRINT "Press <ANY KEY> to continue..."
Send Client, "[GAME SET]Click It" 'Let the server know what game we're playing.
Send Client, "[GAME GET]Highscore.txt"
OK$ = Verify$ 'same deal as above. We don't want to just print the results; we want to use them.
IF LEFT$(OK$
, 4) = "[OK]" THEN highscore$
= MID$(OK$
, 5) 'the server just sent us the whole highscore listing, 'all at once. We can write it to our disk if we want (OPEN file$ FOR BINARY and PUT the whole file to drive),
'or we can parse it as we want, to make sense of who/what it is.
'What this file contains is what we sent to the server to store for us, so if we don't know how to decipher it
'for our game, the server sure as hell doesn't know what to do with it either!
PRINT "Highscore.txt retrieved." PRINT "Press <ANY KEY> to continue..."
'At this point, highscore.txt is either going to be one of two different things:
'1) It's going to be blank and have a file length of 0, as we haven't saved any highscores yet.
'2) It's going to be the whole highscore list.
'Regardless, let's go ahead and just save file this to our drive as file handle #1. (Our only needed filehandle)
PUT #1, , highscore$
'and put the whole file to the drive all at once.
'AGAIN, You'll need to come up with your own method to handle however you process the file the server sent you.
'This is just the simple style which I chose to use for this demo.
SEEK #1, 1 'This isn't the most efficient way to get my high score list, but it's the one I'm using DIM Player$
(1000), Score
(1000) 'Up to 1000 players, and their score. players = players + 1
Score
(players
) = VAL(junk$
) IF Player$
(players
) = n$
THEN click
= Score
(players
): found
= players
LINE (100, 100)-(300, 200), &HFF00FF00, BF
LINE (100, 100)-(300, 200), -1, B
click = click + 1
ELSE 'we clicked outside the box! We have to quit!
Score(found) = click
players = players + 1
Score(players) = click
Player$(players) = n$
FOR i
= 1 TO players
- 1 'a lazy bubble sort to rank our players. FOR j
= i
TO players
'this is inefficient as heck, but I don't expect there will be millions of players to rank! IF Score
(i
) < Score
(j
) THEN SWAP Player$
(i
), Player$
(j
):
SWAP Score
(i
), Score
(j
)
GET #1, 1, s$
'get that information into a single large string
'****************************************************************
'**
'** NEXT COMMAND IS STRESSED FOR EMPHASIS! READ IT!!!!
'**
'*****************************************************************
Send Client
, "[GAME PUT]highscore.txt" + CHR$(0) + s$
'NOTICE THE SEPARATOR BETWEEN OUR COMMAND AND THE FILE CONTENTS'WITHOUT THAT SEPARATOR (CHR$(0)), YOU WILL GET ERRORS AND NOT SAVE YOUR FILE.
'****************************************************************
'**
'** PREVIOUS COMMAND IS STRESSED FOR EMPHASIS! READ IT!!!!
'**
'*****************************************************************
FOR i
= 1 TO players
'just dump the player scores to the screen. 'I'm not looking for pretty at this point, just trying to highlight the functionality.
PRINT i;
") "; Player$
(i
), Score
(i
)
'SERVER CODE BELOW
count = count + 1
temp
= _OPENCLIENT("TCP/IP:" + Port$
+ ":" + IP$
) 'attempt to join as client IF Verbose
THEN PRINT "Cannot connect to Host. Terminating program." ConnectAsClient = temp
out$
= CHR$(2) 'an initial byte to say, "We're sending data."
r$ = r$ + In
In = "" 'Is there a message waiting for us, after we processed one message?
What$
= MID$(r$
, 9) '8 bytes for the length In
= MID$(r$
, length
+ 1)
Receive Client, OK$ 'Get the confirmation message.
_LIMIT 10 'Use low CPU while waiting. No need for a high number here... Be patient and let the server have time to respond. Checking 10 times a second is more than enough to get an [OK] or [ERROR] message. Verify$ = OK$