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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:30:55+00:00 2026-06-01T21:30:55+00:00

I have a client server application where I am using java serversocket(server side) and

  • 0

I have a client server application where I am using java serversocket(server side) and sockets (client side) with the java.nio selector facility. I am exchanging messages between the client and server using serialized objects via ObjectInput/Output Streams.

Here is the scala code for receiving the message from server which is fairly standard

enter code here

var msg:CommunicationMessage = null
    var buf:ByteBuffer  = ByteBuffer.allocate(1024);
    var numBytesRead:Int = 0

    try {
        // Clear the buffer and read bytes from socket
        buf.clear();
        //println("Before Read")



        numBytesRead = sChannel.read(buf)


        //println("After Read  " + numBytesRead)


        numBytesRead match {

          case -1 => 
            println("Something Wrong with the Channel")

          case 0 => 
            //println("Nothing to read")

          case _ =>
            //println("Read some bytes")
            // To read the bytes, flip the buffer
            buf.flip();

            // Read the bytes from the buffer ...;
            // see Getting Bytes from a ByteBuffer
            val bis:ByteArrayInputStream = new ByteArrayInputStream (buf.array());
            val ois:ObjectInputStream = new ObjectInputStream (bis);
            msg = ois.readObject().asInstanceOf[CommunicationMessage];

            println("\n\n")
            println("Received Message from Server")

            msg.msgType match {

                case CommunicationConstants.COMM_MSG_TYPE_RSP =>
                    println("updating server info")
                    updateServerInfo(msg.msgData)

                case CommunicationConstants.COMM_MSG_TYPE_IND =>
                    if (serverAccepted) updateClientUI(msg)

            }

        }

    } catch  {

      case e:StreamCorruptedException =>
        println("Stream Corruption Exception received " + e.getCause())

      case e:IOException => 
        // Connection may have been closed
        println("IO Exception received" + e.getCause())
    }
enter code here

Everything works fine except for situations when the server writes multiple messages within a very short span of time and the client receives them together in the buffer and causes StreamCorruptedException. As I understand , the objectinputstream cannot delimit between multiple serialized objects in the incoming byte stream hence it throws this invalid stream error. Also even I have put the exception for catching it , the program still hangs.

What could be a solution for this. ?

1) Creating my own delimiters in the message based on some length field to identify the message boundary.

2) I also read somewhere to close the socket after every receive and start a new one. Not sure how will this help.

Plz suggest.

Thanks in advance

  • 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-01T21:30:59+00:00Added an answer on June 1, 2026 at 9:30 pm

    You need to send the length of the packetized ObjectOutputStream ahead of its contents. Then you know exacly how much incoming data belongs to each stream and you can construct your ByteArrayInputStream accordingly.

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

Sidebar

Related Questions

I'm using Java sockets for client - server application. I have a situation when
I am writing a client-server application using Java-RMI. Some server-side ressources have to be
we have a java server-client application with an applet in the client side. our
I have a client-server application written in Java using CORBA for the communication. The
I have been developed a web application using java servlet at server side and
I have a client Server application which communicate using objects. when I send only
I have a project to develop a client server application in java . This
I have an java application using Socket TCP/IP and GUI. Server always listens connection
I have client server application - client on C++ (running on AIX,using library ),
I have a client-server java application, where communication happens over SSL. Right now I

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.