Author Topic: Drawing different screens on different monitors from 1 program? (Multi Monitor)  (Read 6676 times)

0 Members and 1 Guest are viewing this topic.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
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...

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
TCP/IP between two programs on the same machine?
Interesting...

An example for you for testing purposes:

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]
  1. DIM SHARED Users(1 TO 1000) ' array to hold other client info
  2. DIM SHARED NumClients
  3.  
  4.  
  5. PRINT "[Steve's Mini Messenger]"
  6. host = _OPENHOST("TCP/IP:7319") ' no host found, so begin new host
  7. IF host THEN
  8.     PRINT "[Beginning new host chat session!]"
  9.     NumClients = 0
  10.     client = _OPENCLIENT("TCP/IP:7319:localhost")
  11.     IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
  12.     INPUT "Enter your name:", myname$
  13.     'PRINT #client, myname$ + " connected!"
  14.     PRINT "[Chat session active!]"
  15.     PRINT "ERROR: Could not begin new host!"
  16. END IF ' host
  17.  
  18.  
  19. DO ' host main loop
  20.     newclient = _OPENCONNECTION(host) ' receive any new connection
  21.     IF newclient THEN
  22.         NumClients = NumClients + 1
  23.         Users(NumClients) = newclient
  24.         PRINT "Welcome to Steve's Mini Messenger!"
  25.     END IF
  26.     FOR i = 1 TO NumClients
  27.         GetMessage Users(i) 'check all clients for a message
  28.         IF out$ <> "" THEN
  29.             l = LEN(out$)
  30.             FOR j = 1 TO NumClients ' distribute incoming messages to all clients
  31.                 PUT #Users(j), , l
  32.                 PUT #Users(j), , out$
  33.             NEXT
  34.         END IF
  35.     NEXT i
  36.  
  37.     SendMessage myname$, mymessage$, client
  38.     _LIMIT 30
  39.  
  40.  
  41. SUB GetMessage (client) ' get & display any new message
  42.     GET #client, , l
  43.     IF l > 0 THEN
  44.         out$ = SPACE$(l)
  45.         GET #client, , out$
  46.         VIEW PRINT 1 TO 20
  47.         LOCATE 20, 1
  48.         PRINT out$
  49.         VIEW PRINT 1 TO 24
  50.     ELSE
  51.         out$ = ""
  52.     END IF
  53.  
  54. SUB SendMessage (myname$, mymessage$, client) ' simple input handler
  55.     k$ = INKEY$
  56.     IF LEN(k$) THEN
  57.         IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
  58.             mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
  59.         ELSE
  60.             IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
  61.         END IF
  62.     END IF
  63.     VIEW PRINT 1 TO 24
  64.     LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed
  65.     LOCATE 22, 1: PRINT myname$ + ": "; mymessage$;
  66.     IF k$ = CHR$(13) THEN ' [Enter] sends the message
  67.         IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program
  68.         mymessage$ = myname$ + ":" + mymessage$
  69.         l = LEN(mymessage$)
  70.         PUT #client, , l
  71.         PUT #client, , mymessage$
  72.         mymessage$ = ""
  73.     END IF
  74.     IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program

THE CLIENT:
Code: QB64: [Select]
  1.  
  2.  
  3. PRINT "[Steve's Mini Messenger]"
  4. client = _OPENCLIENT("TCP/IP:7319:localhost") ' Attempt to connect to local host as a client
  5. PRINT "[connected to " + _CONNECTIONADDRESS(client) + "]"
  6.  
  7. INPUT "Enter your name: ", myname$
  8. out$ = myname$ + " connected!"
  9. l = LEN(out$)
  10. PUT #client, , l
  11. PUT #client, , out$
  12.     GetMessage client
  13.     SendMessage myname$, mymessage$, client ' display current input on screen
  14.     _LIMIT 30
  15.  
  16. '.................... END OF MAIN PROGRAM ................
  17.  
  18.  
  19. SUB GetMessage (client) ' get & display any new message
  20.     GET #client, , l
  21.     IF l > 0 THEN
  22.         out$ = SPACE$(l)
  23.         GET #client, , out$
  24.         VIEW PRINT 1 TO 20
  25.         LOCATE 20, 1
  26.         PRINT out$
  27.         VIEW PRINT 1 TO 24
  28.     ELSE
  29.         out$ = ""
  30.     END IF
  31.  
  32. SUB SendMessage (myname$, mymessage$, client) ' simple input handler
  33.     k$ = INKEY$
  34.     IF LEN(k$) THEN
  35.         IF k$ = CHR$(8) AND LEN(mymessage$) <> 0 THEN
  36.             mymessage$ = LEFT$(mymessage$, LEN(mymessage$) - 1)
  37.         ELSE
  38.             IF LEN(k$) = 1 AND ASC(k$) >= 32 THEN mymessage$ = mymessage$ + k$
  39.         END IF
  40.     END IF
  41.     VIEW PRINT 1 TO 24
  42.     LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed
  43.     LOCATE 22, 1: PRINT myname$ + ": "; mymessage$;
  44.     IF k$ = CHR$(13) THEN ' [Enter] sends the message
  45.         IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program
  46.         mymessage$ = myname$ + ":" + mymessage$
  47.         l = LEN(mymessage$)
  48.         PUT #client, , l
  49.         PUT #client, , mymessage$
  50.         mymessage$ = ""
  51.     END IF
  52.     IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program
  53.  

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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Now that was a refreshing experience. I got in a chat war with myself. You should have seen the insults... ;)