QB64 Phoenix Edition
Inter-Program Data Sharing Demo (by Galleon) - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: Inter-Program Data Sharing Demo (by Galleon) (/showthread.php?tid=2162)



Inter-Program Data Sharing Demo (by Galleon) - SMcNeill - 11-13-2023

Code: (Select All)
SCREEN _NEWIMAGE(512, 512, 256)

DIM a(511, 511) AS INTEGER 'an array we'll send

x = _OPENCLIENT("TCP/IP:1234:localhost") 'try to connect to a host

IF x = 0 THEN '      couldn't be client, so become a host
    'put some data into array a
    FOR xx = 0 TO 511
        FOR yy = 0 TO 511
            a(xx, yy) = xx * yy
        NEXT
    NEXT
    PRINT "(Try running two copies of me at once!)"
    PRINT "Waiting... (Press any key to end)"
    x = _OPENHOST("TCP/IP:1234")
    DO
        z = _OPENCONNECTION(x)
        IF z <> 0 THEN
            PUT #z, , a() 'send array a to any client that connects
            CLOSE z
            PRINT "Array data send to client!"
        END IF
        _LIMIT 10
    LOOP UNTIL INKEY$ <> ""
    PRINT "Finished!"
    END
END IF

'connect to host as a client
PRINT "Conected to host. Reading data..."
DO
    GET #x, , a()
    _LIMIT 100
LOOP UNTIL EOF(x) = 0 'wait until enough data to fill the array arrives
FOR xx = 0 TO 511
    FOR yy = 0 TO 511
        PSET (xx, yy), a(xx, yy)
    NEXT
NEXT
CLOSE x
PRINT "That's how you share data folks!" 'G@lleon

Run it once as a host.  Run it a second time to connect as a client and receive data from that host.