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