TCP/IP Example Demo for LOCAL HOST/CLIENT Applications
#1
We had a discussion about using _CLIPBOARD to deliver info from one running QB64 app to another. Spriggsy brought up the point that method is frowned upon by M$, which recommends using a TCP/IP routine to pass the info.

So I decided to post a way to communicate back and forth between two QB64 programs.

Notes: You will probably need to tell Windows Defender to okay running these apps, as Defender checks for exe files of this nature.

How to use...

1) Copy/Paste the first program to your QB64 IDE.

2) Open a second IDE and copy/paste the second program.

3) Name and save the second program as; Pete2.bas.

4) Compile Pete2.bas or just run Pete2.bas and close it.

5) Go back to the first IDE, and select "Run" (You don't need to name this app. Untitled works just fine.)

What next? Well, the first program will SHELL open Pete.exe. Now you will have two windows opened. Click the first window, and INPUT 1, 2, or 3 at the prompt. After you hit Enter, you will see your choice appear in the second window as either A, B, C, or if you goofed up it will tell you. The second window sends a message back that it completed the task so the first window can loop back to the INPUT statement. Just close them out with the mouse when you are finished.

Code: (Select All)
_SCREENMOVE 0, 0 ' Set up this host window to the left of your desktop.
WIDTH 60, 25
DO
    CLS
    DO UNTIL x ' Stay in loop until window determines if it is the host or client window.
        x = _OPENCLIENT("TCP/IP:1234:localhost") ' Used to establish a TCP/IP routine.
        IF x = 0 THEN
            x = _OPENHOST("TCP/IP:1234") ' Note the host and clinet must have the same 1234 I.D. number.
            a$ = "Opening as host." ' x channel is now open and this window becomes the host.
        ELSE
            a$ = "Opening as client." ' Should not go here for this demo.
        END IF
        PRINT a$
    LOOP

    IF initiate = 0 THEN ' This only needs to be performed once, to open the client window.
        SHELL _HIDE "start pete2.exe" ' Open the client window.
        initiate = -1 ' Switches this block statement off for all subsequent loops.
    END IF

    IF z = 0 THEN ' Initiates an open channel number when zero.
        DO
            z = _OPENCONNECTION(x) ' Checks if host is available to transfer data.
        LOOP UNTIL z
        PRINT "Connection established."
    END IF

    LOCATE 3, 1 ' Okay, time to input something on the host that will be communicated to the client.
    INPUT "Input a number for letter of the alphabet 1, 2, or 3: "; choice
    _KEYCLEAR

    PUT #z, , choice ' Input is now entered into TCP/IP routine.

    DO
        GET #z, , a
    LOOP UNTIL a = -1 ' -1 is the return code from the client we set as: task_complete = -1
    PUT #z, , a ' Now put our -1 value back into the routine. Failure to do so would result in the client not waiting in the GET #x DO/LOOP.
    CLS
LOOP

Save this one as: Pete2.bas and compile it to Pete2.exe.
Code: (Select All)
_SCREENMOVE 600, 0 ' Set up this client window next to your host window.
WIDTH 50, 25
x = _OPENCLIENT("TCP/IP:1234:localhost") ' Used to establish a TCP/IP routine.
PRINT "Opened as client.": PRINT
DO UNTIL x = 0 ' Prevents running if this app is opened without using host.
    DO
        _LIMIT 30
        GET #x, , receive ' Waits until it receives data input from the host.
    LOOP UNTIL receive > 0

    PRINT "You chose the letter: ";

    SELECT CASE receive
        CASE 1
            PRINT "A"
        CASE 2
            PRINT "B"
        CASE 3
            PRINT "C"
        CASE ELSE
            PRINT "Wrong input!"
    END SELECT
    PRINT

    task_complete = -1 ' Send back a task completed message to the host.
    PUT #x, , task_complete
LOOP


@Spriggsy

Please have a look. I'd like to see if there is anything you would like to comment on, add or optimize. What I'm shooting for is making another example entry in our wiki to expand on the use of these QB64 available communication functions.

Pete
Reply


Messages In This Thread
TCP/IP Example Demo for LOCAL HOST/CLIENT Applications - by Pete - 10-24-2022, 07:30 PM



Users browsing this thread: 10 Guest(s)