CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2004
    Posts
    5

    CSocket data lost

    I've been on the project that using CSocket to send file from client to server. When i test the program, only part of data send through CSocket(about one third of the original data). I use CSocket as a SOCK_STREAM. Can anyone figure out the problem on my code?
    thnx

  2. #2
    Join Date
    Aug 2004
    Posts
    184
    I did a quick google search on csocket data loss.......

    and hit this page that offers a possible explanation and a solution in vb.
    http://www.vbip.com/forum/topic.asp?id=4030

    now I know this is a vb forum link I am posting, but it seemed that it may offer some insight to your problem.

    HTH

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [ Moved thread ]

  4. #4
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    I don't know about CSockets, ISTR reading somewhere that the send data length should be less than 8K. You can try and see it for yourself.

    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  5. #5
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    Well one cannot recieve whole data from one Receive Call , so one will have to call Receive multiple times to be sure that all data is successfully fetched.

    But as CSocket is a Blocking Socket so one should know the incoming data size prior calling receive so that one could terminte the loop at right time.
    Unmanaged in a .NET world

  6. #6
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    Quote Originally Posted by atif_ilm
    But as CSocket is a Blocking Socket...
    Technically, CSocket uses a non-blocking socket, but only fakes blocking.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  7. #7
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    Quote Originally Posted by Mathew Joy
    Technically, CSocket uses a non-blocking socket, but only fakes blocking.

    yehh yehh whatever but atleast it pretends to be a Blocking Socket.
    Unmanaged in a .NET world

  8. #8
    Join Date
    Apr 2004
    Posts
    5
    i divide the file into small chunks of 8kb.
    but when i sent the chunks length prior to sending the chunks the program crashed.
    I thought the cause is on the OnAccept() notification which is on the receiver side,after call CSocket::Receive(filelength...), i call CSocket::Send(notify) to notify the sender to begin send the file. Are we not suppose to call receive and send on the same function.
    Could anyone suggest some idea how to send filelength be4 send actual file.
    thnx

  9. #9
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672
    you have to develop your own protocole for sending file into small chunks eg

    Code:
    when socket is accepted then 
    
    Receiver                                                Sender
    
    // Send Ready message for 
    // Start receiving file data
    1  ReceiverSocket.Send(Ready)   
                                                        1  SenderSocket.OnReceive()
                                                            { // when receive ready notification then
                                                               // send total file length
                                                                  SenderSocket.Send(FileLength)
                                                            }
    ReceiverSocket.OnReceive()
    {
         //  When receive total file length
         //  then send request for chunk length
         // and store total file length in variable
         2  ReceiverSocket.Send(SendChunkLength)
    }
    
                                                        2  SenderSocket.OnReceive()
                                                            {  // when receive request for chunk
                                                                // length then send  chunk length 
                                                                // eg 8kb
                                                                 SenderSocket.Send(Length8kb)
                                                            }
    ReceiverSocket.OnReceive()
    {
    //   When receive chunk length
    //   divide fileLength into number of chunks
    //   just like FileLength/chunkLength and store
    //   in a variable and send request to send 
    //   first chunk
        3  ReceiverSocket.Send(SendFirstChunk)
    }
                                                          3 SenderSocket.OnReceive()
                                                            {// when receive request of 
                                                              // chunk with chunk number then
                                                              // send that chunk with its number
                                                                 SenderSocket.Send(1stChunk+Data)
                                                            }
    ReceiverSocket.OnReceive()
    {
    //  First chunk received write it into file
    //  Send request for next chunk
      4   ReceiverSocket.Send(SendSecondChunk)
    }
                                                         4  SenderSocket.OnReceive()
                                                            {
                                                                 SenderSocket.Send(2ndChunk+Data)
                                                            }   
    just like that you can receive all chunks
    hope this will help u .
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  10. #10
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211
    Take a look at File Transfer Using CSockets.

    Also, don't forget to read this
    Last edited by Ejaz; August 17th, 2004 at 12:10 AM.

  11. #11
    Join Date
    Apr 2004
    Posts
    5
    the following code is from the link that someone earlier in this thread

    //////////////////////////////////////////////////////////
    ////////this code is used to send large binary file
    Code:
    do	
    {
       dwRead = myFile.Read(data, 4096);
       sockRecv.Send(data, dwRead);   	
    }	
    while (dwRead > 0);

    /////////////////////////////////////////////
    //////////and this code for receive that file////
    Code:
    byte* data = new byte[4096];
    do
    {		  
    dwRead = sockClient.Receive(data, 4096);
    destFile.Write(data, dwRead); // Write it
    } 
    while (dwRead > 0);
    //////////////////////////////////////////
    THE PROBLEM IS: we are not sure about the order of each sub data. We may in the situation that connection is slow, therefore the previous data not yet write to the file, but coming new data due to the while loop.

  12. #12
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

  13. #13
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    I think you should maintain , two connection , Control Connecton and Data connection.

    Control connection is to send the controls signals such as Incoming File Size , Error Messages and Data connection for transfer of original bytes.


    but keep in mind the blocking nature of CSocket for example

    if incoming File Size was 502 bytes
    and u do things like this

    Receive(buffer,1024);//CSocket would block here unless it gets complete 1024 data , which in this case will not be there.


    also it is not necessary that if you call "Receive(buffer,1024); " you will get the whole data at once , may be you get 500 bytes of total 1024 so u will also have to keep track of actual received bytes (return value from Receive).


    so receive all data in loop and as actual read bytes =File Size break that loop.

    another very famouse problem with CSocket is its Fake FD_READ notification while receiving the data in OnReceive event handler , so its better at the start of OnReceive disable FD_READ using AsyncSelect and enable it efter receiving whole data.


    I hope i addressed some important issues regarding CSocket .
    If someone could add to this information , he is welcome.
    Unmanaged in a .NET world

  14. #14
    Join Date
    Sep 2004
    Posts
    2

    Re: CSocket data lost

    For my server/client that i wrote in CSocket i could only send 1000 bytes at a time...I just kept sending 1000 bytes at a time until i reached the end of the file. I also told my program to Sleep(1000); after sending each byte in order to prevent problems. So far, it has been working flawlessly.

  15. #15
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354

    Re: CSocket data lost

    Quote Originally Posted by Drummer16161616
    For my server/client that i wrote in CSocket i could only send 1000 bytes at a time...I just kept sending 1000 bytes at a time until i reached the end of the file. I also told my program to Sleep(1000); after sending each byte in order to prevent problems. So far, it has been working flawlessly.
    Well, that doesn't sound good. You are sending less than 1MB at a time and you sleep 1 sec at times. Your usage of winsock has been restricted for the sake of just using, not for its usefullness. You get far better results if you can use just the basic blocking socket using winsock APIs. And programming is easier that using CSocket.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured