Announcement

Collapse
No announcement yet.

Sending & Receiving Data With TCP

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

  • Sending & Receiving Data With TCP

    I'm using TCP PRINT followed by TCP RECV to communicate between 2 programs.

    If the server is generating large amounts of data, how can I send it as it's generated rather than build a string and the send it with TCP SEND

    I have tried using TCP PRINT but only the first PRINT of data is received by the client program

    Code:
    TCP OPEN PORT 999 AT "127.0.0.1" AS 1 TIMEOUT 60000
        '
        TCP PRINT 1, "Send Me Your Data Please";
        '
        DO WHILE NOT EOF( 1 )
            '
            TCP RECV 1, 512, TS1
            '
            Result + = TS1
            '
        LOOP
        '
        TCP CLOSE 1

  • #2
    About the only way I can think of is to buffer the server data and use multiple SENDs with some sort of EOF indicator at the end of all the data. The receiving end keeps listening and concatenates all received data until that indicator is seen.

    Comment


    • #3
      Steve
      You might want to play with this some.
      https://forum.powerbasic.com/forum/u...gen-web-server
      p purvis

      Comment


      • #4
        Have you tried UDP?

        Code:
        FUNCTION QueryUDPServer(strQuery AS STRING) AS STRING
        '
          LOCAL ip     AS LONG      ' This machines IP address
          LOCAL bip    AS LONG      ' Broadcase IP address for this segment (class D)
          LOCAL hUdp   AS LONG      ' UDP file number
          LOCAL Buffer AS STRING    ' UDP data received
          LOCAL ipAddr AS LONG      ' IP address of sending machine
          LOCAL ipPort AS LONG      ' UDP Port of sending machine to reply to
          LOCAL t      AS SINGLE    ' Timer for reply monitoring
          LOCAL x      AS LONG      ' Counter
          LOCAL Op     AS STRING    ' Status text
          '
          ' get the IP address
          HOST ADDR TO ip
          '
          HOST ADDR g_strUDPServer TO bip
          '
          ' open channel
          hUdp = FREEFILE
          '
          UDP OPEN AS #hUdp TIMEOUT 5000
          IF ERR THEN
            EXIT FUNCTION
          END IF
          '
          Buffer = g_strUDPServer & "|" & strQuery
          CON.STDOUT "Sending to " & DottedIP(bip) & " -> " & Buffer
          '
          DO
            UDP SEND hUdp, AT bip, %UPort, Buffer
            ' We'll monitor replies for 5 seconds, then do another broadcast
            t = TIMER
            WHILE ABS(TIMER - t) < 5
              '
              ERRCLEAR
              UDP RECV #hUdp, FROM ipAddr, ipPort, Buffer
              '
              ' Ignore any timout or other errors
              IF ERR THEN ITERATE
              '
              ' We got one!  return the data (remote date)
              FUNCTION = Buffer
              CLOSE #hUdp
              EXIT FUNCTION
            WEND
          '
          INCR x
          LOOP WHILE x < 1
          '
          CLOSE #hUdp
          '
        END FUNCTION
        https://www.gsfsoftware.co.uk/PBTutorials/Projects.htm

        https://www.youtube.com/channel/UCpP...OhN1_B30pvlPdA

        Comment


        • #5
          Steve,
          I use a send thread and a receive thread with separate FIFO buffers and 2 different ports. The Server sends to the Client receive port and vice versa.

          When a message is received it is added to the FIFO buffer with a special character sequence "[+]" preceding the new message.

          If there is a large amount of data to send then I segment it into several smaller messages. Of course each message has a header with information about the Sender and what type of message it is. Identifying the type of message is very useful when deciding what to do with the message. How it should be processed. "Weather1" "Weather2" "Weather3" etc.

          The FIFO buffers are processed/interpreted in other threads that are synchronized with their parent thread (SEND or RECV). In others words a FIFO buffer is not processed when data is being added to that buffer. Very efficient and very fast.
          Last edited by Jim Fritts; 6 Jan 2020, 01:01 PM.

          Comment


          • #6
            Steve,
            Sorry, I posted code that sends everything in 1-send.
            Last edited by Mike Doty; 14 Jan 2020, 02:31 AM.
            https://duckduckgo.com instead of google

            Comment

            Working...
            X