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 8011089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:55:38+00:00 2026-06-04T18:55:38+00:00

Im working on a Client/server chat application which allows user to send files (images

  • 0

Im working on a Client/server chat application which allows user to send files (images / videos…) through a socket connection.

In order to manage all kind of communication, I use an Object “Packet” which stores all information that I want to send. (Sender, receivers, file …).

Here is a code sample where I write in the stream :

private void write(Packet packet) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bos);
    os.writeObject(packet);
    this.outStream.write(bos.toByteArray());
}

And outStream is an OutputStream.

Here is my Connection run :

public void run() {
    while (isRunning()) {
        try {
            byte[] buffer = new byte[65536];
            // Read from the InputStream
            inStream.read(buffer);
            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer));
            Packet p = (Packet) in.readObject();

        } catch (IOException e) {
            e.printStackTrace();
            this.disconnect();
        }
    }
}

It works very well for all purpose except files transfer !
I put the file in a byte[] (with filestream) and store the array in my Packet Object.
When the server receive the communication it breaks on the “in.readObject()” and give me a pretty “java io streamcorruptedexception wrong format : 0” exception.

I tried the transfer with a custom byte[] (filled by a string.getBytes()) and it worked very well.

So, what am I doing wrong ?

  • 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-04T18:55:39+00:00Added an answer on June 4, 2026 at 6:55 pm

    You’re reading from the InputStream to a byte array (with an arbitrary size which could be too small). Then you construct an ObjectInputStream to read from this byte array. Why don’t you read your object directly from the InputStream?

    ObjectInputStream in = new ObjectInputStream(inStream);
    Packet p = (Packet) in.readObject();
    

    No need for a buffer.

    Moreover, InputStream.read() doesn’t read everything from the InputStream. It reads what is available, and returns the number of bytes read. If you don’t loop until it returns -1, you only read a part of what has been sent on the other side.

    BTW, you’re doing the same mistake on the sending side. Instead of writing your object directly to the output stream, you write it to a byte array, adn then send this byte array. Write your object directly to the stream:

    ObjectOutputStream os = new ObjectOutputStream(this.outputStream);
    os.writeObject(packet);
    

    No need for a buffer.

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

Sidebar

Related Questions

I am working on a mini-project which consists of doing a client-server chat application.
I'm working on the client side application of the client/server chat I'm doing for
I am working on Client Server application where I need to send 6 byte
I'm working on client/server application which uses AsyncSocket. For transferring data, it uses NSData
I'm working on chat application. The underlying server uses Node.js and the client/server communication
I am working an Client/Server application my client is Flash, and server is C#.
I'm working on a client-server Android application and trying to figure out how to
I'm working on a client-server application (.NET 4, WCF) that must support backwards compatibility.
I am working on a .net rich client server application, the client call the
I am currently running a Solr client/server pair which is working fine. However, in

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.