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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:47:25+00:00 2026-05-18T08:47:25+00:00

My problem is concerning JAVANIO client server message passing,i m unsure about defining the

  • 0

My problem is concerning JAVANIO client server message passing,i m unsure about defining the problem technically but:
it seems that buffer is caching the data and when it is done then it is sending all together which is disturbing logic:

private void sendCreate(String line,SocketChannel from)
 /* A new client wishes to join the world.

      This requires the client to find out about the existing
      clients, and to add itself to the other clients' worlds.

      Message format: create name xPosn zPosn

      Store the user's name, extracted from the "create" message
  */
 { StringTokenizer st = new StringTokenizer(line);
 st.nextToken();                  // skip 'create' word
 userName = st.nextToken();
 String xPosn = st.nextToken();   // don't parse
 String zPosn = st.nextToken();   // don't parse

 // request details from other clients
 sendBroadcastMessage( "wantDetails " + achannel.socket().getInetAddress() + " " + port,from);

 // tell other clients about the new one
 sendBroadcastMessage( "create " + userName + " "+xPosn+" "+zPosn,from);

 } // end of sendCreate()

method responsible for broadcasting messages from server:

private void sendBroadcastMessage(String mesg, SocketChannel from) {
  prepWriteBuffer(mesg);
  Iterator i = clients.iterator();
  while (i.hasNext()) {
   SocketChannel channel = (SocketChannel) i.next();
   if (channel != from)
    channelWrite(channel, writeBuffer);
  }
 }

i m assuming that this should send the first message i.e sendBroadcastMessage( “wantDetails ” + achannel.socket().getInetAddress() + ” ” + port,from); but this is not,it seems that it is waiting for other method call i.e sendBroadcastMessage( “create ” + userName + ” “+xPosn+” “+zPosn,from);and then sending both message as one message which is affecting application logic.ideally it should or it should send the first message after first call to sendBroadcastMessage and then when client recive the first then other call should be processed.

these are methods which are using in sendBroadcastMessage():

private void prepWriteBuffer(String mesg) {
  // fills the buffer from the given string
  // and prepares it for a channel write
  writeBuffer.clear();
  writeBuffer.put(mesg.getBytes());
  writeBuffer.putChar('\n');
  writeBuffer.flip();
 }

 private void channelWrite(SocketChannel channel, ByteBuffer writeBuffer) {
  long nbytes = 0;
  long toWrite = writeBuffer.remaining();

  // loop on the channel.write() call since it will not necessarily
  // write all bytes in one shot
  try {
    nbytes += channel.write(writeBuffer);

  } catch (ClosedChannelException cce) {
   cce.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
  // get ready for another write if needed
  writeBuffer.rewind();
 }

please suggest some solution.

thanks,

jibby lala

Edit :
what about this,i got this patch from some chat app:

private void prepWriteBuffer(String mesg) {
        // fills the buffer from the given string
        // and prepares it for a channel write
        writeBuffer.clear();
        writeBuffer.put(mesg.getBytes());
        writeBuffer.putChar('\n');
        writeBuffer.flip();
    }


// called needs to remove the channel if it fails, otherwise it will fail forever.
        private void channelWrite(SocketChannel channel, ByteBuffer writeBuffer)  {    
            long nbytes = 0;
            long toWrite = writeBuffer.remaining();
            // loop on the channel.write() call since it will not necessarily
            // write all bytes in one shot
            try {
            while (nbytes != toWrite) {
                nbytes += channel.write(writeBuffer);

                try {
                    Thread.sleep(CHANNEL_WRITE_SLEEP);
                } catch (InterruptedException e) {
                }
            }
        } catch (ClosedChannelException cce) {
        } catch (Exception e) {
        }
        // get ready for another write if needed
        writeBuffer.rewind();
    }
  • 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-18T08:47:25+00:00Added an answer on May 18, 2026 at 8:47 am

    perhaps you intended

     while(writeBuffer.remaining()>0)
          channel.write(writeBuffer);
    

    However, you problem appears to be that you assume there is some type of magic marker between messages. However, no such divider exists. A stream is just a stream of bytes. When you read in a blocking mode you will get atleast one byte, you may get more this may span what was multiple writes but unless you include in the stream where you expect a message to start and end, you will have no way of knowing.

    A simple approach is to write the length of the message at the start of the message and read at most single message until you get all of it. Something like.

    private void prepWriteBuffer(String mesg) {    
      // fills the buffer from the given string    
      // and prepares it for a channel write    
      writeBuffer.clear();
      byte[] bytes = mesg.getBytes());
      writeBuffer.putInt(bytes.length);    
      writeBuffer.put(bytes);
      writeBuffer.flip();    
     } 
    
    
    // called needs to remove the channel if it fails, otherwise it will fail forever.
    private void channelWrite(SocketChannel channel, ByteBuffer writeBuffer) throws IOException {    
     while(writeBuffer.remaining()>0)
          channel.write(writeBuffer);
     writeBuffer.rewind();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem concerning good application design that has been keeping me up
I am currently facing a problem that is concerning the google search, curl and
I have quite a problem concerning the use of relational database concepts in Delphi
I've got a problem concerning combinatorics. Unfortunately, I can't describe it abstractly so I
I've got a problem concerning the javascript this keyword when used within a javascript
I have problem concerning python packages and testing. I'm writing an application using wx
Today we came accross a problem concerning static member functions in an multithreaded environment.
I am using LaTeX and I have a problem concerning string manipulation. I want
I came across a problem I cannot solve on my own concerning the downloadable
I have a problem concerning folder structure of a built InstallShield suite project. 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.