I’m trying to send a public key object of type Key to my Server. But I didn’t get this running. The prototocl looks the following:
[Command]\n[serialized Key object]
The client uses this code:
Socket admin;
PrintWriter pw;
OutputStream os;
BufferedReader is;
for(int tries = 0; tries < MAX_RECONNECT_TRIES_ADMIN_SERVER; tries++)
{
try
{
admin = new Socket(host,port);
os = admin.getOutputStream();
is = new BufferedReader(new InputStreamReader(admin.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(os));
AdminServerCommand.NODE_REGISTER.writeToPrintWriter(pw);
pw.flush();
sendPublicKey(os);
String resultLine = null;
resultLine = is.readLine();
if(AdminServer.Feedback.KEY_REGISTERED.commandMatch(resultLine))
{
is.close();
os.close();
admin.close();
return true;
}
is.close();
os.close();
admin.close();
registerNodeRetrySleep(1000);
}
catch (Exception e)
{}
}
return false;
public void sendPublicKey(OutputStream out)
{
try
{
ObjectOutputStream outO = new ObjectOutputStream(out);
outO.writeObject(cyper.getPublicKey());
outO.flush();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void writeToPrintWriter(PrintWriter os)
{
if(os == null)
throw new IllegalArgumentException("Can not write command to null stream.");
os.println(comm);
os.flush();
}
The Server uses
String com = "";
try
{
if(client.getInputStream().available() > 2)
com = is.readLine();
}
catch (IOException e)
{
errorResponse(Error.COMMAND_ERROR);
}
Key key = null;
try
{
ObjectInputStream keyIn = new ObjectInputStream(client.getInputStream());
key = (Key)keyIn.readObject();
}
catch(Exception b)
{
b.printStackTrace();
errorResponse(Error.BAD_KEY);
return;
}
The Exception looks the following:
Nov 28, 2012 9:52:59 PM AdminServer.AdminServer run
INFO: Get a new request
java.io.StreamCorruptedException: invalid stream header: 73720014
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at AdminServer.AdminServer.registerNewKeyEntry(AdminServer.java:115)
at AdminServer.AdminServer.run(AdminServer.java:65)
at java.lang.Thread.run(Unknown Source)
Nov 28, 2012 9:52:59 PM AdminServer.AdminServer errorResponse
WARNING: Error: Bad public key format. Use object stream with key object.
Does anybody now how I can solve this. The type Key is an Interface type that implements itself the Interface Serializable. So it shouldn’t be a problem to serialize this object. I spend the whole evening with this Problem. Hope that anybody can help me getting rid of this.
There is not enough code in your question to diagnose completely – but here are a couple of observations.
This code looks suspicious:
Presumably you have a buffered reader
iswrapping the client input stream. What happens if the if statement is not true – you skip reading the line? Now that line of text is still in the pipeline and will pass to your keyIn.readObject method. That could cause the corrupt error.I would recommend just removing the whole
ifline. readLine() blocks anyways so there is no need for the check.Also, are you absolutely sure that
AdminServerCommand.NODE_REGISTER.writeToPrintWriter(pw);sends exactly one line of text with absolutely no characters after the line feed?However – I think you have a bigger issue here. This design where you are switching back and forth between Java object serialization and manual text reading/writing is just a disaster waiting to happen. If you want to use object serialization, use it exclusively. You can alternate sending a String object, then a Key object and your stream won’t get corrupted because you sent one too many or one two few line breaks.
For example:
ObjectOutputStream outO = new ObjectOutputStream(out);
Then on the server side, always use readObject, knowing that the first will be a command, the second will be a key.