Hi !
I get a strange problem regarding URLConnection in JDK 1.4.Here is the
situation : I have a client server application in which the client (askhim)
uses either a Socket or an URLConnection to talk to the server side (askme).
The client sends messages (ASCII) as byte arrays to the server, the server
passes the messages to askme which parses it and returns a response (ASCII)
again as byte arrays.
Now, I have to version of the server, one of which is a regular
SocketServer, and the other a HTTPServlet. So the client either use a Socket
to send the messages or an URLConnection.
Everything works perfectly. However, I recently decided to have the client
run with JDK1.4. The SocketServer version still works perfectly, but when I
use the servlet version, the byte[] messages I receive from the servlets are
truncated. Actually, the messages have the right length but only contain the
first few bytes of data, and the remaining of the array is filled with '0'.
Seems to me that something changed between jdk1.3 and jdk1.4 regarding
URLConnections.
BTW, the servlet runs in jakarta-tomcat, so it uses JDK 1.3. But I guess
there should be no problem in having my client in JDK1.4 and the servlet in
JDK1.3, especially since I simply send and receive byte arrays...
Here is a sample clode : This is the client code sending to the servlet and
reading from it.
DataOutputStream out = null;
DataInputStream in = null;
try {
URL url = new URL(transportServerURL);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
out = new DataOutputStream(connection.getOutputStream());
sender.send(out, b);
in = new DataInputStream(connection.getInputStream());
return receiver.receive(in);
} finally {
out.close();
in.close();
}
This is the servlet code :
DataInputStream in = new DataInputStream(request.getInputStream());
DataOutputStream out = new DataOutputStream(response.getOutputStream());
try {
byte[] message = receiver.receive(in);
message = mapping.perform(message);
sender.send(out, message);
} finally {
in.close();
out.close();
}
And the sender/receiver you see are :
(sender :)
byte[] size = new byte[4];
size[0] = (byte) (message.length & 0x000000ff);
size[1] = (byte) ((message.length >> 8) & 0x000000ff);
size[2] = (byte) ((message.length >> 16) & 0x000000ff);
size[3] = (byte) ((message.length >> 24) & 0x000000ff);
out.write(size);
out.write(message);
out.flush();
(receiver :)
byte[] size = new byte[4];
in.read(size);
int iSize = (size[0] & 0x000000ff) | (size[1] & 0x000000ff) << 8 | (size[2]
& 0x000000ff) << 16 | (size[3] & 0x000000ff) << 24;
byte[] message = new byte[iSize];
in.read(message);
As you can see, this is very simple. And very very basic. But when messages
get longer (like 8k), then I only get the first few bytes followed by '0's.
Anyone have an idea on this ?
Here is my e-mail address :
[EMAIL PROTECTED]
Have a nice day all !
Max.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html