By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,572 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,572 IT Pros & Developers. It's quick & easy.

Tomcat doesn't send all data while Jetty does

P: n/a
I am using the following code in a servlet to send data back to a J2ME
application:
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 11 Jul 2003 09:22:53 -0400, BigAl <a.***********@ieee.org>
wrote or quoted :
Simply put, what is wrong ? Is there a setting in Tomcat that I should
look at ?


If you are reading at the raw socket level, remember that the data
come in chunks, not all in one piece.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #2

P: n/a
redir to cljs

"BigAl" <a.***********@ieee.org> schrieb im Newsbeitrag
news:3F**************@ieee.org...
I am using the following code in a servlet to send data back to a J2ME
application:
Which version of Tomcat do you use? Is it current?
.
.
data = myResp.serialize();
response.setStatus(response.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
os.flush();
os.close();
First, flush is superfluous before a close, as defined by OutputStream.

Second, I'd prefer to use response.flushBuffer() instead of os.flush() and
leave out os.close(). Try this:

data = myResp.serialize();
response.setStatus(response.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
response.flushBuffer();

Regards

robert
.
.
and the following input code on the J2ME side (simplified a bit):
.
.
int len = (int)conn.getLength();
in = conn.openInputStream();
_data = new byte[len];
while (total < len) {
total += in.read(_data, total, len - total);
}
.
.
Using Jetty as the servlet container, I am able to SEND and RECEIVE data
without any problem. Using Tomcat, I usually get a -1 on the CLIENT side
when trying to read the data. The CLIENT gets the length from the
content header but there doesn't seem to be any data.

Simply put, what is wrong ? Is there a setting in Tomcat that I should
look at ? Why would Jetty work with this code and not Tomcat ? This
seems like simple standard stuff.

Thanks

=Alan=


Jul 17 '05 #3

P: n/a
I'm using Tomcat Version 4.1.24 - fairly recent. Jetty is Version 4.2.9
- also fairly recent.

I tried the response.flushBuffer() but I still have the same issue
(detailed below). Here is some more information:

If I run my J2ME app within the Wireless Toolkit, Tomcat and Jetty both
work fine. When I download the app to a phone (in this case a Motorola
T720), the app is able to receive messages from the Jetty servlet
container but not from Tomcat.

Here is a snippet of the phone code where we read from the input stream:

byte[] _data;
int len = (int)conn.getLength();
in = conn.openInputStream();

if (len != -1) {
_data = new byte[len];
while (total < len) {
total += in.read(_data, total, len - total);
}
} else {
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
tmp.write(ch);
total++;
}
_data = tmp.toByteArray();
}
Thanks

=Alan=

Robert Klemme wrote:
redir to cljs

"BigAl" <a.***********@ieee.org> schrieb im Newsbeitrag
news:3F**************@ieee.org...
I am using the following code in a servlet to send data back to a J2ME
application:

Which version of Tomcat do you use? Is it current?

.
.
data = myResp.serialize();
response.setStatus(response.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
os.flush();
os.close();

First, flush is superfluous before a close, as defined by OutputStream.

Second, I'd prefer to use response.flushBuffer() instead of os.flush() and
leave out os.close(). Try this:

data = myResp.serialize();
response.setStatus(response.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
response.flushBuffer();

Regards

robert

.
.
and the following input code on the J2ME side (simplified a bit):
.
.
int len = (int)conn.getLength();
in = conn.openInputStream();
_data = new byte[len];
while (total < len) {
total += in.read(_data, total, len - total);
}
.
.
Using Jetty as the servlet container, I am able to SEND and RECEIVE data
without any problem. Using Tomcat, I usually get a -1 on the CLIENT side
when trying to read the data. The CLIENT gets the length from the
content header but there doesn't seem to be any data.

Simply put, what is wrong ? Is there a setting in Tomcat that I should
look at ? Why would Jetty work with this code and not Tomcat ? This
seems like simple standard stuff.

Thanks

=Alan=


Jul 17 '05 #4

P: n/a
"BigAl" == BigAl <a.***********@ieee.org> writes:

BigAl> phone (in this case a Motorola T720), the app is able to
BigAl> receive messages from the Jetty servlet container but not
BigAl> from Tomcat.
BigAl> Here is a snippet of the phone code where we read from the
BigAl> input stream:

byte[] _data;
int len = (int)conn.getLength();
in = conn.openInputStream();

I've never done J2ME, but I am guessing that the conn.getLength()
method is not returning the number of bytes in the response, it is
returning the number of bytes available to read from the connection
(i.e. you are hitting a network timing issue).

if (len != -1) {
_data = new byte[len];
while (total < len) {
total += in.read(_data, total, len - total);
}
} else {
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
tmp.write(ch);
total++;
}
_data = tmp.toByteArray();
}

This remaining code makes me even more suspicious. When do you get
-1 for len? Why? Does you code work if you simply use only the 'else'
clause? Perhaps I'm wrong since I know nothing about J2ME, but the
mistake I am describing is a very common one when people write code to
read off TCP connections.

Cheers!
Shyamal
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.