locked
Error in Binary File Writing from client using socket in VB.NET RRS feed

  • Question

  • Hi I have developed a client application in c++ whose function is to tracsfer data to the Server(Developed in Vb.net).I first tested the client application with another application developed in C++ and file transmitting and receving seemed to have worked well. Now the problem arrises when i transfer a file from my C++ (client) to my vb.net server. A Small file is transferred ok. but in case files care than 100 kb errors occur.I suspect the error is taking place because of my lack of understanding of binary sequential files . The code which I am using is as follows for receiving:

    Private Sub DoListen()    'Independant thread rnnung in infinite loop

    While ( True )

    Try

    client = Server.AcceptTcpClient

    'Blocking Function (Thread Hangs Here Until True)

    Console.WriteLine("Connected" & client.Client.RemoteEndPoint.ToString)

    'Since A Connection Has Been established Start Infinite Loop For Incoming Data

    While ( True )

    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

          If Microsoft.VisualBasic.Left(IncomeData, 4) = "Make" Then

                IncomeData = Replace(IncomeData,"Make" , "" )

             IncomeData ="c:\" & IncomeData & ".exe"

    fsStream =  New System.IO.FileStream(Incomedata , IO.FileMode.Create, IO.FileAcess.Write,IO.FileShare.ReadWrite)  'Create And Open A File For Writing

    bwWriter = New System.IO.BinaryWriter(fsStream)

    ElseIf Microsoft.VisualBasic.Right(IncomeData, 5) = "*End*" Then

    IncomeData = Replace(IncomeData,"*End*" , "" )

    bwWriter.Write(IncomeData, 0, Receiveddata)

    bwWriter.Close()

    IncomeData =""

    ElseIf Microsoft.VisualBasic.Left(IncomeData, 5) = "*End*" Then

    bwWriter.Close()

    IncomeData =""

    Else

    bwWriter.Write(IncomeData, 0, Receiveddata)   '-------------->>>>Isuspect The problem is here in vb6 we had put#1, ,incomedata

    IncomeData =""

    End If

    End If

    End If

    End While

    Catch e As Exception

    MsgBox("Error " & (e.ToString()))

    End Try

    End While

    End Sub

    i would appreciate any advice or help in this matter...Thanks again


    A candle loses nothing by lighting another candle.
    Wednesday, August 26, 2009 2:16 PM