Announcement

Collapse
No announcement yet.

TCP RECV

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP RECV

    In code below:
    Code:
    DO
        'SLEEP 1
        TCP RECV hTCP, 1024, sBuffer
        Buffer=Buffer+sBuffer
        'SLEEP 1
    LOOP UNTIL sBuffer="" OR ISTRUE EOF(hTCP) OR ISTRUE ERR
    On a very fast machine, the loop exit by sBuffer="", but
    one byte more is incoming. Is it possible ?

    A false EOF, before the true segment ends : Is it possible ?

  • #2
    As I understand it, if EOF() tests true after TCP RECV, what's happened is that there was not enough data in the TCP buffer to fulfill the request. In your example, this is 1024 bytes. EOF does NOT mean that more data won't be coming in to the TCP buffer. So on a very fast machine, or on a connection with lots of latency, a false EOF is indeed possible.

    ------------------
    "People sleep peacefully at night because rough men stand ready to do violence on their behalf" -- George Orwell
    Real programmers use a magnetized needle and a steady hand

    Comment


    • #3
      So, may be this is a way:

      Code:
      DO    
        TCP RECV hTCP, 1024, sBuffer    
        Buffer=Buffer+sBuffer    
        IF sBuffer="" OR ISTRUE EOF(hTCP) THEN
           SLEEP 5
           TCP RECV hTCP, 1024, sBuffer
           Buffer=Buffer+sBuffer
        END IF
      LOOP UNTIL sBuffer="" OR ISTRUE EOF(hTCP) OR ISTRUE ERR
      What do you think about it ?
      Thanks.

      ------------------

      Comment


      • #4
        Perhaps a little better. I too had this annoying issue and it caused
        me to give up on adding more advanced stuff to the Web Server app I
        made. I did buy a TCP pack offered here at a very good price sometime
        back though, so I may recode it someday to use it. Another problem
        is when IE is used to download a file it sends the request before the user
        is finished selecting a location, so it can be canceled...why in the world
        didn't they just request the header then the file when the location
        is selected?

        Main issue even with modified code is going to be timeouts. The wait with
        something like your added SLEEP, plus the wait for the connection when
        no data. And never knowing if you need another SLEEP or not. Data size
        is never a sure thing...unless you make up your own data structure, but then
        you can only use it in your own apps or for a file transfer, etc...not internet
        type stuff as they all use unknowns.

        ------------------
        If you aim at nothing...you will hit it.

        [This message has been edited by Roger Garstang (edited September 04, 2006).]
        sigpic
        Mobile Solutions
        Sys Analyst and Development

        Comment


        • #5
          Having same problem.
          Can't get read more than 1460 bytes with reliability.
          TCP SEND nSocket, STRING$(%PacketLength,"X") 'send packet
          'SLEEP IS REQURED HERE
          'SLEEP 104 'SLEEP 104 required after TCP SEND if PacketLength = 8192 (1 thread)
          'SLEEP 160 'SLEEP 160 required after TCP SEND if PacketLength = 8192 (2 threads)
          'SLEEP 180 'SLEEP 200 required after TCP SEND if PacketLength = 8192 (3 threads)
          'SLEEP 210 'SLEEP 210 required after TCP SEND if PacketLength = 8192 (4 threads)
          'SLEEP 260 'SLEEP 260 required after TCP SEND if PacketLength = 8192 (5 threads)
          'SLEEP 310 'SLEEP 31 required after TCP SEND if PacketLength = 8192 (6 threads)


          Code:
          DO
                 TCP RECV nSocket, %PacketLength, sBuffer
                 sPacket = sPacket + sBuffer
                 IF sBuffer="" OR ISTRUE EOF(nSocket) THEN
                     'falls through with sBuffer = 1460
                     '? "The length of sBuffer was" + STR$(Len(sBuffer))
                     SLEEP 5
                     TCP RECV nSocket, %PacketLength, sBuffer  'goes into a wait until timeout
                     ? "The length of sBuffer on second recv" + STR$(LEN(sbuffer))
                     sPacket = sPacket+ sBuffer
                 END IF
               LOOP UNTIL sBuffer="" OR ISTRUE EOF(nSocket) OR ISTRUE ERR
               ? "This doesn't execute until timeout occurs"
          The world is full of apathy, but who cares?

          Comment


          • #6
            Solution by Don Dickinson in Rserver2

            Code:
            FUNCTION tcpSafeReceive(BYVAL hSocket AS LONG, BYVAL iBufferLen AS LONG, _
                                    recBuff AS STRING) AS LONG
             
               'tcpSafeReceive is by Don Dickinson  - rated A+  (required)
             
               DIM iLeft AS LONG
               DIM sBuffer AS STRING
               recBuff = ""
               iLeft = iBufferLen
               DO
                  sBuffer = SPACE$(iLeft)
                  ON ERROR RESUME NEXT
                  sBuffer = SPACE$(iBufferLen)
                  TCP RECV hSocket, iLeft, sBuffer
                  IF ERR THEN
                     FUNCTION = %False
                     EXIT FUNCTION
                  END IF
                  recBuff = recBuff + sBuffer
                  IF LEN(recBuff) >= iBufferLen THEN
                     EXIT DO
                  END IF
                  iLeft = iBufferLen - LEN(recBuff)
                  SLEEP 5
               LOOP
               FUNCTION = %True
            END FUNCTION
            The world is full of apathy, but who cares?

            Comment

            Working...
            X