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

  • Home
  • SEARCH
  • 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 7515631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:52:15+00:00 2026-05-30T00:52:15+00:00

I am trying to send a serialized Java object from a client to a

  • 0

I am trying to send a serialized Java object from a client to a server via socket, but there is a cast error on the server side and I can’t figure out why.
The Class being serialized is below. (I am using Eclipse, and this class is in both the client
package and the server package.)

package client; //it is "package server;" on server side

import java.io.Serializable;

public class Simple implements Serializable {
    public String msg;
    public Simple(){
        msg = "simple";
    }
}

on client side, I did:

Simple obj = new Simple();
Socket socket = new Socket(hostname, portNum);
ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
objOut.writeObject(obj);
objOut.flush();
objOut.close();

on the server side, I did:

//already accepted clientSocket
ObjectInputStream objIn = new ObjectInputStream(clientSocket.getInputStream());
Simple anObj = (Simple) objIn.readObject();
System.out.println("from server: " + anObj.msg);
objIn.close();
clientSocket.close();

However, I got the error:

Exception in thread "main" java.lang.ClassCastException: client.Simple cannot be cast to server.Simple at server.Server.main

I tried something similar with the Server and Client in a single package, and both the serialization and inflation worked. I would really appreciate it if you can help me find out why it doesn’t work here.

  • 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-05-30T00:52:18+00:00Added an answer on May 30, 2026 at 12:52 am

    Simply put; to avoid two types in different namespaces from colliding, the package name is a part of the type name.

    What happens is that the client serializes a client.Simple, which the server also deserializes as a client.Simple. Then you proceed by casting it to a server.Simple, which is an entirely different type.

    It can be shown by doing;

    ObjectInputStream objIn = new ObjectInputStream(clientSocket.getInputStream());
    Object anObj = objIn.readObject();
    System.out.println(anObj);               // Output "client.Simple@39617189"
    obj = (server.Simple)anObj;              // Throws ClassCastException
    

    The code shows that the server knows that the type of the deserialized object is client.Simple, and it simply can’t cast it to the separate type server.Simple.

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

Sidebar

Related Questions

I am trying to send a serialized object from a server process to a
I'm trying to send messages (byte arrays) from Node.js to Java via TCP socket
I have an applet trying to send java serialized object to a servlet (hosted
I'm trying to send an email in Java but when I read the body
I am trying to send an email from a site I am building, but
I am trying to send an anonymous object over a web service. Is there
I am trying to send a byte[] from a client to a web service.
I'm trying to send a string via ftp using the code below but I'm
I trying to write a client server application in Python, but I faced a
I'm trying to find the best way to transfer data from server -> client

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.