none
Why Does My File Transfer remain incomplete??

    Question


  • Hi , I am currently having a problem during file receiving . The code for the receiver which I am using is given below.

    char mybuff[1024];

    FILE *fp1 = fopen("c:\\copy.txt", "wb");

    myno = recv(client,mybuff,sizeof(mybuff),0);

                      if (myno > 0 && myno !=-1)
                       {
                            while (myno != -1 && myno > 0)
                             {
                                  Written = fwrite(mybuff, 1,myno, fp1); ///////Line 1
                                  myno = myno - Written ;
                             }
                             myno = 0;
                       }

    The Problem is the tranfer process remain incomplete (The final part does not get written in the file).The transmitters code is working and I know the receiver receives the data (by using - cout << Mybuff ). Then why does the complete file writing process does not work (remains incomplete) most of the times ?? . I tried to handle the problem by Introducing Sleep(800)  after line 1 which seems to have temporaryly solved the problem. But still I need to know why it isnt working. Furthermore I am also attaching the transmitter code along with this. I am using Borland C++ 5.02

    Transmitter Code:
      FILE *fp = fopen("c:\\Send.txt", "rb");
                                   while(!feof(fp))
                                     {
                                        bytes_read = fread(&buff, sizeof(char), CHUNK , fp) ;
                                           {
                                               //SEND FILE
                                               Bytes_Sent=send(myclient, buff, bytes_read, 0);
                                           }

                                     }
                                     fclose (fp);

    Could You guys please please try to help me out ..  I really need to solve this problem.This is the first post i ever made in a forum.....................................................................

    Sunday, August 23, 2009 10:29 AM

All replies

  • What is breaking the recv loop? Can I see your recv loop?
    While (?)
    {
       myno = recv(client,mybuff,sizeof(mybuff),0);
    if (myno > 0 && myno !=-1) { while (myno != -1 && myno > 0) { Written = fwrite(mybuff, 1,myno, fp1); ///////Line 1 myno = myno - Written ; } myno = 0; } }
    Wednesday, August 26, 2009 11:11 PM
  • Actually the receiver has been developed in vb.net using TCPCLient Libraries

    Private Sub GetData() 'Separate continuous non terminating thread
            While (True)
                Try
                    ClientStream = client.GetStream()
                    If ClientStream.CanWrite And ClientStream.CanRead Then
                        Dim bytes(client.ReceiveBufferSize) As Byte
                        Receiveddata = ClientStream.Read(bytes, 0, CInt(client.ReceiveBufferSize)) 'Blocking Function
                        Dim IncomeData As String = Encoding.ASCII.GetString(bytes) 'bytes
                        If Receiveddata <> 0 And Receiveddata <> -1 Then
                            'MsgBox(IncomeData)
                            If Microsoft.VisualBasic.Left(IncomeData, 6) = "#Make#" Then
                                Dim SplitArray() As String
                                'First Delivery --> #Make#IP-ADDRESS#ThreadNo#FileName(Ext)
                                IncomeData = Replace(IncomeData, "#Make#", "")
                                SplitArray = Split(IncomeData, "#")
                                fsStream = New System.IO.FileStream("C:\copy.flv", FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)
                                bwWriter = New System.IO.BinaryWriter(fsStream)
                                bwWriter.Flush()


                            ElseIf Microsoft.VisualBasic.Right(IncomeData, 5) = "*End*" Then
                                IncomeData = Replace(IncomeData, "*End*", "")
                                bwWriter.Write(IncomeData, 1, Receiveddata)
                                bwWriter.Close()
                                IncomeData = ""
                            ElseIf Microsoft.VisualBasic.Left(IncomeData, 5) = "*End*" Then
                                bwWriter.Close()
                                IncomeData = ""
                            Else
                                bwWriter.Write(IncomeData, 1, Receiveddata)
                                bwWriter.Flush()
                                IncomeData = ""
                            End If

                        End If
                    End If
                Catch e As Exception
                    ' MsgBox("Error " & (e.ToString()))
                End Try
            End While
        End Sub

    A candle loses nothing by lighting another candle.
    Thursday, August 27, 2009 4:28 AM