How to send a string to server

by TheSeeker » Thu, 02 Sep 2010 01:55:57 GMT


Sponsored Links
 Hi
I am trying to send a string to server, i would like to know how many
alternatives i have. Actually i am using a socket but i don't know if
there is a better option.
The string is big(lenght about 192000 , yes too much 192000), i think
is a little is stupid try to do this, but the server works with
sending a string to one computer to other computer and my mision is to
make a conection between a computer like server with a client (android
phone). If i can send i big string so i do not need to change the
program on the server.
I need info about how to send a string with
1. GPRS EDGE
2. Wi-FI

--



How to send a string to server

by TheSeeker » Thu, 09 Sep 2010 05:35:07 GMT


  did it, but now i have a problem with garbage collector on android
(GC freed) and the time spend it on send a big string(array of char)
to a server
Here code snippet

xm1,xm2a,xm2b,xm4,xm5,xm6 are String of Java, array is an array of
char huge char[] array = new char[900000]
try{
Log.i("Android","before DOS");
out = new
DataOutputStream(s.getOutputStream());
Log.i("Android","later DOS");
Log.i("Android","write xm1
xm2");
out.writeBytes(xm1);
out.writeBytes(xm2a);

Log.i("Android","writeByte BigString 1");
for(int a =0;a<nc;a++){
out.writeByte(array[a]);
}

//out.writeBytes(xm3);
Log.i("Android"," End writeByte BigString
1");
out.writeBytes(xm4);
out.writeBytes(xm2b);
Log.i("Android","writeByte BigString 2");

for(int a =0;a<nc;a++){
out.writeByte(array[a]);
}

//out.writeBytes(xm3);
Log.i("Android","End writeByte BigString
2");
out.writeBytes(xm4);
out.writeBytes(xm5);
out.writeBytes(xm6);
Log.i("Android","END writeByte xm4 5 6");

}
catch(Exception e0){
e0.printStackTrace();
}

The logCat(DDMS view) said

09-08 16:41:26.677: INFO/Android(1926): before DOS
09-08 16:41:26.687: INFO/Android(1926): later DOS
09-08 16:41:26.687: INFO/Android(1926): write xm1 xm2
09-08 16:41:26.687: INFO/Android(1926): writeByte BigString 1
09-08 16:41:29.257: DEBUG/dalvikvm(1926): GC freed 35034 objects /
902128 bytes in 175ms
09-08 16:41:31.187: DEBUG/dalvikvm(227): GC freed 106 objects / 53400
bytes in 192ms
09-08 16:41:32.247: DEBUG/dalvikvm(1926): GC freed 39530 objects /
949520 bytes in 175ms
09-08 16:41:34.907: DEBUG/dalvikvm(1926): GC freed 37157 objects /
892248 bytes in 178ms
09-08 16:41:37.587: DEBUG/dalvikvm(1926): GC freed 37157 objects /
892248 bytes in 166ms
09-08 16:41:40.277: DEBUG/dalvikvm(1926): GC freed 37157 objects /
892248 bytes in 173ms
09-08 16:41:42.927: DEBUG/dalvikvm(1926): GC freed 37157 objects /
892248 bytes in 165ms
09-08 16:41:45.587: DEBUG/dalvikvm(1926): GC freed 37157 objects /
892248 bytes in 174ms
09-08 16:41:47.587: INFO/Android(1926): End writeByte BigString 1
09-08 16:41:47.587: INFO/Android(1926): writeByte BigString 2
09-08 16:41:48.247: DEBUG/dalvikvm(1926): GC freed 37146 objects /
892064 bytes in 170ms
09-08 16:41:50.907: DEBUG/dalvikvm(1926): GC freed 37149 objects /
892064 bytes in 173ms
09-08 16:41:53.537: DEBUG/dalvikvm(


Sponsored Links


How to send a string to server

by Frank Weiss » Thu, 09 Sep 2010 05:47:40 GMT


 Looks bad, very bad.

First, I have no idea why you're allocating 1.8 MB (remember that Java, and
I suppsoe Dalvik as well, takes 16 bits per char). How come so much? Second,
of course writing a byte (or is it a char, there's a mismatch there) is
going to be slower than writing at least a few thousand at a time.

--



How to send a string to server

by TheSeeker » Fri, 10 Sep 2010 12:41:50 GMT


 yes, i had a mismach, thanks. But i still have the same problem with
time(1 min ) and GC. Create 2 or more array instead of the big one
will solve the problem with time?




--



How to send a string to server

by Miguel Morales » Fri, 10 Sep 2010 12:53:48 GMT


 No, breaking it into two arrays will not help you.
I haven't done this myself, but I THINK what you'll want to do is use
a BufferedWriter  or something similar.  In any case, you'll HAVE to
buffer your data and send it in chunks.  HTTP supports this quite
well.  Take a look at some BufferedWriter tutorials, or perhaps
someone can suggest a better approach.




>



How to send a string to server

by TheSeeker » Thu, 23 Sep 2010 23:54:35 GMT


 I have reduced lenght of byte to 250 000 (byte[] array = new
byte[250000] instead of char huge char[] array = new char[900000] )

I've been trying sending string instead of character by character. if
string is big (250 000 characters aprox and 250 Kbytes) de Garbage
collector go crazy and the time spend is too much (more than 2 min and
it is funny). Then i do other experiment, split the string in a lot of
little string of lenght of 1024 bytes and 256 bytes. The conclusion is
if string is huge the time is infinite and CG produce the spend of
time, and when the string is small 256bytes GC seem to do not produce
the problem. In the extreme case when i send byte by byte (Log posted
above) is the best case

In the original problem is the best case, because GC spent 1,2 seg(150
ms*8) aprox but my chronometer in my hand said 15 seg.

This is rare for me because if i send byte by byte i will be sending a
header of TCP/IP about 40bytes. If i send byte by byte i will be
sending 40*(1Mbytes of info) = 40Mbyes and it work better than send
1Mbyte in a string





> >



How to send a string to server

by Kostya Vasilyev » Fri, 24 Sep 2010 00:10:24 GMT


  You should be able to come up with some middle ground number for your 
buffers - sending one byte at a time is obviously bad, and so is 
allocating the entire 900K buffer.


Using a send buffer around 8K to 16K should work pretty well. My guess 
is that you need to do Base64 encoding on the fly - in that case, you 
can tweak input chink size to that there is no padding at the end of 
encoded data (the "==") and the next encoded chunk seamlessly fits at 
the end of the previous one.


Finally, where does the string come from? If it comes from a file, you 
should not read the whole thing at once.


-- Kostya

23.09.2010 19:54, TheSeeker :
I have reduced lenght of byte to 250 000 (byte[] array = new byte[250000] instead of char huge char[] array = new char[900000] ) I've been trying sending string instead of character by character. if string is big (250 000 characters aprox and 250 Kbytes) de Garbage collector go crazy and the time spend is too much (more than 2 min and it is funny). Then i do other experiment, split the string in a lot of little string of lenght of 1024 bytes and 256 bytes. The conclusion is if string is huge the time is infinite and CG produce the spend of time, and when the string is small 256bytes GC seem to do not produce the problem. In the extreme case when i send byte by byte (Log posted above) is the best case In the original problem is the best case, because GC spent 1,2 seg(150 ms*8) aprox but my chronometer in my hand said 15 seg. This is rare for me because if i send byte by byte i will be sending a header of TCP/IP about 40bytes. If i send byte by byte i will be sending 40*(1Mbytes of info) = 40Mbyes and it work better than send 1Mbyte in a string
No, breaking it into two arrays will not help you. I haven't done this myself, but I THINK what you'll want to do is use a BufferedWriter or something similar. In any case, you'll HAVE to buffer your data and send it in chunks. HTTP supports this quite well. Take a look at some BufferedWriter tutorials, or perhaps someone can suggest a better approach.
yes, i had a mismach, thanks. But i still have the same problem with time(1 min ) and GC. Create 2 or more array instead of the big one will solve the problem with time?
Looks bad, very bad. First, I have no idea why you're allocating 1.8 MB (remember that Java, and I suppsoe Dalvik as well, takes 16 bits per char). How come so much? Second, of course writing a byte (or is it a char, there's a mismatch there) is going to be slower than writing at least a few thousand at a time.
--



How to send a string to server

by DanH » Fri, 24 Sep 2010 00:58:16 GMT


 ote that a lot depends on how you break up the large array into
smaller pieces. The absolute worst thing you can do is to "break" the
original array into two pieces -- reducing the length of the
"remainder" with each cycle. This beats the hell out of the garbage
collector and sends performance down the toilet. The best approach,
of course, is to use an interface that allows you to specify an array
and start/end dimensions, so that no copying is required -- but I
don't know that such an interface is available in the scenario you're
looking at.

And note that you should use System.arraycopy (or possibly some
Android equivalent I don't know about) to copy data from an original
array into a smaller buffer (if copying is necessary at all). Don't
do a byte-at-a-time copy in a loop. And don't create a new buffer
array for each iteration -- reuse the previous buffer array.

On Sep 23, 10:54am, TheSeeker <alvaro.herr...@yahoo.com> wrote:
> > >



How to send a string to server

by Pawe艂 Zi臋ba » Fri, 24 Sep 2010 01:44:58 GMT


 Do you really need a such big array allocated in memory?
Maybe you want to save it to file or database?
If so, you don't need to create the huge array.
Just try to redirect stream.






> > >



How to send a string to server

by Kumar Bibek » Fri, 24 Sep 2010 02:09:01 GMT


 I would suggest that you save the data into a file. and then, send a
post request with a FileEntity/InputStreamEntity. I have used it with
HttpClient and it works fine. You would be relieved of all the manual
work in this case,

-Kumar Bibek
 http://techdroid.kbeanie.com 







> > > >



How to send a string to server

by TheSeeker » Sat, 02 Oct 2010 03:39:55 GMT


  found the solution but i don't have a logic explanation:

In a class named myclass extends Thread, I put the follow
public static byte[] utf1= new byte[300000]
.......
InetAddress addr = InetAddress.getByName(IP);
SocketAddress sockaddr = new InetSocketAddress(addr,PORT);
s1 = new Socket();
s1.connect(sockaddr, 1000);
out = new DataOutputStream(s1.getOutputStream());
out.flush();
out.writeBytes(xm1);//xm1 is a little string of 5 lines
out.flush();
int nc1 = number;// is the lenght of the array until i want
send, because array is not full and it have some byte of garbage
out.write(myclass.utf1,0,nc1);// generally nc1 is 200 000
out.flush();
......
s1.close();
out.close();

//to receive i use:
BufferedReader in = null;
in = new BufferedReader(new
InputStreamReader(s1.getInputStream()));
String x="";
int i=0,j=0;
while(i<16){
x = in.readLine();
j=xmlres.indexOf("something");
if(j!=-1){
break;
}
i++;
}

That all. it work fast without garbage collector to send like 1Mbyte.
When i used a string, garbage collector go crazy with the same data,
so my suggest to others is to use an array of byte and send the whole
array to the server. I probe the code on a real device and very old
HTC Magic with API level 3(Android 1.5) and it works fine.

On Sep 23, 2:08pm, Kumar Bibek <coomar....@gmail.com> wrote:
> > > > >



How to send a string to server

by DanH » Sat, 02 Oct 2010 04:09:43 GMT


 'd suggest that you not create the byte array in <clinit> like that
but rather make it an instance variable. The way you're doing it that
large array is getting allocated as soon as myclass is loaded, (which
may be well before you first create an instance of the class) and
isn't freed until myclass is unloaded (a lifetime which may be much,
much more than the time you're actually doing a transfer). Plus if
you ever used your function in multiple threads the threads would
"share" that one array -- with disastrous results.

On Oct 1, 2:39pm, TheSeeker <alvaro.herr...@yahoo.com> wrote:
> > > > > >



How to send a string to server

by Kostya Vasilyev » Sat, 02 Oct 2010 04:26:04 GMT


 Using a send buffer of 8 or 16 kilobytes gives pretty good performance, and
avoids problems with garbage collection.

--
Kostya Vasilyev --  http://kmansoft.wordpress.com 

02.10.2010 0:09  "DanH" <danhi...@ieee.org> :

I'd suggest that you not create the byte array in <clinit> like that
but rather make it an instance variable.  The way you're doing it that
large array is getting allocated as soon as myclass is loaded, (which
may be well before you first create an instance of the class) and
isn't freed until myclass is unloaded (a lifetime which may be much,
much more than the time you're actually doing a transfer).  Plus if
you ever used your function in multiple threads the threads would
"share" that one array -- with disastrous results.





--



How to send a string to server

by Miguel Morales » Sat, 02 Oct 2010 06:07:37 GMT


 Wow, that is really bad code.  Don't you know what streaming or buffering is?

2010/10/1 Kostya Vasilyev <kmans...@gmail.com>:

>



Other Threads

1. How to add code to HAL layer without adding to hardware directory

We have downloaded Android source and are developing pieces in the HAL
layer that we want to interact with Android System.

Question :
We would like to keep the Android code that we downloaded intact as
much as possible. Most of the interfaces that we are using are from ./
hardware/ directory (atleast the interfaces that are available).. We
would like to extend/use these interfaces to develop specific HAL
components.

We intend to create different set of directories that will hold the
company specific piece of the HAL code.

Is there a way in Android Build System/etc that lets us develop code
outside the Android directories without necessarily modifying Android
code base.

Thx
Sanjay Dhar
--~--~---------~--~----~------------~-------~--~----~

2. Is there a way to listen for the completion of AnimationDrawable animation?

For tweening animation there is a callback that can be registered on
the Animation object.

I dont see anything similar on AnimationDrawable.

Any thoughts?

Thanks for your help
Satya

--~--~---------~--~----~------------~-------~--~----~

3. Developers, the Apps We Need Most!

4. Developers, the Apps We Need Most! --- no need for app killer

5. Google Notebook

6. Where to get widget code

7. Virtual Machinery announces Android BTree implementation - demo available