Here's a version of your program that can run both client and/or host. Checks if a host is already open, then connects as client. Its just easier than having two separate programs.
Zep
Or If you want an example of how to use it for Network game play you can check out my Battleship 64! game.
https://www.qb64.org/forum/index.php?topic=826
the LAN network game play is\was(at that time) fully working. The basic guts of the network code is based off the Mini Messenger example from the wiki.
If anything I would Love to know how to get it running over the NET instead of just a LAN.
Don’t you need to set up your router for port forwarding for that? Or am I overthinking things?
Hi. I tried it now, with Bad Filename or number error message on line 6. Probably because the server is down. In a real network, when passing data through multiple routers (namely one computer was connected via wifi, the other by cable over two routers) while I was writing battleship, I found that the client and server should watch the time from sending the packet until receive is not confirmed, or after time limit must be the packet again send. Then the counterparty should wait and send an acknowledgment upon receipt, perhaps as a LONG type with the size of the incoming packet. Perhaps a byte structure such as a PNG header would be useful, - I heard it was well written for work in network. (PNG begin and PNG end blocks)
Is this like a DCC Chat that's directly computer to computer over the Internet, or just a local LAN chat, or an Internet chat that needs a host server? I ask because first you talk about it being used locally, then later guys are talking to each other with it. I haven't tried it, just wondering, thanks.
Steve, i see your sentences. And you mine?
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
I just don't see any point in keeping it running 24/7 when it's just sitting there and doing nothing... ;)