1

I have an Android app that communicates with a server via asynchronous socket connection to transfer somewhat large packets of information (up to 11 Megs). In order to accommodate for this interactions I have placed small sleeps in key points of the data sending and receiving. On WiFi the connection works. Do to the size of the information one read or write doesn't seem to be sufficient but I have created functions to keep reading off the socket until the transfer is finished.

As I said all of this works on WiFi. However, on 3G or edge networks the transfer fails, interrupts midway, and generally is buggered. I have placed numerous checks and sleeps to ensure that information is given the opportunity to pass, however it fails to.

Unfortunately it is difficult to be clearer as to the source of the problem. I have worked for several days trying to clear up this problem as elegantly as possible with no avail.

TLDNR WiFi is able to send data but mobile networks fail. I need to know why and how to fix it.

Thank you.

Ashterothi
  • 3,282
  • 1
  • 21
  • 35
  • Can you recreate the problem with some small simple code that you could post here? :) Also, I wouldn't bother inserting `sleep()` calls into your code; `write()` on TCP is going to be a blocking operation, so your sending thread is going to be put to sleep anyway when the kernel TCP stack can't accept more input. Just make a quick tight loop and `write()` 4096 bytes at a time. (Good small magic number. Not too big, not too small, and maybe you'll get lucky and get page-aligned faster data access.) – sarnold May 17 '11 at 00:08
  • One thing I discovered very early on in this project is that when sending data a max amount is sent at any given time. If I attempt to do another write without at least a very small sleep, the entire write becomes worthless. Likewise I actually got the 3g to work with a 3 second sleep per read (which is a terribly long time), however after doing some other changes that stopped working. That is why I'm trying to find a better way. I will work on getting some code for you. – Ashterothi May 17 '11 at 16:08
  • I finally worked out the an answer: Basically I assumed that my read() on 3g would be large enough for a meaningful chunk of data. Thank you for the 4096 it pointed me in the right direction. Basically it reads the information (XML data) until it gets to a point that it is meaningful and then proceeds forward. Thanks! – Ashterothi May 17 '11 at 21:01

2 Answers2

2

The actual problem turned out to be that the amount received per read() was substantially smaller then I had expected. My program required the entire schema to be sent via XML before the program knew how to use it. It wasn't receiving enough of the string, and so would die. Now it checks for if the /schema tag is in the string (via if (!response.contains("/schema"))) and does not stop reading until it finds that or times out.

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
Ashterothi
  • 3,282
  • 1
  • 21
  • 35
0

You probably want a solution that will allow for resuming interrupted data transfers. Take a look at how to resume an interrupted download - part 2 and https://stackoverflow.com/search?q=[android]+resume+download for examples.

Also, I highly recommend watching Virgil Dobjanschi's Google I/O 2010 presentation on Android REST client applications, in which he describes designing an app for handling such data and network communications. He is the author of the official Twitter app for Android.

Community
  • 1
  • 1
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97