Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help regarding my J2ME client server application  RSS feed

Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am making one client server application in J2ME.
The client is a midlet from which i want to send all the image files one by one from the phone file system to the server where there is a servlet.

What i am doing is dividing the total file size for one file.

Looping upto that much counter i am opening a inputstream, reading the data into that input stream ,opening an output stream ,writing the read data to that output stream.

This process i am doing upto the no of parts the file gets divided as mentioned above & each time i am reding like this:
t = 0;
byte[] buf = byte[2000];
for(i =0;i<counter;i++)

where is & os are input stream & output stream.t is the offset for reading.

The problem i am facing is for the first i =1 the bytes gets read & write & servlet is sending the response code 200 .
But as soon as i=2 the program throws exception from reading line & on the server side Socket exception is thrown : Read timed out.

So anybody could help me designing this abt how to achiev this in the easiest way or provide me a sample code for the client side & servlet if anybody has done this.

Regards ,
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following loop
>for(i =0;i<counter;i++)
for first time its read successfully till 2000 bytes and written to server. However when second time the read API that is being used expects the buffer size to which data is to be copied to be of atleast (t+2000), which is true when i =0 but as soon as i > 0 then buffer size is less than that expected. So its throwing exception (IndexOutOfBoundsException I think so). So instead of using this version of read try using read(buf) instead. Also if you know the size of file that is being read then buffer size can be fixed using byte[] buf = new byte[is.available()]; Hope this helps
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!