Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2003

Tip: Looking for answers? Try searching our database.

Tomcat doesn't send all data while Jetty does

Thread view: 
BigAl - 11 Jul 2003 14:22 GMT
 I am using the following code in a servlet to send data back to a J2ME
application:

               .
               .
        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();
               .
               .

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=
Roedy Green - 11 Jul 2003 18:44 GMT
>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.
Robert Klemme - 14 Jul 2003 14:38 GMT
redir to cljs

>   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?

>                 .
>                 .
[quoted text clipped - 6 lines]
>          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

>                 .
>                 .
[quoted text clipped - 24 lines]
>
> =Alan=
BigAl - 17 Jul 2003 21:11 GMT
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=

> redir to cljs
>
[quoted text clipped - 59 lines]
>>
>>=Alan=
Shyamal Prasad - 17 Jul 2003 22:46 GMT
   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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.