One more upgraded version of the mini messenger which works across the internet and connects to my host.
DIM SHARED OUT$, myname$, client
DIM SHARED l AS _UNSIGNED _BYTE
DIM SHARED PingTimer AS _FLOAT, LastSeen AS _FLOAT
SCREEN _NEWIMAGE(800, 600, 32)
COLOR &HFFFFFFFF, &HFF000000
PRINT "[Steve's Mini Messenger]"
client = _OPENCLIENT("TCP/IP:7993:172.93.60.23") ' Attempt to connect to local host as a client
PRINT "[connected to " + _CONNECTIONADDRESS(client) + "]"
DO
INPUT "Enter your name: ", myname$
myname$ = _TRIM$(myname$)
LOOP UNTIL myname$ <> ""
OUT$ = myname$ + " connected!" + CHR$(10)
PUT #client, , OUT$
PingTimer = TIMER(0.01)
LastSeen = TIMER(0.01)
DO
GetMessage
IF PingTimer + 30 < TIMER(0.01) THEN
PingTimer = TIMER(0.01)
OUT$ = "PING" + CHR$(10)
'uncomment the below to diagnostically print when we ping the chat server
'VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT "PING"; TIME$
PUT #client, , OUT$
OUT$ = ""
END IF
OUT$ = ""
GetInput mymessage$ ' display current input on screen
VIEW PRINT
FOR i = 32 TO 35
LOCATE i, 1: PRINT SPACE$(_WIDTH / _FONTWIDTH); ' erase previous message displayed
NEXT
LOCATE 32, 1: PRINT myname$ + ": "; mymessage$;
IF INSTR(mymessage$, CHR$(10)) THEN
OUT$ = myname$ + ":" + mymessage$
PUT #client, , OUT$
VIEW PRINT 1 TO 30
LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
mymessage$ = ""
END IF
_DISPLAY
_LIMIT 60
IF LastSeen + 60 < TIMER(0.01) THEN 'inactive connection for too long
VIEW PRINT 1 TO 30
LOCATE 30, 1: PRINT "[Session Termined. Host Timed Out.]"
END
END IF
LOOP
'.................... END OF MAIN PROGRAM ................
SUB GetMessage ' get & display any new message
DO
GET #client, , a$
done = -1
IF LEN(a$) > 0 THEN
OUT$ = LTRIM$(OUT$ + a$)
done = 0
l = INSTR(a$, CHR$(10))
IF l THEN
IF OUT$ = "PONG" + CHR$(10) THEN
'Uncomment the following line if you want to watch the chat server talk back automatically to PING requests.
'VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
LastSeen = TIMER(0.01)
OUT$ = ""
ELSE
VIEW PRINT 1 TO 30: LOCATE 30, 1: PRINT LEFT$(OUT$, LEN(OUT$) - 1)
END IF
done = -1
END IF
END IF
_LIMIT 60
LOOP UNTIL done
END SUB
SUB GetInput (mymessage$) ' simple input handler
k$ = INKEY$
IF LEN(k$) THEN
IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
ELSE
IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
END IF
END IF
IF k$ = CHR$(27) OR _EXIT THEN
OUT$ = myname$ + ": Terminated the session." 'Be nice and let the server know when we log out.
PUT #client, , OUT$
SYSTEM ' [Esc] key ends program
END IF
IF LEN(mymessage$) >= 255 THEN
mymessage$ = mymessage$ + CHR$(10) 'autosend after 255 characters
ELSE
IF k$ = CHR$(13) THEN mymessage$ = mymessage$ + CHR$(10) 'so we don't send with 2 chr$(10) characters.
END IF
END SUB
The code here is now a lot friendly for my PC... It notifies me when someone terminates a session, and it sends PINGs and PONGs every so often to make certain that both sides are still active and that the connection still exists.
I'm going to leave the chat server up and running tonight, feel free to pop in at anytime and chat with whoever might be hanging around the place if I'm not there. No matter what, when you connect, you should get a nice entry message welcoming you to the room. As long as you see it, you know you connected to my PC, told me hello, and my PC replied back to you.
Enjoy guys! I don't know how much more I'll continue to work with this little demo, but I think it already has proven beyond a doubt that QB64 can be used to communicate across the internet for us. ;)