News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
Client makes "invalid stream header in JavaServer ?  
by Krzysztof Paz [kpaz at samorzad dot pw dot edu dot pl]
posted on 2003/09/01

There is multithreaded JavaServer which opens a defined port and listens for incoming TCP/IP connections.
I've a problem with connecting to this JavaPowered SSL Server with sockets from CSharp.
While I'm connecting from C# to Java, there is a such exception on JavaServer side:
java.io.StreamCorruptedException: invalid stream header at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at ClientConnection.<init>(ClientConnection.java:54)
---
Some lines from Java Side:
[..]SSLSocket client = (SSLSocket) server.accept();
...
[54] datain = new ObjectInputStream(client.getInputStream ());
[55] dataout = new ObjectOutputStream(client.getOutputStream ());
---
Some lines from my C# code:
---
m_ResetEvent = new ManualResetEvent(false);
SecurityOptions options = new SecurityOptions(
SecureProtocol.Ssl3, // use SSL3
null, // do not use client authentication
ConnectionEnd.Client, // this is the client side
CredentialVerification.None, // let the SecureSocket verify the remote certificate automatically
null, // not used with automatic certificate verification
targetserver, // this is the common name of the server
SecurityFlags.Default, // use the default security flags
SslAlgorithms.SECURE_CIPHERS, // only use secure ciphers
null); // do not process certificate requests.
try {
m_Socket = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
IPEndPoint endpoint = new IPEndPoint(Dns.Resolve(targetserver).AddressList[0], targetport);
String request = "GET_SERVER_VERSION";
byte[] buffer = Encoding.ASCII.GetBytes(request);
SecureSocket s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
s.Connect(endpoint);

/*
SecureTcpClient stcp = new SecureTcpClient(targetserver, targetport, options);
SecureNetworkStream str = stcp.GetStream();
Console.Write("Writing request: '"+request+"' to server...");
str.Write(buffer,0,buffer.Length);
buffer = new byte[1024];
Console.Write("Reading answer...");
str.Read(buffer,0,1024);
Console.Write(Encoding.ASCII.GetString(buffer, 0, 1024));
*/

int sent = s.Send(buffer, 0, buffer.Length, SocketFlags.None);
buffer = new byte[4096];
int ret = s.Receive(buffer);
while(ret != 0) {
Console.Write(Encoding.ASCII.GetString(buffer, 0, ret));
ret = s.Receive(buffer);
}
s.Shutdown(SocketShutdown.Both);
s.Close();
/*
m_Socket.BeginConnect(endpoint, new AsyncCallback(this.OnConnect), null);
m_ResetEvent.WaitOne();
m_Socket.Close();*/
} catch (Exception me){
OnError("Error with server conversation...");
Console.WriteLine("\nDEATILS: "+me.ToString());
}
Console.WriteLine("Press enter to quit.");
Console.ReadLine();
}
-----------------
My JavaClient Classes are working great...
Exeption is throwed while Java Server is trying to call: [client.getInputStream].

So, help me please with this issue...
Greetings from Poland.
Kris.

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2003/09/01

The problem appears to be the ObjectInputStream and ObjectOutputStream classes you're using. These classes expect strictly formatted data, and the data you're sending from the .NET side does not follow that formatting, hence the "invalid stream header" exception. It has nothing to do with SSL or TLS.

You can easily test this with the following Java example. It does exactly what your program is doing, but it doesn't use SSLSocket objects.

byte[] buffer = "GET_SERVER_VERSION".getBytes();
ByteArrayInputStream istream = new ByteArrayInputStream(buffer);
ObjectInputStream p = new ObjectInputStream(istream);

This example will throw the exact same error you're seeing in your application.

by sundaram [rs rajasekaran at lntinfotech dot com]
posted on 2004/02/05

Whats the solution for this exception.
I have the same code what you have written

by Kris [kpaz at samorzad dot pw dot edu dot pl]
posted on 2004/02/14

So, AFIK you are absolutly right.
Thanks for this info.
But the problem still persist - and now there is another question:
"How to communicate with Java.Object.Streams from C#(sharp) lang?"
Any ideas or advices ?
Regards,
Christopher.

by bl0rf
posted on 2004/06/04

Object streams throw an invalid header exception when the input they are getting is not an object - this happened to me when I was accidentally sending garbage to the Object stream. Make doubly, and triply sure that what you are sending is a perfectly valid formatted object ( you can print the chars you are printing to the Obj stream to System.out for this ).

- bl0rf

by Ira Juneau [irajuneau79 at yahoo dot com]
posted on 2005/02/11

I just fixed this problem by making a lock on my sending port. I was sending multiple objects at the same time and they were becoming jumbled in the port, you may want to make certain this is not happening to you

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.