Development goes on. ⚡️👟QB64 v2.0.2 released! 🤩🤩🤩🤩Get it now!
0 Members and 1 Guest are viewing this topic.
In that case, I'd use OPENHOST and OPENCLIENT to form a TCP/IP connection between the two programs, ofwhich there are several examples already here on the forums via chat hosts and clients.
TCP/IP between two programs on the same machine? Interesting...
A lot of folks are curious about how we can get our programs to talk to each other, and curious about how we'd use QB64 to communicate via TCP/IP over a network. The wiki has a few examples, but they tend to be outdated and simply don't work for me. (Such as the mini-messenger example here: http://www.qb64.org/wiki/OPENHOST.) I figured people might like a working example of how to get all the proper parts working together, so I tried various wiki samples and eventually decided to rework one until I got it to working for me...The finished code here is working as intended on Windows. (I dunno if it'll work for Linux or Mac users, but I'd love to hear if it does or doesn't.) Instead of a single set of code which tries to toggle between client and host, I worked this up as two separate sets of code -- one for each. Copy one set of code into QB64, and then run it. Then, while that program is still running in the background, copy the second set of code and run it.. Type in either program and watch as they happily communicate with each other without any issues.THE HOST:Code: QB64: [Select]DIM SHARED Users(1 TO 1000) ' array to hold other client infoDIM SHARED NumClientsDIM SHARED out$ PRINT "[Steve's Mini Messenger]"host = _OPENHOST("TCP/IP:7319") ' no host found, so begin new hostIF host THEN PRINT "[Beginning new host chat session!]" NumClients = 0 client = _OPENCLIENT("TCP/IP:7319:localhost") IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!" INPUT "Enter your name:", myname$ 'PRINT #client, myname$ + " connected!" PRINT "[Chat session active!]"ELSE PRINT "ERROR: Could not begin new host!"END IF ' host DO ' host main loop newclient = _OPENCONNECTION(host) ' receive any new connection IF newclient THEN NumClients = NumClients + 1 Users(NumClients) = newclient PRINT "Welcome to Steve's Mini Messenger!" END IF FOR i = 1 TO NumClients GetMessage Users(i) 'check all clients for a message IF out$ <> "" THEN l = LEN(out$) FOR j = 1 TO NumClients ' distribute incoming messages to all clients PUT #Users(j), , l PUT #Users(j), , out$ NEXT END IF NEXT i SendMessage myname$, mymessage$, client _LIMIT 30LOOP SUB GetMessage (client) ' get & display any new message GET #client, , l IF l > 0 THEN out$ = SPACE$(l) GET #client, , out$ VIEW PRINT 1 TO 20 LOCATE 20, 1 PRINT out$ VIEW PRINT 1 TO 24 ELSE out$ = "" END IFEND SUB SUB SendMessage (myname$, mymessage$, client) ' 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 VIEW PRINT 1 TO 24 LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed LOCATE 22, 1: PRINT myname$ + ": "; mymessage$; IF k$ = CHR$(13) THEN ' [Enter] sends the message IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program mymessage$ = myname$ + ":" + mymessage$ l = LEN(mymessage$) PUT #client, , l PUT #client, , mymessage$ mymessage$ = "" END IF IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends programEND SUBTHE CLIENT:Code: QB64: [Select]DIM SHARED out$ PRINT "[Steve's Mini Messenger]"client = _OPENCLIENT("TCP/IP:7319:localhost") ' Attempt to connect to local host as a clientPRINT "[connected to " + _CONNECTIONADDRESS(client) + "]" INPUT "Enter your name: ", myname$out$ = myname$ + " connected!"l = LEN(out$)PUT #client, , lPUT #client, , out$DO GetMessage client SendMessage myname$, mymessage$, client ' display current input on screen _LIMIT 30LOOP '.................... END OF MAIN PROGRAM ................ SUB GetMessage (client) ' get & display any new message GET #client, , l IF l > 0 THEN out$ = SPACE$(l) GET #client, , out$ VIEW PRINT 1 TO 20 LOCATE 20, 1 PRINT out$ VIEW PRINT 1 TO 24 ELSE out$ = "" END IFEND SUB SUB SendMessage (myname$, mymessage$, client) ' 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 VIEW PRINT 1 TO 24 LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed LOCATE 22, 1: PRINT myname$ + ": "; mymessage$; IF k$ = CHR$(13) THEN ' [Enter] sends the message IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program mymessage$ = myname$ + ":" + mymessage$ l = LEN(mymessage$) PUT #client, , l PUT #client, , mymessage$ mymessage$ = "" END IF IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends programEND SUB Have fun playing around with this as a local system messenger program. Try it out, kick it around, and let me know if there's anything you don't understand about what it's doing. This isn’t exactly how I'd normally write one of these; but that's because I started with what the wiki had and then gutted it and rebuilt it up until it was actually working for me as it should. Honestly, I think I would've been better off to have just wrote the whole program from scratch! :P