Android Question Astreams TCP connection looses data

Discussion in 'Android Questions' started by Samplatiner, Jul 9, 2015.

  1. Samplatiner

    Samplatiner Member Licensed User

    Hello all there,

    I am writing an app, in which a small feature should be to take Pictures and send them to a PC.

    This Little Feature does not work correctly at a point, which i don't understand why it does not work.

    Opening the Connection

    Dim socket1 As Socket
    Dim AStreams As AsyncStreams
    'In some Sub
    Well, I make the photo and get the correct data from it. I send the length of the data to the Computer, which receives it correctly.

    Sub Camera1_PictureTaken(Data() As Byte)
      dataFile=DataDim out 
    As OutputStream
    File.OpenOutput(File.DirDefaultExternal, "42.jpg"False)
    0, Data.Length)
    "Image saved: " & File.Combine(File.DirDefaultExternal, "42.jpg"), True)Dim datalength(4As Byte
    0, datalength.Length)
      i = 
    End Sub
    Afterwards, i send packages of data to the Computer.

    Sub sendnextData()
    Do While j < 2000
    0, sendData.Length)
    End Sub
    After each package the PC receives, it sends an acknowledge, so the next package will be sent.
    Sub AStreams_NewData(Buffer() As Byte)
    If i < dataFile.Length Then
    End If
    End If
    End Sub
    I know that this is still no perfect solution, because the Chance is very low that the file is exactly x*1000 Bytes, but that shall not matter for the moment.

    2 times it works fine. But if the tablet sends the third package, the Computer just receives round about 1990 of the 2000 Bytes. The absolut amount of Bytes is not constant.
    On the PC there is a Server written in VB, which reads Byte for Byte. Something like this
    while Counter< datasize
    while j< 2000
    'save the data to file
    end while
    end while
    If the PC gets only 1990 Bytes, the while Loop does not end and the Programm gets stuck because it waits for data.
    Well, I think this simply is not allowed to happen, because TCP guarantees a complete Transmission.
    If I reduce the size of the packages to 5 Bytes, I can send round about 27 packages, but then I get the same Problem.

    Neither B4A nor VisualStudio throws an exception, error or something like that.

    I tried it with "Write" instead of "Write2", same Problem.

    Could you give me a hint, where I have to search the Problem?
    Last edited: Jul 9, 2015
  2. sorex

    sorex Expert Licensed User

    shouldn't you add 2000 to counter after each inner loop ending?

    if will never reach the datasize value now.
  3. Samplatiner

    Samplatiner Member Licensed User

    dthe while Loops are working fine.

    If the Problem occurs and i send manually for example by pressing an button a few times something like this

    Sub Button1_Click()
    Dim Buffer(1as Byte
    End Sub
    the Loop works as it has to.

    After a few transmissions, the Problem occurs once again. There are packages of Bytes missing

    i could not paste this part of the code because ist on another pc. I had to write it by hand

    i edited it.
    Last edited: Jul 9, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
  5. Samplatiner

    Samplatiner Member Licensed User

    thx, i will read through them
  6. raphaelcno

    raphaelcno Active Member Licensed User

    When you receive 1990 of 2000 Bytes, do you miss the last 10 Bytes or 10 arbitrary Bytes?
    What is the value of "i" when the transmission stops, 1989 or 1999?
  7. Samplatiner

    Samplatiner Member Licensed User

    Thanks a lot for your help.

    I'm still not sure why my last code does not work, but i could find another way.

    The Problem was at the
    Because of a reason i don't know, the while Loop counting the received Bytes does not work correctly.

    I changed the code to this (This code is written by hand, not copy pasted):

    while true
    'Receive datalength of a package, saved in 2 bytes
      dim datalength as integer = streamr.Read
      datalength = datalength * 
    256 + streamr.Read
    dim data(datalength) as char
    'if i know how much Byte a package has, i can read exactly this amount of bytes
      streamrServer.Read(data, 0, datalength)
    Exit While
    End Try
    end while
    Well, this code works quite well. I have to optimize the speed, because at the moment it's about 4kB/s, but basically the data transfer works.
    Last edited: Jul 13, 2015
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice