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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:01:27+00:00 2026-06-12T21:01:27+00:00

I have just started learning java. I modified the client side code for a

  • 0

I have just started learning java. I modified the client side code for a server/client communication program, by creating two threads for the client side, main thread for receiving user’s input, and inputThread for receiving server’s response. I am sure that server has sent the response to client, however, no response message is obtain at client.

Here is my code. Can anyone help me to figure it out? Thanks

package clientnio;

import java.net.*; 
import java.nio.*; 
import java.io.*;
import java.nio.channels.*; 
import java.util.Scanner; 

public class ClientNIO {
public static int bufferLen = 50;
public static SocketChannel client;
public static ByteBuffer writeBuffer;
public static ByteBuffer readBuffer;

public static void main(String[] args) { 
    writeBuffer = ByteBuffer.allocate(bufferLen);
    readBuffer = ByteBuffer.allocate(bufferLen);

    try { 
        SocketAddress address = new InetSocketAddress("localhost",5505); 
        System.out.println("Local address: "+ address);
        client=SocketChannel.open(address);
        client.configureBlocking(false); 

        //readBuffer.flip();
        new inputThread(readBuffer);

        /*
        String a="asdasdasdasddffasfas"; 
        writeBuffer.put(a.getBytes()); 
        writeBuffer.clear();  
        int d=client.write(writeBuffer);
        writeBuffer.flip();
        */

        while (true) {
            InputStream inStream = System.in;
            Scanner scan = new Scanner(inStream);
            if (scan.hasNext()==true) {
                String inputLine = scan.nextLine();
                writeBuffer.put(inputLine.getBytes()); 
                //writeBuffer.clear();
                System.out.println(writeBuffer.remaining());
                client.write(writeBuffer);
                System.out.println("Sending data: "+new String(writeBuffer.array()));
                writeBuffer.flip(); 
                Thread.sleep(300);
            }
        }                 
    } 
    catch(Exception e) { 
        System.out.println(e);
    } 

}
}

class inputThread extends Thread {
private ByteBuffer readBuffer;
public inputThread(ByteBuffer readBuffer1) {  
    System.out.println("Receiving thread starts.");
    this.readBuffer = readBuffer1;
    start();
}

@Override
public void run() {
    try {
        while (true) {
            readBuffer.flip();
            int i=ClientNIO.client.read(readBuffer); 
            if(i>0) { 
                byte[] b=readBuffer.array(); 
                System.out.println("Receiving data: "+new String(b)); 
                    //client.close(); 
                    //System.out.println("Connection closed."); 
                    //break; 
            }
            Thread.sleep(100); 
        }
    }
    catch (Exception e) {
       System.out.println(e);
    }
  }
}
  • 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-12T21:01:29+00:00Added an answer on June 12, 2026 at 9:01 pm

    Disclaimer: I’m not an active user of Java. (I only used it in school.)


    Advice: I think it will greatly simplify the debugging process if you use blocking mode, at least until your code example is working correctly. (Currently your code does not seem to benefit from the non-blocking mode.)


    I have identified two issues, culminating into four possible lines of code that may require changing:

    1. When a ByteBuffer allocates its backing array, it sets itself ready to write by setting position to zero and limit to the capacity of that array. Your two uses of ByteBuffer.flip() (in the writing loop and the reading loop respectively) seem to be contrary to the convention.
    2. Calling the ByteBuffer.array() method always returns the whole backing array, thus it always has size bufferLen. Because of this, a String constructed from the full-size array may contain junk from a previous transmission.
      • Typically, the array needs to be trimmed to the transmission size, and the conversion between a String and a byte array must use the same encoding as the server.

    My suggested changes for first issue:
    (Note: I don’t know how to fix the array trimming and encoding issue.)

    writeBuffer.put(inputLine.getBytes()); 
    writeBuffer.flip();                       // <--here
    client.write(writeBuffer);
    ...
    writeBuffer.clear();                      // <-- should be clear() instead of flip()
    Thread.sleep(300);
    

    // readBuffer.flip();                      // <-- remove this line
    int i=ClientNIO.client.read(readBuffer); 
    if(i>0) { 
        readBuffer.flip();                     // <-- move it here
        byte[] b=readBuffer.array(); 
        System.out.println("Receiving data: "+new String(b)); 
        ...
    }
    

    References

    • http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html
    • http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/channels/SocketChannel.html
    • Socketchannel always null
    • http://www.exampledepot.com/egs/java.nio.charset/ConvertChar.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have just started learning Android, this is the code of my java file.
I have just started learning about Java server faces and successfully tested out my
I have just started learning java, and know only a small amount of code,
First of all, this is my code (just started learning java): Queue<String> qe =
G'Day Programmers, I am from Java background however I have just started learning C++
I have just started learning java and i have about 8 months time. My
I am just starting out learning Java, GAE etc. I have started a project
I have just started learning java. I downloaded eclipse for linux and had an
I've just started learning Java and have come across a little quirk I'm not
I just started learning Java, and I have no idea how it works. The

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.