Author Topic: GET (TCP/IP command) - how it works?  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
GET (TCP/IP command) - how it works?
« on: March 29, 2019, 05:16:32 am »
Hello programmers,

I am trying to get a little into the network things, and there is something i don't get about the GET network command, maybe someone experienced could explain it.
Here's the situation in fragment of code:

Code: QB64: [Select]
  1. TYPE someType
  2.     x AS INTEGER
  3.     y AS INTEGER
  4.     s AS STRING * 10
  5.     '...
  6.  
  7. DIM someVar(1 TO 999) AS someType
  8. DIM someOtherVar AS someOtherType 'any type, doesnt matter
  9.  
  10. clientHandle& = _OPENCLIENT("TCP/IP:1234:localhost")
  11.  
  12.     GET clientHandle#, , someVar()
  13.     '...other code that deals with somevar()
  14.     IF NOT EOF(clientHandle) THEN GET clientHandle#, , someOtherVar
  15. LOOP UNTIL EOF(clientHandle)
  16.  
  17. '...and another code that deals with somevar() and someOtherVar
  18.  
  19.  

My questions are:
1. after GET reads someVar() from client, how can I tell, if it has actually read it? I understand, that GET reads someVar only if there are enough bytes in clientHandle. But what tells me it did? GET does not return anything like status, from what I know.

2. If GET doesn't put anything into someVar (e.g. there is nothing or few to read), will the original content of someVar() be destroyed or preserved?

Thanks.



Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: GET (TCP/IP command) - how it works?
« Reply #1 on: March 29, 2019, 03:55:37 pm »
Hi. I didn't celebrate success with networking ... but ....
If one party sends information about the start of the transmission, it is good to wait for the set time (set yoursefl by your program) to confirm. Then the sending party sends the amount of data it wants to transfer. He waits for confirmation from the counterparty and then begins to send data and waits for a specified amount of time to confirm receipt from the counterparty. Preferably, the counterparty sends the size of the received data. If it does not send anything within the set limit, you must resolve it in the program so that both parties resume the new transmission (transfer reset). You'll never get the data you won't catch. These are my experiences gained when writing a network version of BattleShip game, that worked on the network .... 3x correctly and then stuck, just because I waited for the client to receive data in infinite time...  (my version not check the time)  And be careful. What works in localhost mode perfectly, may not work in the actual network.

Marked as best answer by dajan on April 30, 2019, 09:28:57 am

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: GET (TCP/IP command) - how it works?
« Reply #2 on: March 29, 2019, 04:41:46 pm »
Here I found one old program to transfer the image over the network. Again - it does not watch the time, so it will be unreliable in the real network.

Code: QB64: [Select]
  1.  
  2. DIM SHARED Host AS LONG, Client AS LONG
  3. DIM Buffer AS _MEM, Offset AS _OFFSET, lastOffset AS _OFFSET
  4.  
  5. I_am = Network("localhost")
  6. image& = _SCREENIMAGE '_LOADIMAGE("so.jpg", 32)
  7. 'Image& = _LOADIMAGE("3.jpg", 32)
  8.  
  9.     CASE 1 'HOST   -    in this demo HOST send image to client
  10.         _TITLE "Host"
  11.  
  12.         Res$ = MKL$(_WIDTH(image&)) + MKL$(_HEIGHT(image&)) + MKL$(_PIXELSIZE(image&))
  13.         PUT #Host, , Res$
  14.  
  15.  
  16.         Buffer = _MEMIMAGE(image&)
  17.         Offset = Buffer.OFFSET
  18.         lastOffset = Buffer.OFFSET + _WIDTH(image&) * _HEIGHT(image&) * 4
  19.         imageData$ = SPACE$(lastOffset - Offset)
  20.         _MEMGET Buffer, Offset, imageData$
  21.         _MEMFREE Buffer
  22.         LOCATE 1, 1: PRINT "Sending picture data:"
  23.  
  24.         PUT #Host, , imageData$
  25.         _DELAY .05
  26.         PRINT "Waiting for confirm": _DISPLAY
  27.         message$ = Transfer$(Host&, 6)
  28.  
  29.         DO
  30.             IF message$ = "ALL OK" THEN LOCATE 2, 1: PRINT "Image transferred   ": _DISPLAY: END
  31.             _DISPLAY
  32.             _LIMIT 25
  33.         LOOP
  34.  
  35.  
  36.     CASE 2 'CLIENT - in this demo receive picture and show it on the screen
  37.         _TITLE "Client"
  38.         Head$ = Transfer$(Client&, 12)
  39.         W& = CVL(MID$(Head$, 1, 4))
  40.         H& = CVL(MID$(Head$, 5, 4))
  41.         D& = CVL(MID$(Head$, 9, 4))
  42.         SELECT CASE D&
  43.             CASE 0: PRINT "Textmode not supported": END
  44.             CASE 1: clrs = 256
  45.             CASE 4: clrs = 32
  46.         END SELECT
  47.  
  48.         imageData$ = Transfer$(Client&, (W& * H& * D&))
  49.         receivedImage& = _NEWIMAGE(W&, H&, clrs) 'use the width and height you transferred before here
  50.         Buffer = _MEMIMAGE(receivedImage&)
  51.         _MEMPUT Buffer, Buffer.OFFSET, imageData$
  52.         _MEMFREE Buffer
  53.         SCREEN receivedImage&
  54.         confirm$ = "ALL OK" '6 bytes
  55.         PUT #Client&, , confirm$
  56.         END
  57.  
  58.  
  59. FUNCTION Transfer$ (channel AS LONG, lenght)
  60.     Transfer$ = ""
  61.     DO
  62.         GET #channel&, , T$
  63.         IF LEN(T$) THEN Transfer$ = Transfer$ + T$
  64.         '  IF _CONNECTED(channel&) = 0 THEN PRINT "Connection failure.": END
  65.     LOOP WHILE LEN(Transfer$) < lenght
  66.  
  67. FUNCTION Network (IP AS STRING)
  68.     Client& = _OPENCLIENT("TCP/IP:3455:" + LTRIM$(IP$))
  69.     IF Client& THEN
  70.         Network = 2 'client
  71.     ELSE
  72.         PRINT "No host found"
  73.         _DELAY 1
  74.         Client& = _OPENHOST("TCP/IP:3455")
  75.         IF Client& THEN
  76.             PRINT "Host created!"
  77.             DO
  78.                 i& = _KEYHIT
  79.                 IF i& = 27 THEN EXIT FUNCTION
  80.                 Host& = _OPENCONNECTION(Client&)
  81.                 _DISPLAY
  82.             LOOP UNTIL Host&
  83.             Network = 1
  84.         END IF
  85.     END IF
  86.  
  87.  


I forgot .... you have to run it twice to try this program. The first run creates a HOST, lets you run it and starts it again, the second run creates a client and sends a screenshot between them, the client will also show it for you, when the transfer is complete.
« Last Edit: March 29, 2019, 04:56:50 pm by Petr »