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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:58:45+00:00 2026-05-25T18:58:45+00:00

(I know this can be done in RMI, but I need to do this

  • 0

(I know this can be done in RMI, but I need to do this using sockets since I found there could be some setup process if RMI methods used)

Please have a look at the simple Client-Server code at http://www.coderanch.com/t/205325/sockets/java/send-any-java-Object-through

In this program, the order two objects sent by SimpleServer are known by SimpleClient.

i.e: Server

oos.writeObject(new testobject(1,"object from client"));
oos.writeObject(new String("another object from the client"));

Client does the casting according to the order the object is received.But I want to avoid this nature and make client send any object at any time so the server should also be able to handle each object sent accordingly and return a result.

testobject to = (testobject)ois.readObject(); 
System.out.println(to.id);}  
System.out.println((String)ois.readObject());

Is there a way to “label” the objects being sent so that the action can be determined by a simple “if” statement in Server?

OR

is there a better way to use a ResultSet returned by the Server instead of my object serializing approach?

thanks in advance.

Thanks

  • 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-25T18:58:46+00:00Added an answer on May 25, 2026 at 6:58 pm

    readObject will return an object in its proper class. if you need to have some branching logic based on that you can use instanceof:

    Object newObj = stream.readObject();
    if (newObj instanceof testobject) {
         doSomething((testobject) newObj);
    } else if (newObj instanceof String) {
         doSomethingWithString((String) newObj);
    } // etc.
    

    As a rule, this is not recommended for all objects read from a stream. If you’re going to use ObjectStreams to establish a protocol, you should document it and stick to it. That way, if one side sends incorrect data, you’ll catch it more quickly on the other side.

    However, in a scenario where at a given point in the protocol flow, it’s expected that the client might be sending one of several different types of objects, then it might make sense.

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

Sidebar

Related Questions

I know this can be done and i have seen it done using some
I know this can be done with mvc2, but is there any way for
I know this can be done easily using jQuery but haven't been able to
I know that this can be easily done by using if(i%5 == 0 OR
I know I've done this before years ago, but I can't remember the syntax,
I know this can be done but can't find the place to swap target
I know this can be done in many ways but im curious as to
I know this can be done as there are other modules out there that
I know this can be done with foreign keys but I cannot add them
I know this can be done in Ada but I'm new to Java and

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.