473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SocketServer and a Java applet listener

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 BaseRequestHand ler .
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 .

problem 2:
I have tried IMHO everything.
In the BaseRequestHand ler.handle() method, I want to update a list of
clients in the server, i.e.:

self.server.pla yers[username] = self

self := instance of the BaseRequestHand ler, I only do this after
succesfull connect , i.e. first time socket. I assume (wrongfully?)
that I can now use the self.request socket for future transmissions to
the client.

In the applet, I start a thread that listens to the socket by eternally
looping over:
String line = self.din.readUT F()
if (line == null)
break;
handle(line);

self := instance of Thread
self.din := DataInputStream (socket.getInpu tStream());

(It's a bit simplistic, but I am quite sure (well...) I got those
things right, the issue seems to me to lie in some weird
java-python-socket anomaly having to do with close()/flush() etc. ...)

However, the handle(line) method only seems to get called when I
destroy (close ?) the socket on the server side. I tried making it a
wfile (socket.makefil e) and calling the flush() method.
Moreover, I searched and googled and couldn't find reference to a
working implementation of a python SocketServer sending to a java
Applet (socket listener).
Would be much appreciated if anyone knows such a reference ?

Any pointers to correct handling of the 'java propietary UTF-8 format'
in python (xml.sax) would also be appreciated. Skipping the first two
bytes really is a smelly workaround, I know, *deep sigh*...

--
Thijs Cobben
www.phaedro.com

Aug 24 '05 #1
4 5268
go****@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 BaseRequestHand ler .
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/...va.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.
E.g. if you do:
out.write('Stev e was here")
out.flush()
out.write("Bilb o Baggins wasn't")
out.flush()

it is entirely legal for two successive read() calls to retrieve
"steve was " and "hereBilbo Baggins wasn't". Although in
practice, fragmentation won't normally happen until strings reach
around 1500 bytes.

writeUTF tries to fix the problem by telling the receive how much
string to expect.

problem 2:
I have tried IMHO everything.
In the BaseRequestHand ler.handle() method, I want to update a list of
clients in the server, i.e.:

self.server.pla yers[username] = self

self := instance of the BaseRequestHand ler, I only do this after
succesfull connect , i.e. first time socket. I assume (wrongfully?)
that I can now use the self.request socket for future transmissions to
the client.

In the applet, I start a thread that listens to the socket by eternally
looping over:
String line = self.din.readUT F()
if (line == null)
break;
handle(line);


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.

HTH
Steve
Aug 27 '05 #2

Steve Horsley schreef:
go****@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 BaseRequestHand ler .
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/...va.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.getInput Stream to a BufferedInputRe ader 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

Aug 29 '05 #3
go****@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.getInput Stream to a BufferedInputRe ader 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.


I would encourage you to re-think this.

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.

HTTP is an interesting mixture of these - the header contains a
content-length line telling you how long the payload is, but the
header itslef is variable length, terminated by a blank line. In
chunked encoding, there is a variable number of chunks, and the
last chunk is marked as such in the header.

Anyway, either approach is good. But using timing to separate
messages is Bad. There is always the chance that you will get
bitten by strange timings happening later on.

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.

Steve
Aug 29 '05 #4

Steve Horsley schreef:
go****@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.getInput Stream to a BufferedInputRe ader 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.St reamingRequestH andler), 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.

Aug 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3383
by: apchar | last post by:
I am trying to use php as a kind of servlet to act as a middle man between a java applet and mysql. I know java has jdbc but it's flakey and painful. php access to mysql is much nicer. So I have: 1. An html page that holds the applet. 2. a php page that accepts data submitted to it by the applet via the $_POST array and writes it to the mysql database. This page never makes it to the browser window. 3. a simple Thank you page that shows...
0
9873
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ********************************** package Celcom.Client;
1
5699
by: polilop | last post by:
i have an java applet, and when i start it it shows a gray win with x. the console shows java.lang.NoClassDefFoundError: GeomApplet (wrong name: vj3/GeomApplet) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:502) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123) at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:148) at...
12
13404
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a handle_request loop instead of serve_forever, it still seems difficult: class myserver(ThreadingMixIn, TCPServer): pass server = myserver(...) server.shutdown = False while not server.shutdown: server.handle_request()
2
13519
by: Tim Murray | last post by:
First of all, I don't know much about Java, even its naming and version numbering nomenclature, and second, if there is a better group to ask this in, please let me know. System is Mac with 10.4.4. I have Java 1.3.1 and 1.4.2 plug-ins, and J2SE 5.0 (1.5.0) installed. The Java preferences application lets me choose J2SE 5 or 1.4.2 to run applets via a browser. The problem happens in both settings. The problem is that we have a...
1
9649
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
2823
by: Reg | last post by:
I have a following code in HttpRequestListener's ProcessRequest method which is a Windows Console Application. I'm trying to read html page with browser (Opera) and html file contains tags to include Java Applet jar too, for (int i = 0; i < numRequestsToBeHandled; i++) { HttpListenerResponse response = null;
0
1271
by: Reg | last post by:
I have a following code in HttpRequestListener's ProcessRequest method which is a Windows Console Application. I'm trying to read html page with browser (Opera) and html file contains tags to include Java Applet jar too, for (int i = 0; i < numRequestsToBeHandled; i++) { HttpListenerResponse response = null;
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.