SocketServer and a Java applet listener

google at phaedro.com google at phaedro.com
Mon Aug 29 17:48:41 EDT 2005


Steve Horsley schreef:

> google at phaedro.com wrote:
> > Steve Horsley schreef:
> >
> >
> >>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.
<snap>

> There are two normal ways to delineate messages in the
> byte-stream: An explicit length indication up front (which java
> read/writeUTF chooses), or a unique end-of-message indication at
> the end such as your readline() for strings that end in linefeed.
>

<snap>

>
> If you choose to go for the java read/writeUTF approach, the
> 2-byte length indicator goes Most Significant Byte first, so a
> 100 char string would be preceded by 00 64 ... Also, the
> indicator gives the number of bytes after encoding, not the
> number of characters before encoding.
>

You are right, Steven. I invested some time and isolated the problem of
transferring UTF-8 strings between a python SocketServer and a Java
applet. In a very, very draft version, hereby surrounded by every
disclaimer imagineable (as in "Don't try this at home!") I have put the
result on a webpage:
http://www.phaedro.com/javapythonutf8/

Not only I tried to give a more or less 'generic' solution to the UTF-8
interface (reversing the leading bytes using python's struct module,
and based on SocketServer.StreamingRequestHandler), maybe the
draft-craft helps others looking for (rather scarce) examples of
SocketServer implementations - this one is very trivial so maybe others
can learn more efficiently than I had to do.

-- 
Thijs Cobben
Explicit typing sux.




More information about the Python-list mailing list