Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:58:48+00:00 2026-06-15T03:58:48+00:00

I’m trying to send a public key object of type Key to my Server.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:58:49+00:00Added an answer on June 15, 2026 at 3:58 am

    There is not enough code in your question to diagnose completely – but here are a couple of observations.

    This code looks suspicious:

    if(client.getInputStream().available() > 2)
    com = is.readLine();
    

    Presumably you have a buffered reader is wrapping 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 if line. 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);

        String command = "whatever";
        outO.writeObject(command);
        outO.writeObject(cyper.getPublicKey());
        outO.flush();
    

    Then on the server side, always use readObject, knowing that the first will be a command, the second will be a key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.