Author Topic: TCP/IP local communication sample  (Read 1013 times)

0 Members and 1 Guest are viewing this topic.

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
TCP/IP local communication sample
« on: June 07, 2018, 01:50:48 AM »
Compile, run the binary 2 times simultaneously and watch how they communicate.

Code: QB64: [Select]
  1. DIM SHARED client AS LONG, host AS LONG
  2. SCREEN _NEWIMAGE(500, 500, 32)
  3.  
  4. PRINT "Connecting to host...";
  5. host = _OPENCLIENT("TCP/IP:60710:localhost")
  6. IF host = 0 THEN
  7.  
  8.     'this is the host program
  9.  
  10.     PRINT "not found."
  11.     PRINT "Starting host...";
  12.     host = _OPENHOST("TCP/IP:60710")
  13.     IF host THEN
  14.         PRINT "done."
  15.         DO
  16.             PRINT "Listening on port 60710..."
  17.             DO
  18.                 client = _OPENCONNECTION(host)
  19.                 _LIMIT 30
  20.             LOOP UNTIL client
  21.  
  22.             a$ = "MSG>HELLO!"
  23.             Send client, a$
  24.             PRINT "Connected. Waiting for requests..."
  25.  
  26.             DO
  27.                 incomingData$ = ""
  28.                 GET #client, , incomingData$
  29.                 stream$ = stream$ + incomingData$
  30.  
  31.                 DO WHILE INSTR(stream$, "<END>") > 0
  32.                     'process requests from the client
  33.                     'custom format: REQUEST>data to be sent<END>
  34.  
  35.                     'separate command, data and remove this command from the stream
  36.                     thisData$ = LEFT$(stream$, INSTR(stream$, "<END>") - 1)
  37.                     stream$ = MID$(stream$, INSTR(stream$, "<END>") + 5)
  38.                     thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  39.                     IF thisCommand$ = "" THEN
  40.                         thisCommand$ = thisData$
  41.                     ELSE
  42.                         thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  43.                     END IF
  44.  
  45.                     SELECT CASE UCASE$(thisCommand$)
  46.                         CASE "TIME"
  47.                             myData$ = "MSG>" + TIME$
  48.                             Send client, myData$
  49.                         CASE "DATE"
  50.                             myData$ = "MSG>" + DATE$
  51.                             Send client, myData$
  52.                         CASE "WISDOM"
  53.                             myData$ = "MSG>Time flies like bananas."
  54.                             Send client, myData$
  55.                         CASE "MSG"
  56.                             PRINT "Message from client: "; thisData$
  57.                         CASE "BYE"
  58.                             PRINT thisData$
  59.                             PRINT "Disconnected."
  60.                             myData$ = "MSG>BYE!"
  61.                             Send client, myData$
  62.                             CLOSE client
  63.                             client = 0
  64.                             EXIT DO
  65.                         CASE "MOUSEX"
  66.                             mX = CVI(thisData$)
  67.                         CASE "MOUSEY"
  68.                             mY = CVI(thisData$)
  69.                         CASE "MOUSEDOWN"
  70.                             CIRCLE (mX, mY), 10, _RGB32(255, 150, 0)
  71.                             mouseDown = -1
  72.                         CASE "MOUSEUP"
  73.                             CIRCLE (mX, mY), 10, _RGB32(0, 255, 0)
  74.                             mouseDown = 0
  75.                     END SELECT
  76.                 LOOP
  77.  
  78.                 IF mouseDown THEN
  79.                     IF mX <> oldMx OR mY <> oldMy THEN
  80.                         IF oldMx > 0 AND oldMy > 0 THEN
  81.                             LINE (oldMx, oldMy)-(mX, mY)
  82.                         ELSE
  83.                             PSET (mX, mY)
  84.                         END IF
  85.                         oldMx = mX
  86.                         oldMy = mY
  87.                     END IF
  88.                 ELSE
  89.                     IF mX <> oldMx OR mY <> oldMy THEN
  90.                         oldMx = mX
  91.                         oldMy = mY
  92.                     END IF
  93.                 END IF
  94.  
  95.                 _LIMIT 30
  96.             LOOP WHILE client
  97.         LOOP
  98.     ELSE
  99.         PRINT "failed."
  100.     END IF
  101.  
  102.     'this is the client program
  103.  
  104.     PRINT
  105.     PRINT "Connected."
  106.     PRINT "Click around to send mouse data; hit space to enter command..."
  107.  
  108.     DO
  109.         DO
  110.             incomingData$ = ""
  111.             GET #host, , incomingData$
  112.             stream$ = stream$ + incomingData$
  113.         LOOP WHILE LEN(incomingData$)
  114.  
  115.         DO WHILE INSTR(stream$, "<END>") > 0
  116.             'process responses from the host
  117.             'custom format: RESPONSE>data received<END>
  118.  
  119.             'separate command, data and remove this command from the stream
  120.             thisData$ = LEFT$(stream$, INSTR(stream$, "<END>") - 1)
  121.             thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  122.             thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  123.             stream$ = MID$(stream$, INSTR(stream$, "<END>") + 5)
  124.  
  125.             SELECT CASE UCASE$(thisCommand$)
  126.                 CASE "MSG"
  127.                     PRINT "Message from host: "; thisData$
  128.                     IF thisData$ = "BYE!" THEN
  129.                         PRINT "Disconnected."
  130.                         CLOSE host
  131.                         END
  132.                     END IF
  133.             END SELECT
  134.         LOOP
  135.  
  136.         DO WHILE _MOUSEINPUT
  137.             mX = _MOUSEX
  138.             mY = _MOUSEY
  139.             mb = _MOUSEBUTTON(1)
  140.         LOOP
  141.  
  142.         IF mX <> oldMx OR mY <> oldMy THEN
  143.             oldMx = mX
  144.             oldMy = mY
  145.             c$ = "MOUSEX>" + MKI$(mX)
  146.             Send host, c$
  147.             c$ = "MOUSEY>" + MKI$(mY)
  148.             Send host, c$
  149.         END IF
  150.  
  151.         IF mb THEN
  152.             IF NOT mouseDown THEN
  153.                 c$ = "MOUSEDOWN>" + MKI$(1)
  154.                 Send host, c$
  155.                 mouseDown = -1
  156.             END IF
  157.         ELSE
  158.             IF mouseDown THEN
  159.                 c$ = "MOUSEUP>" + MKI$(1)
  160.                 Send host, c$
  161.                 mouseDown = 0
  162.             END IF
  163.         END IF
  164.  
  165.  
  166.         IF INKEY$ = " " THEN
  167.             INPUT "Command (TIME, DATE, WISDOM, MSG>your message, BYE:", c$
  168.  
  169.             IF INSTR(c$, ">") = 0 THEN c$ = c$ + ">"
  170.             Send host, c$
  171.             c$ = ""
  172.         END IF
  173.  
  174.         _LIMIT 30
  175.     LOOP WHILE host
  176.  
  177. SUB Send (channel, __theData$)
  178.     theData$ = __theData$ + "<END>"
  179.     PUT #channel, , theData$
  180.  
« Last Edit: June 07, 2018, 02:00:03 PM by FellippeHeitor »

Offline commandvom

  • Newbie
  • Posts: 10
Re: TCP/IP local communication sample
« Reply #1 on: June 07, 2018, 11:29:55 AM »
Good code!  I will borrow some ideas to migrate my project to GL ambient! Thanks!

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
Re: TCP/IP local communication sample
« Reply #2 on: June 07, 2018, 12:09:45 PM »
I will be delighted if my small contribution is useful for your awesome game to be able to be compiled with the latest version of QB64, commandvom!

Looking forward to hearing from you about how the transition goes.