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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:20:47+00:00 2026-06-13T23:20:47+00:00

I’m currently making a client-server application which primarily is built to make procedure call

  • 0

I’m currently making a client-server application which primarily is built to make procedure call to a server and wait for a packet response.

however i would like to make the server ‘push’ news out to the client (at a spontaneous time), but that seems to be a problem if the message arrives when the client listens for another packet response.

Is there a way to split a socket into 2 unique channel streams, through the same socket instance? Or would it be better to make a client thread which only listens for incoming connections and dispatches them to a queue?

What are some common patterns and practices when working with 2-way client-server connection? (Where streams occur spontaneous)

For reminders i’m using Java Socket, ObjectInputStream and ObjectOutputstream. i’m not using Java RMI.

  • 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-13T23:20:48+00:00Added an answer on June 13, 2026 at 11:20 pm

    So one of the things you’ll come up against when learning about networking is loss of synchronization. This happens when you ‘hard-code’ a certain set of client-server interactions, for example;

    server sends 2 byte status code
    client receives 2 byte status code
    client responds with 4 byte operation code
    

    If for some reason there is a bug that causes any part of this interaction to not happen precisely as it needs to, then the rest of the program fails because all network interaction is now out of sync. The client might read a set of bytes that it believes represents a string, when really the server is sending an int, and so on. Worst of all you might find that your main network threads deadlock because both client and server are waiting for input at the same time.

    With a larger project, where there are certainly bugs, this will happen an awful lot if you code in this style. For this reason, we have something called middleware.

    A very common and flexible middleware paradigm is the TLV Message Protocol. You would implement some simple classes (in semi-java-pseudocode);

    TLVMessage
        int type;
        byte[] value;
    

    TLVPusher implements Runnable
        OutputStream out;
        Queue<TLVMessage> messages;
        run() {
            while(true) {
                //poll and write front of queue to out (INCLUDING value.length!)
            }
        }
    

    TLVReader implements Runnable
        InputStream in;
        Queue<TLVMessage> messages;
        run() {
            while(true) {
                //read message from in and add to queue
            }
        }
    

    Now you have two threads running on the client, and two threads running on the server. Each end has its own Pusher and Reader. The important thing to note is that because you write the length field to the output stream, the reader always knows how many bytes it needs to read. So even if one message has been serialised incorrectly, its length is still correct and the next message will always be read correctly from the first byte to the last. This way your program can never go out of sync.

    You simply add TLVMessage objects to the pusher.queue, and they will arrive at the reader.queue on the other end of the socket. You can then handle the messages (in another, third, thread which watches reader.messages.size()) by their type field.

    You do not need to worry about what order things happen in, you have a robust mechanism for passing messages between client and server, in two directions. You have abstracted away the fiddly network stuff, and can just get on with coding.

    There are, of course, libraries that do all this for you, but it’s always worth understanding the how and the why in my opinion.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.