Author Topic: TCP/IP client/host error (Advanced question, need help)  (Read 4744 times)

0 Members and 1 Guest are viewing this topic.

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
TCP/IP client/host error (Advanced question, need help)
« on: August 06, 2018, 02:06:25 pm »
Ok, not so long ago FellippeHeitor made a client/host networking demo, I talked to him on IRC about the gui I'm working on (for probably the 50 millionth time). So he modified the version he posted in sample programs to show how it would work to have a client program update a displayed window in the host program. So long story short, I went hacking crazy on it, and it works great for localhost connections (see screenshot). But... when I try to run the host on one LAN node, and the client on a different node, it never works properly and gives me this error:

Critical Error #300

Line: 334 (In main module)
Memory region out of range

See attachment for picture of error message...

Now I have my theories on why its not working, but i have yet to figure it out and fix it.
(LINE 334 is _MEMPUT, the error has something to do with _MEM, I think. I think the issue is that the client and host programs might be assuming that they share the same computer, so I think one of the programs isn't allocating it's memory properly)

If someone knows alot about TCP/IP and can point me in the right direction for fixing this issue, let me know.

Here is the code:
Code: QB64: [Select]
  1. TYPE aDisplayOption
  2.     isFullscreen AS _BYTE
  3.     isScaled AS _BYTE
  4.     isSmoothed AS _BYTE
  5.  
  6. TYPE aDisplay
  7.     sizeX AS _UNSIGNED INTEGER
  8.     sizeY AS _UNSIGNED INTEGER
  9.     aspectRatio AS SINGLE
  10.     Options AS aDisplayOption
  11.     Handle AS _UNSIGNED LONG
  12.  
  13. TYPE aBackgroundOption
  14.     isCentered AS _BYTE
  15.     isScaled AS _BYTE
  16.     isTiled AS _BYTE
  17.     isSolidColour AS _BYTE
  18.     theColour AS _UNSIGNED LONG
  19.  
  20. TYPE aBackground
  21.     Options AS aBackgroundOption
  22.     Handle AS _UNSIGNED LONG
  23.  
  24. TYPE aLimit
  25.     Maximum AS _UNSIGNED _INTEGER64
  26.     Minimum AS _UNSIGNED _INTEGER64
  27.  
  28. TYPE aSize
  29.     Titles AS _UNSIGNED _INTEGER64
  30.     Canvases AS _UNSIGNED _INTEGER64
  31.     Windows AS _UNSIGNED _INTEGER64
  32.     Zones AS _UNSIGNED _INTEGER64
  33.  
  34. TYPE aGrayscale
  35.     Black AS _UNSIGNED LONG
  36.     Darkest AS _UNSIGNED LONG
  37.     Darker AS _UNSIGNED LONG
  38.     Dark AS _UNSIGNED LONG
  39.     Neutral AS _UNSIGNED LONG
  40.     Light AS _UNSIGNED LONG
  41.     Lighter AS _UNSIGNED LONG
  42.     Lightest AS _UNSIGNED LONG
  43.     White AS _UNSIGNED LONG
  44.  
  45. TYPE aPrimaryColour
  46.     Red AS _UNSIGNED LONG
  47.     Green AS _UNSIGNED LONG
  48.     Blue AS _UNSIGNED LONG
  49.  
  50. TYPE aSecondaryColour
  51.     Cyan AS _UNSIGNED LONG
  52.     Magenta AS _UNSIGNED LONG
  53.     Yellow AS _UNSIGNED LONG
  54.  
  55. TYPE aTertiaryColour
  56.     Azure AS _UNSIGNED LONG
  57.     Violet AS _UNSIGNED LONG
  58.     Rose AS _UNSIGNED LONG
  59.     Orange AS _UNSIGNED LONG
  60.     Chartreuse AS _UNSIGNED LONG
  61.     springGreen AS _UNSIGNED LONG
  62.  
  63. TYPE aPalette
  64.     Grayscale AS aGrayscale
  65.     Primary AS aPrimaryColour
  66.     Secondary AS aSecondaryColour
  67.     Tertiary AS aTertiaryColour
  68.  
  69. TYPE aThemePalette
  70.     activeTitlebar AS _UNSIGNED LONG
  71.     inactiveTitlebar AS _UNSIGNED LONG
  72.     resizeBox AS _UNSIGNED LONG
  73.  
  74. TYPE aCursor
  75.     positionX AS INTEGER
  76.     positionY AS INTEGER
  77.     leftClicked AS _BYTE
  78.     rightClicked AS _BYTE
  79.     imageHandle AS _UNSIGNED LONG
  80.  
  81. TYPE CLIENT
  82.     id AS LONG
  83.     handle AS LONG
  84.     active AS _BYTE
  85.     image AS LONG
  86.     x AS INTEGER
  87.     y AS INTEGER
  88.     w AS INTEGER
  89.     h AS INTEGER
  90.     ping AS SINGLE
  91.  
  92. DIM SHARED theLimit AS aLimit
  93. theLimit.Minimum = 0
  94. theLimit.Maximum = 0 - 1
  95.  
  96. DIM SHARED theSize AS aSize
  97. theSize.Titles = theLimit.Minimum
  98. theSize.Canvases = theLimit.Minimum
  99. theSize.Windows = theLimit.Minimum
  100. theSize.Zones = theLimit.Minimum
  101.  
  102. DIM SHARED theDisplay AS aDisplay
  103. theDisplay.Options.isScaled = 0
  104. theDisplay.Options.isSmoothed = -1
  105.  
  106. DIM SHARED theBackground AS aBackground
  107.  
  108. ' Declare RGB Colors (Primary, Secondary, and Tertiary)
  109. DIM SHARED thePalette AS aPalette
  110.  
  111. ' Initialize Grayscales (8 Brightnesses)
  112. thePalette.Grayscale.Black = _RGBA32(0, 0, 0, 255)
  113. thePalette.Grayscale.Darkest = _RGBA32(31, 31, 31, 255)
  114. thePalette.Grayscale.Darker = _RGBA32(63, 63, 63, 255)
  115. thePalette.Grayscale.Dark = _RGBA32(95, 95, 95, 255)
  116. thePalette.Grayscale.Neutral = _RGBA32(127, 127, 127, 255)
  117. thePalette.Grayscale.Light = _RGBA32(159, 159, 159, 255)
  118. thePalette.Grayscale.Lighter = _RGBA32(191, 191, 191, 255)
  119. thePalette.Grayscale.Lightest = _RGBA32(223, 223, 223, 255)
  120. thePalette.Grayscale.White = _RGBA32(255, 255, 255, 255)
  121.  
  122. ' Initialize Colors (Primary, Secondary, and Tertiary)
  123. thePalette.Primary.Red = _RGBA32(255, 0, 0, 255)
  124. thePalette.Primary.Green = _RGBA32(0, 255, 0, 255)
  125. thePalette.Primary.Blue = _RGBA32(0, 0, 255, 255)
  126. thePalette.Secondary.Cyan = thePalette.Primary.Green + thePalette.Primary.Blue
  127. thePalette.Secondary.Magenta = thePalette.Primary.Red + thePalette.Primary.Blue
  128. thePalette.Secondary.Yellow = thePalette.Primary.Red + thePalette.Primary.Green
  129. thePalette.Tertiary.Azure = thePalette.Primary.Blue + thePalette.Secondary.Cyan
  130. thePalette.Tertiary.Violet = thePalette.Primary.Blue + thePalette.Secondary.Magenta
  131. thePalette.Tertiary.Rose = thePalette.Primary.Red + thePalette.Secondary.Magenta
  132. thePalette.Tertiary.springGreen = thePalette.Primary.Green + thePalette.Secondary.Cyan
  133. thePalette.Tertiary.Orange = thePalette.Primary.Red + thePalette.Secondary.Yellow
  134. thePalette.Tertiary.Chartreuse = thePalette.Primary.Green + thePalette.Secondary.Yellow
  135.  
  136. ' Declare Theme colors
  137. DIM SHARED theTheme AS aThemePalette
  138.  
  139. ' Initialize Theme colors
  140. theTheme.activeTitlebar = thePalette.Tertiary.Azure + thePalette.Secondary.Magenta
  141. theTheme.inactiveTitlebar = thePalette.Tertiary.Azure + thePalette.Tertiary.springGreen
  142. theTheme.resizeBox = thePalette.Grayscale.Light
  143.  
  144. DIM SHARED overlayColor AS _UNSIGNED LONG: overlayColor = _RGBA32(255, 100, 0, 100)
  145.  
  146. ' Declare global arrays
  147. REDIM SHARED theTitle(theSize.Titles) AS STRING
  148.  
  149. ' Declare mouse cursor variable
  150. DIM SHARED theMouse AS aCursor
  151.  
  152. DIM SHARED aspectRatio AS _FLOAT
  153.  
  154. DISPLAY_aspectRatio 1.33, 1
  155. DISPLAY_init INT((_DESKTOPHEIGHT * aspectRatio)), _DESKTOPHEIGHT, -1
  156.  
  157. BACKGROUND_init thePalette.Primary.Red, thePalette.Tertiary.Orange, "CBOX"
  158. BACKGROUND_image "ggravity.jpg", "CENTER"
  159. BACKGROUND_put
  160. CONST false = 0, true = NOT false
  161.  
  162.     IF attempts > 1000 THEN PRINT "Unable to start as host.": END
  163.     host = _OPENHOST("TCP/IP:65535")
  164.     attempts = attempts + 1
  165. LOOP UNTIL host
  166.  
  167. Status "Listening on port 65535..."
  168.  
  169. maxConnections = 65535 - 1024
  170. DIM SHARED client(1 TO maxConnections) AS CLIENT
  171. DIM SHARED stream(1 TO maxConnections) AS STRING
  172. DIM SHARED totalClients AS INTEGER, totalIDs AS LONG
  173.  
  174.     MOUSE_probe
  175.     'detect which client is being hovered
  176.     IF theMouse.positionX <> oldMx OR theMouse.positionY <> oldMy THEN
  177.         hover = 0
  178.         oldMx = theMouse.positionX
  179.         oldMy = theMouse.positionY
  180.         FOR i = totalClients TO 1 STEP -1
  181.             IF theMouse.positionX >= client(i).x AND theMouse.positionX <= client(i).x + client(i).w AND theMouse.positionY >= client(i).y AND theMouse.positionY <= client(i).y + client(i).h THEN
  182.                 IF client(i).active THEN
  183.                     hover = i
  184.                     EXIT FOR
  185.                 END IF
  186.             END IF
  187.         NEXT
  188.     END IF
  189.  
  190.     IF theMouse.leftClicked THEN
  191.         IF NOT mouseDown THEN
  192.             mouseDown = true
  193.             IF hover < totalClients THEN
  194.                 SWAP client(hover), client(totalClients)
  195.                 hover = totalClients
  196.             END IF
  197.             IF hover > 0 THEN
  198.                 dragging = hover: dragX = theMouse.positionX: dragY = theMouse.positionY
  199.                 originalX = theMouse.positionX: originalY = theMouse.positionY
  200.                 Status "Dragging client #" + STR$(dragging)
  201.             END IF
  202.         ELSE
  203.             IF dragging THEN
  204.                 client(dragging).x = client(dragging).x + (theMouse.positionX - dragX)
  205.                 client(dragging).y = client(dragging).y + (theMouse.positionY - dragY)
  206.                 dragX = theMouse.positionX
  207.                 dragY = theMouse.positionY
  208.             END IF
  209.         END IF
  210.     ELSE
  211.         IF mouseDown THEN
  212.             IF dragging THEN
  213.                 IF theMouse.positionX = originalX AND themousepositiony = originalY THEN
  214.                     'just a click
  215.                     b$ = "CLICK>" + MKI$(theMouse.positionX - client(dragging).x) + MKI$(theMouse.positionY - client(dragging).y)
  216.                     Send client(dragging).handle, b$
  217.                 END IF
  218.                 dragging = false
  219.             END IF
  220.             Status "Idle."
  221.             mouseDown = false
  222.         END IF
  223.     END IF
  224.  
  225.     IF theMouse.rightClicked THEN
  226.         IF NOT mouse2down THEN mouse2down = true
  227.     ELSE
  228.         IF mouse2down THEN
  229.             mouse2down = false
  230.             IF hover > 0 THEN
  231.                 'close this client
  232.                 closeClient hover
  233.             END IF
  234.         END IF
  235.     END IF
  236.  
  237.     IF totalClients < UBOUND(client) THEN
  238.         'look for new clients until limit is reached
  239.         newClient = _OPENCONNECTION(host)
  240.         IF newClient THEN
  241.             Status "new client connected; handshaking..."
  242.             _DISPLAY
  243.  
  244.             totalClients = totalClients + 1
  245.  
  246.             client(totalClients).handle = newClient
  247.             Send newClient, "MSG>HELLO!"
  248.  
  249.             start! = TIMER
  250.             reply = false
  251.             DO
  252.                 GET newClient, , incomingData$
  253.                 stream(totalClients) = stream(totalClients) + incomingData$
  254.                 IF INSTR(stream(totalClients), "<END>") THEN reply = true: EXIT DO
  255.             LOOP UNTIL TIMER - start! > 5 'timeout
  256.  
  257.             IF reply THEN
  258.                 thisData$ = LEFT$(stream(totalClients), INSTR(stream(totalClients), "<END>") - 1)
  259.                 stream(totalClients) = MID$(stream(totalClients), INSTR(stream(totalClients), "<END>") + 5)
  260.                 thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  261.                 IF thisCommand$ = "" THEN
  262.                     thisCommand$ = thisData$
  263.                 ELSE
  264.                     thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  265.                 END IF
  266.  
  267.                 IF thisCommand$ = "HELLO" THEN
  268.                     client(totalClients).x = RND * (_WIDTH / 3)
  269.                     client(totalClients).y = RND * (_HEIGHT / 3)
  270.                     client(totalClients).w = CVI(LEFT$(thisData$, 2))
  271.                     client(totalClients).h = CVI(RIGHT$(thisData$, 2))
  272.                     client(totalClients).active = true
  273.                     client(totalClients).ping = TIMER
  274.                     totalIDs = totalIDs + 1
  275.                     client(totalClients).id = totalIDs
  276.                 ELSE
  277.                     GOTO failed
  278.                 END IF
  279.             ELSE
  280.                 failed:
  281.                 Status "Connection failed!"
  282.                 totalClients = totalClients - 1
  283.             END IF
  284.         END IF
  285.     END IF
  286.  
  287.     CLS
  288.     BACKGROUND_put
  289.     COLOR _RGB32(255, 255, 255), _RGB32(0, 0, 0)
  290.     PRINT "total clients:"; totalClients
  291.  
  292.     Status ""
  293.  
  294.     FOR i = 1 TO totalClients
  295.         IF client(i).active THEN
  296.             b$ = "PING>"
  297.             Send client(i).handle, b$
  298.  
  299.             GET client(i).handle, , incomingData$
  300.             stream(i) = stream(i) + incomingData$
  301.  
  302.             DO WHILE INSTR(stream(i), "<END>")
  303.                 thisData$ = LEFT$(stream(i), INSTR(stream(i), "<END>") - 1)
  304.                 stream(i) = MID$(stream(i), INSTR(stream(i), "<END>") + 5)
  305.                 thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  306.                 IF thisCommand$ = "" THEN
  307.                     thisCommand$ = thisData$
  308.                 ELSE
  309.                     thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  310.                 END IF
  311.  
  312.                 SELECT CASE thisCommand$
  313.                     CASE "IMAGE"
  314.                         IF client(i).image < -1 THEN _FREEIMAGE client(i).image
  315.                         client(i).image = _NEWIMAGE(client(i).w, client(i).h, 32)
  316.                         DIM imgMem AS _MEM
  317.                         imgMem = _MEMIMAGE(client(i).image)
  318.                         _MEMPUT imgMem, imgMem.OFFSET, thisData$
  319.                         _MEMFREE imgMem
  320.                     CASE "BYE!"
  321.                         IF client(i).image < -1 THEN _FREEIMAGE client(i).image
  322.                         client(i).active = false
  323.                         totalClients = totalClients - 1
  324.                         GOTO nextClient
  325.                     CASE "PONG"
  326.  
  327.                         'show ping:
  328.                         FOR c = 1 TO 20 STEP 2
  329.                             IF TIMER - client(i).ping >= 20 THEN
  330.                                 CIRCLE (client(i).x, client(i).y), c, _RGB32(255, 0, 0)
  331.                             ELSEIF TIMER - client(i).ping >= 10 AND TIMER - client(i).ping < 20 THEN
  332.                                 CIRCLE (client(i).x, client(i).y), c, _RGB32(255, 255, 0)
  333.                             ELSEIF TIMER - client(i).ping < 10 THEN
  334.                                 CIRCLE (client(i).x, client(i).y), c, _RGB32(0, 255, 0)
  335.                             END IF
  336.                         NEXT
  337.                         client(i).ping = TIMER
  338.                 END SELECT
  339.             LOOP
  340.  
  341.             box client(i).x, client(i).y, client(i).w - 1, client(i).h - 1, 1
  342.             Titlebar client(i).x + 2, client(i).y + 2, client(i).w - 5, 23, thePalette.Grayscale.Black
  343.             box2 client(i).x + (client(i).w - 48), client(i).y + 3, 20, 20, 1, 7
  344.             box2 client(i).x + (client(i).w - 25), client(i).y + 3, 20, 20, 1, 2
  345.             box3 client(i).x + 4, client(i).y + 3, 20, 20, 1, 1, 7
  346.             LINE (client(i).x, client(i).y)-(client(i).x + client(i).w - 1, client(i).y + client(i).h - 1), mycolor~&, BF
  347.  
  348.             'window title
  349.             t$ = "Client #" + LTRIM$(STR$(client(i).id))
  350.             COLOR _RGB32(0, 0, 0), 0
  351.             _PRINTSTRING (client(i).x + 31, client(i).y + 5), t$
  352.             COLOR _RGB32(255, 255, 255), 0
  353.             _PRINTSTRING (client(i).x + 30, client(i).y + 5), t$
  354.  
  355.             'window contents
  356.             IF client(i).image < -1 THEN
  357.                 _PUTIMAGE (client(i).x + 2, client(i).y + 26)-(client(i).x + client(i).w - 2, client(i).y + client(i).h - 2), client(i).image
  358.             END IF
  359.  
  360.             IF TIMER - client(i).ping > 30 THEN
  361.                 closeClient i
  362.             END IF
  363.         END IF
  364.         nextClient:
  365.     NEXT
  366.  
  367.     _DISPLAY
  368.     _LIMIT 30
  369.  
  370. SUB DISPLAY_aspectRatio (inWidth AS _FLOAT, inHeight AS _FLOAT)
  371.     aspectRatio = inWidth / inHeight
  372.  
  373. SUB DISPLAY_init (inSizeX AS _UNSIGNED INTEGER, inSizeY AS _UNSIGNED INTEGER, inIsFullscreen AS _BYTE)
  374.     theDisplay.sizeX = inSizeX
  375.     theDisplay.sizeY = inSizeY
  376.     theDisplay.Options.isFullscreen = inIsFullscreen
  377.     theDisplay.Handle = _NEWIMAGE(theDisplay.sizeX, theDisplay.sizeY, 32)
  378.     SCREEN theDisplay.Handle
  379.     IF inIsFullscreen THEN
  380.         DISPLAY_fullscreen theDisplay.Options.isScaled, theDisplay.Options.isSmoothed
  381.     END IF
  382.  
  383. SUB DISPLAY_fullscreen (inScaled AS _BYTE, inSmoothed AS _BYTE)
  384.     IF inScaled THEN
  385.         IF inSmoothed THEN
  386.             theDisplay.Options.isScaled = -1
  387.             theDisplay.Options.isSmoothed = -1
  388.             IF theDisplay.Options.isScaled = -1 AND theDisplay.Options.isSmoothed = -1 THEN
  389.                 _FULLSCREEN _STRETCH , _SMOOTH
  390.             END IF
  391.         ELSE
  392.             theDisplay.Options.isScaled = -1
  393.             theDisplay.Options.isSmoothed = 0
  394.             IF theDisplay.Options.isScaled = -1 AND theDisplay.Options.isSmoothed = 0 THEN
  395.                 _FULLSCREEN _STRETCH
  396.             END IF
  397.         END IF
  398.     ELSE
  399.         IF inSmoothed THEN
  400.             theDisplay.Options.isScaled = 0
  401.             theDisplay.Options.isSmoothed = -1
  402.             IF theDisplay.Options.isScaled = 0 AND theDisplay.Options.isSmoothed = -1 THEN
  403.                 _FULLSCREEN _SQUAREPIXELS , _SMOOTH
  404.             END IF
  405.         ELSE
  406.             theDisplay.Options.isScaled = 0
  407.             theDisplay.Options.isSmoothed = 0
  408.             IF theDisplay.Options.isScaled = 0 AND theDisplay.Options.isSmoothed = 0 THEN
  409.                 _FULLSCREEN _SQUAREPIXELS
  410.             END IF
  411.         END IF
  412.     END IF
  413.  
  414. SUB BACKGROUND_add
  415.     IF NOT theBackground.Handle THEN
  416.         theBackground.Handle = _NEWIMAGE(_WIDTH(theDisplay.Handle), _HEIGHT(theDisplay.Handle), 32)
  417.     END IF
  418.  
  419. SUB BACKGROUND_init (inColor1 AS _UNSIGNED LONG, inColor2 AS _UNSIGNED LONG, inOption AS STRING)
  420.     BACKGROUND_add
  421.     SELECT CASE inOption
  422.         CASE "HLINE"
  423.             BACKGROUND_horizontalLines inColor1, inColor2
  424.         CASE "VLINE"
  425.             BACKGROUND_verticalLines inColor1, inColor2
  426.         CASE "CBOX"
  427.             BACKGROUND_checkerBox inColor1, inColor2
  428.         CASE "COLOR"
  429.             BACKGROUND_colour inColor1: inColor2 = 0
  430.     END SELECT
  431.  
  432. SUB BACKGROUND_image (inFilename AS STRING, inOption AS STRING)
  433.     inFilename = LTRIM$(RTRIM$(inFilename))
  434.     IF LEN(inFilename) THEN
  435.         IF _FILEEXISTS(inFilename) THEN
  436.             DIM Temporary AS _UNSIGNED LONG
  437.             Temporary = _LOADIMAGE(inFilename, 32)
  438.         ELSE
  439.             EXIT SUB
  440.         END IF
  441.     END IF
  442.     _DEST theBackground.Handle
  443.     inOption = LTRIM$(RTRIM$(UCASE$(inOption)))
  444.     SELECT CASE inOption
  445.         CASE "CENTER"
  446.             DIM centeredX AS _UNSIGNED INTEGER: centeredX = (_WIDTH(theDisplay.Handle) - _WIDTH(Temporary)) / 2
  447.             DIM centeredY AS _UNSIGNED INTEGER: centeredY = (_HEIGHT(theDisplay.Handle) - _HEIGHT(Temporary)) / 2
  448.             _PUTIMAGE (centeredX, centeredY)-(_WIDTH(Temporary) + centeredX, _HEIGHT(Temporary) + centeredY), Temporary, theBackground.Handle
  449.             theBackground.Options.isCentered = -1
  450.         CASE "SCALE"
  451.             _PUTIMAGE (0, 0)-(_WIDTH(theDisplay.Handle), _HEIGHT(theDisplay.Handle)), Temporary, theBackground.Handle
  452.             theBackground.Options.isScaled = -1
  453.         CASE "TILE"
  454.             DIM numberWide AS SINGLE: numberWide = _WIDTH / _WIDTH(Temporary)
  455.             DIM numberHigh AS SINGLE: numberHigh = _HEIGHT / _HEIGHT(Temporary)
  456.             DIM currentTileY AS _UNSIGNED INTEGER: FOR currentTileY = 0 TO numberHigh
  457.                 DIM currentTileX AS _UNSIGNED INTEGER: FOR currentTileX = 0 TO numberWide
  458.                     _PUTIMAGE (_WIDTH(Temporary) * currentTileX, _HEIGHT(Temporary) * currentTileY), Temporary, theBackground.Handle
  459.                 NEXT currentTileX
  460.             NEXT currentTileY
  461.             theBackground.Options.isTiled = -1
  462.     END SELECT
  463.     _DEST theDisplay.Handle
  464.     _FREEIMAGE Temporary
  465.  
  466. SUB BACKGROUND_colour (inColour AS _UNSIGNED LONG)
  467.     _DEST theBackground.Handle
  468.     LINE (0, 0)-(_WIDTH(theBackground.Handle), _HEIGHT(theBackground.Handle)), inColour, BF
  469.  
  470. SUB BACKGROUND_checkerBox (inColor1 AS _UNSIGNED LONG, inColor2 AS _UNSIGNED LONG)
  471.     _DEST theBackground.Handle
  472.     FOR j = 1 TO _HEIGHT
  473.         IF j MOD 2 = 1 THEN
  474.             FOR i = 1 TO _WIDTH
  475.                 IF i MOD 2 = 1 THEN
  476.                     PSET (i, j), inColor2
  477.                 ELSE
  478.                     PSET (i, j), inColor1
  479.                 END IF
  480.             NEXT i
  481.         ELSE
  482.             FOR i = 1 TO _WIDTH
  483.                 IF i MOD 2 = 0 THEN
  484.                     PSET (i, j), inColor2
  485.                 ELSE
  486.                     PSET (i, j), inColor1
  487.                 END IF
  488.             NEXT i
  489.         END IF
  490.     NEXT j
  491.  
  492. SUB BACKGROUND_verticalLines (inColor1 AS _UNSIGNED LONG, inColor2 AS _UNSIGNED LONG)
  493.     _DEST theBackground.Handle
  494.     FOR j = 1 TO _HEIGHT
  495.         FOR i = 1 TO _WIDTH
  496.             IF i MOD 2 = 1 THEN
  497.                 PSET (i, j), inColor2
  498.             ELSE
  499.                 PSET (i, j), inColor1
  500.             END IF
  501.         NEXT i
  502.     NEXT j
  503.  
  504. SUB BACKGROUND_horizontalLines (inColor1 AS _UNSIGNED LONG, inColor2 AS _UNSIGNED LONG)
  505.     _DEST theBackground.Handle
  506.     FOR j = 1 TO _HEIGHT
  507.         FOR i = 1 TO _WIDTH
  508.             IF j MOD 2 = 1 THEN
  509.                 PSET (i, j), inColor2
  510.             ELSE
  511.                 PSET (i, j), inColor1
  512.             END IF
  513.         NEXT i
  514.     NEXT j
  515.  
  516. SUB BACKGROUND_put
  517.     IF theBackground.Handle THEN
  518.         _PUTIMAGE , theBackground.Handle, theDisplay.Handle
  519.     END IF
  520.  
  521. SUB Titlebar (titlebarPositionX AS _UNSIGNED INTEGER, titlebarPositionY AS _UNSIGNED INTEGER, titlebarWidth AS _UNSIGNED INTEGER, titlebarHeight AS _UNSIGNED INTEGER, titlebarColor AS _UNSIGNED LONG)
  522.     LINE (titlebarPositionX, titlebarPositionY)-(titlebarPositionX + titlebarWidth, titlebarPositionY + titlebarHeight), theTheme.activeTitlebar, BF
  523.  
  524. SUB box (boxPositionX AS _UNSIGNED INTEGER, boxPositionY AS _UNSIGNED INTEGER, boxWidth AS _UNSIGNED INTEGER, boxHeight AS _UNSIGNED INTEGER, boxDepth AS _UNSIGNED INTEGER)
  525.     LINE (boxPositionX, boxPositionY)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Lighter, BF
  526.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Darker, BF
  527.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-((boxPositionX + boxWidth) - boxDepth, (boxPositionY + boxHeight) - boxDepth), thePalette.Grayscale.Neutral, BF
  528.  
  529. SUB box2 (boxPositionX AS _UNSIGNED INTEGER, boxPositionY AS _UNSIGNED INTEGER, boxWidth AS _UNSIGNED INTEGER, boxHeight AS _UNSIGNED INTEGER, boxDepth AS _UNSIGNED INTEGER, interiorDepth AS _UNSIGNED INTEGER)
  530.     interiorDepth = interiorDepth * boxDepth
  531.     LINE (boxPositionX, boxPositionY)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Darker, BF
  532.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Lighter, BF
  533.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-((boxPositionX + boxWidth) - boxDepth, (boxPositionY + boxHeight) - boxDepth), thePalette.Grayscale.Neutral, BF
  534.     LINE (boxPositionX + boxDepth + interiorDepth, boxPositionY + boxDepth + interiorDepth)-(boxPositionX + boxWidth - boxDepth - interiorDepth, boxPositionY + boxHeight - boxDepth - interiorDepth), thePalette.Grayscale.Lighter, BF
  535.     LINE (boxPositionX + (boxDepth * 2) + interiorDepth, boxPositionY + (boxDepth * 2) + interiorDepth)-(boxPositionX + boxWidth - boxDepth - interiorDepth, boxPositionY + boxHeight - boxDepth - interiorDepth), thePalette.Grayscale.Darker, BF
  536.     LINE (boxPositionX + (boxDepth * 2) + interiorDepth, boxPositionY + (boxDepth * 2) + interiorDepth)-(boxPositionX + boxWidth - (boxDepth * 2) - interiorDepth, boxPositionY + boxHeight - (boxDepth * 2) - interiorDepth), thePalette.Grayscale.Neutral, BF
  537.  
  538. SUB box3 (boxPositionX AS _UNSIGNED INTEGER, boxPositionY AS _UNSIGNED INTEGER, boxWidth AS _UNSIGNED INTEGER, boxHeight AS _UNSIGNED INTEGER, boxDepth AS _UNSIGNED INTEGER, interiorDepthX AS _UNSIGNED INTEGER, interiorDepthY AS _UNSIGNED INTEGER)
  539.     interiorDepthX = interiorDepthX * boxDepth
  540.     interiorDepthY = interiorDepthY * boxDepth
  541.     LINE (boxPositionX, boxPositionY)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Darker, BF
  542.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-(boxPositionX + boxWidth, boxPositionY + boxHeight), thePalette.Grayscale.Lighter, BF
  543.     LINE (boxPositionX + boxDepth, boxPositionY + boxDepth)-((boxPositionX + boxWidth) - boxDepth, (boxPositionY + boxHeight) - boxDepth), thePalette.Grayscale.Neutral, BF
  544.     LINE (boxPositionX + boxDepth + interiorDepthX, boxPositionY + boxDepth + interiorDepthY)-(boxPositionX + boxWidth - boxDepth - interiorDepthX, boxPositionY + boxHeight - boxDepth - interiorDepthY), thePalette.Grayscale.Lighter, BF
  545.     LINE (boxPositionX + (boxDepth * 2) + interiorDepthX, boxPositionY + (boxDepth * 2) + interiorDepthY)-(boxPositionX + boxWidth - boxDepth - interiorDepthX, boxPositionY + boxHeight - boxDepth - interiorDepthY), thePalette.Grayscale.Darker, BF
  546.     LINE (boxPositionX + (boxDepth * 2) + interiorDepthX, boxPositionY + (boxDepth * 2) + interiorDepthY)-(boxPositionX + boxWidth - (boxDepth * 2) - interiorDepthX, boxPositionY + boxHeight - (boxDepth * 2) - interiorDepthY), thePalette.Grayscale.Neutral, BF
  547.  
  548. SUB MOUSE_probe
  549.     DO
  550.         theMouse.positionX = _MOUSEX
  551.         theMouse.positionY = _MOUSEY
  552.         theMouse.leftClicked = _MOUSEBUTTON(1)
  553.         theMouse.rightClicked = _MOUSEBUTTON(2)
  554.  
  555. SUB closeClient (ID AS LONG)
  556.     b$ = "BYE!>"
  557.     Send client(ID).handle, b$
  558.     _DELAY .1 'give time for BYE! to be sent
  559.     CLOSE client(ID).handle
  560.     client(ID).active = false
  561.     IF client(ID).image < -1 THEN _FREEIMAGE client(ID).image: client(ID).image = 0
  562.     FOR i = ID + 1 TO totalClients
  563.         client(i - 1) = client(i)
  564.     NEXT
  565.     totalClients = totalClients - 1
  566.  
  567. SUB Send (channel, __theData$)
  568.     theData$ = __theData$ + "<END>"
  569.     PUT #channel, , theData$
  570.  
  571. SUB Status (__text$)
  572.     STATIC lastStatus$
  573.  
  574.     IF LEN(__text$) THEN lastStatus$ = __text$
  575.     IF LEN(lastStatus$) = 0 THEN EXIT SUB
  576.  
  577.     LINE (0, _HEIGHT - 2 - _FONTHEIGHT)-(_WIDTH, _HEIGHT), _RGB32(194, 194, 194), BF
  578.     COLOR _RGB32(0, 0, 0), _RGB32(194, 194, 194)
  579.     _PRINTSTRING (5, (_HEIGHT - _FONTHEIGHT - 1)), lastStatus$

Code: QB64: [Select]
  1. 'client
  2. 'run several copies of this client to see the interaction
  3. 'with the host
  4. CONST false = 0, true = NOT false
  5.  
  6.  
  7. myWidth% = RND * 400
  8. IF myWidth% < 150 THEN myWidth% = 150
  9. myHeight% = RND * 250
  10. IF myHeight% < 150 THEN myHeight% = 150
  11. myColor~& = _RGBA32(RND * 255, RND * 255, RND * 255, 50)
  12.  
  13. SCREEN _NEWIMAGE(myWidth%, myHeight%, 32)
  14.  
  15. PRINT "Looking for host..."
  16. host = _OPENCLIENT("TCP/IP:65535:localhost")
  17. IF host = 0 THEN SYSTEM
  18.  
  19. PRINT "Connected to host; handshaking..."
  20.  
  21. start! = TIMER
  22. reply = false
  23.     GET host, , incomingData$
  24.     stream$ = stream$ + incomingData$
  25.     IF INSTR(stream$, "<END>") THEN reply = true: EXIT DO
  26. LOOP UNTIL TIMER - start! > 5 'timeout
  27.  
  28. IF reply THEN
  29.     thisData$ = LEFT$(stream$, INSTR(stream$, "<END>") - 1)
  30.     stream$ = MID$(stream$, INSTR(stream$, "<END>") + 5)
  31.     thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  32.     IF thisCommand$ = "" THEN
  33.         thisCommand$ = thisData$
  34.     ELSE
  35.         thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  36.     END IF
  37.  
  38.     IF thisCommand$ = "MSG" AND thisData$ = "HELLO!" THEN
  39.         'we're in! let's send our dimensions
  40.         b$ = "HELLO>" + MKI$(myWidth%) + MKI$(myHeight%)
  41.         Send host, b$
  42.     ELSE
  43.         GOTO failed
  44.     END IF
  45.     failed:
  46.     SYSTEM
  47.  
  48. 'connection established; wait for input and send the client's screen
  49. ping = TIMER
  50.     IF _EXIT THEN
  51.         b$ = "BYE!>"
  52.         Send host, b$
  53.         CLOSE host
  54.         SYSTEM
  55.     END IF
  56.  
  57.     GET host, , incomingData$
  58.     stream$ = stream$ + incomingData$
  59.  
  60.     DO WHILE INSTR(stream$, "<END>")
  61.         thisData$ = LEFT$(stream$, INSTR(stream$, "<END>") - 1)
  62.         stream$ = MID$(stream$, INSTR(stream$, "<END>") + 5)
  63.         thisCommand$ = LEFT$(thisData$, INSTR(thisData$, ">") - 1)
  64.         IF thisCommand$ = "" THEN
  65.             thisCommand$ = thisData$
  66.         ELSE
  67.             thisData$ = MID$(thisData$, LEN(thisCommand$) + 2)
  68.         END IF
  69.  
  70.         SELECT CASE thisCommand$
  71.             CASE "BYE!"
  72.                 SYSTEM
  73.             CASE "CLICK"
  74.                 FOR i = 1 TO 30
  75.                     CIRCLE (CVI(LEFT$(thisData$, 2)), CVI(RIGHT$(thisData$, 2))), i, _RGB32(255, 255, 255)
  76.                 NEXT
  77.             CASE "PING"
  78.                 ping = TIMER
  79.                 b$ = "PONG>"
  80.                 Send host, b$
  81.         END SELECT
  82.     LOOP
  83.  
  84.     LINE (0, 0)-(_WIDTH, _HEIGHT), myColor~&, BF
  85.     FOR i = 1 TO 1000
  86.         PSET (RND * _WIDTH, RND * _HEIGHT), _RGB32(RND * 255, RND * 255, RND * 255)
  87.     NEXT
  88.     LOCATE 2, 2: PRINT RND
  89.  
  90.     myCanvas& = _COPYIMAGE(0)
  91.     DIM imgMem AS _MEM
  92.     imgMem = _MEMIMAGE(myCanvas&)
  93.     b$ = SPACE$(imgMem.SIZE)
  94.     _MEMGET imgMem, imgMem.OFFSET, b$
  95.     _MEMFREE imgMem
  96.     _FREEIMAGE myCanvas&
  97.  
  98.     IF prevImage$ <> b$ THEN
  99.         prevImage$ = b$
  100.         b$ = "IMAGE>" + b$
  101.         Send host, b$
  102.     END IF
  103.  
  104.     _DISPLAY
  105.     _LIMIT 30
  106. LOOP UNTIL TIMER - ping > 30
  107.  
  108. SUB Send (channel, __theData$)
  109.     theData$ = __theData$ + "<END>"
  110.     PUT #channel, , theData$
  111.  

You must also download the background image as an attachment or reconfigure to use another or no image.

Thank you,
-Keybone

« Last Edit: August 06, 2018, 02:24:54 pm by keybone »
I am from a Kazakhstan, we follow the hawk.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: TCP/IP client/host error (Advanced question, need help)
« Reply #1 on: August 06, 2018, 02:54:42 pm »
Hi. In the real network it occasionally reports this error, but in localhost it works correctly? I'm very curious what FellippeHeitor will answer you. I would recommend testing if client (i) .w, client (i) .h and thisData$ contains the expected data, I would bet not. I also had a lot of problems working on the real network and I after many tryings without correct output in real network stop working on it. In localhost mode, they were always working without problems, but the behavior in the real network was totally different, and the program sometimes sent me data that has never coming to second site....

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: TCP/IP client/host error (Advanced question, need help)
« Reply #2 on: August 06, 2018, 03:16:39 pm »
Hi. In the real network it occasionally reports this error, but in localhost it works correctly? I'm very curious what FellippeHeitor will answer you. I would recommend testing if client (i) .w, client (i) .h and thisData$ contains the expected data, I would bet not. I also had a lot of problems working on the real network and I after many tryings without correct output in real network stop working on it. In localhost mode, they were always working without problems, but the behavior in the real network was totally different, and the program sometimes sent me data that has never coming to second site....

Well, I'm glad to know i'm not the only one having issues :D

On the real network it *never* works... The window pops up on the host screen (like it should) then the message pops up and the host crashes. Well for now localhost connectivity is the main priority anyways... the LAN stuff can wait til later. :)
« Last Edit: August 06, 2018, 03:23:34 pm by keybone »
I am from a Kazakhstan, we follow the hawk.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: TCP/IP client/host error (Advanced question, need help)
« Reply #3 on: August 06, 2018, 03:23:23 pm »
Next ask is, if b$ lenght (image data lenght) is the same on both sides?

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: TCP/IP client/host error (Advanced question, need help)
« Reply #4 on: August 06, 2018, 03:45:54 pm »
Next ask is, if b$ lenght (image data lenght) is the same on both sides?

I'll let you know the status of those tests later... thanks man!
I am from a Kazakhstan, we follow the hawk.

FellippeHeitor

  • Guest
Re: TCP/IP client/host error (Advanced question, need help)
« Reply #5 on: August 06, 2018, 05:06:33 pm »
RhoSigma recently pushed a patch into the repository which is already available in the latest development build that addresses a faulty behavior of _MEMIMAGE and related functions. Try the latest dev build from www.qb64.org and see if you still get those.