+ Reply to Thread
Results 1 to 2 of 2

Thread: How to receive data in larger chunks using TCP?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    26

    How to receive data in larger chunks using TCP?

    I have following code:

    Code:
    Void ReceiveData(SOCKET hSocket)
    {
          char data[10000] ;
          int nLength = 10000 ;
          int nDataReceived = recv(hSocket, data, nLength, 0) ;
    
          //nDataReceived is not the same that I sent from the other end of the socket.
          // how should I resolve this issue? 
    }
    I do not receive the exact number of bytes using recv call.

    How to resolve this issue?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    38
    1. You can use one technique like below:

    Code:
    void ReceiveData(SOCKET hSocket)
    {
          char data[1024] ;
          int nLength = 1024 ;
          int nDataReceived = 0 ;
          do
          {
              nDataReceived = recv(hSocket, data, nLength, 0) ;
          } while(nDataReceived > 0) ;
    }
    This technique receives 1K bytes in chunk till there is no more data.

    2. Another technique is that you send number of bytes to be received by receiver end and then send the actual data.

    Receiver will always receive number of bytes to be received and then receives the actual data.

    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts