SocketServer and a Java applet listener

google at phaedro.com google at phaedro.com
Mon Aug 29 02:27:54 EDT 2005


Steve Horsley schreef:

> google at phaedro.com wrote:
> > Dear newsgroup,
> >
> > I give up, I must be overseeing something terribly trivial, but I can't
> > get a simple (Java) applet to react to incoming (python) SocketServer
> > messages.
> >
> > Without boring you with the details of my code (on request available,
> > though), here is what I do :
> >
> > I have a TCPServer and BaseRequestHandler .
> > Connecting via telnet : everything goes OK.
> >
> > Connecting from Applet :
> > problem 1 (worked around it) : java has some 'propietary' UTF-8 format,
> > python's unicode doesn't seem to handle it correctly and I have to
> > strip the first two bytes/chars , then all goes OK .
> >
>
> Those 2 bytes are important! They are a string length indicator.
> Here are the docs that tell you that it puts a 2-byte length on
> the front:
> http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataOutputStream.html#writeUTF(java.lang.String)
> If you are ignoring these bytes, then how can you be sure you
> have received the whole string? Please don't tell me you "just
> hope" that the whole string will always arrive in a single read()
> call. There is no guarantee of that. TCP does NOT have message
> boundaries, and TCP packets can be both fragmented and coalesced.

Ah, I see... I worked around this (see below), the workaround is
consistent with what you assume.
My question "How to send/receive between python SocketServer and java
Applet (SocketListener)" then seems to be boiling down to: "How to
interface between python unicode and java read/writeUTF?"

>
> Probably the same problem. If you didn't send a 2 byte length
> indicator first, then java's readUTF() will have tried to
> interpret the first 2 bytes that you did actually send as the
> string length, and may well simply be waiting patiently for the
> rest to arrive.
>
I just couldn't get read/writeUTF and python unicode to interface, so I
refactored the applet's socketlistener to convert the
socket.getInputStream to a BufferedInputReader on which I call the
readline() method.
I still skip the first two bytes in the python receiver which seems
harmless since python doesn't use the byte length.
On both sides I have other way to know when the end-of-message has been
reached. A timeout mechanism provides some safety for partial
transmission handling.


Thanks !



Thijs




More information about the Python-list mailing list