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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:50:59+00:00 2026-06-18T12:50:59+00:00

been trying to figure this problem out for about 5 hours but cant seem

  • 0

been trying to figure this problem out for about 5 hours but cant seem to see it, although all the steps are done to send data, I can only receive messages to the server, but not from server to client. I’m in the early stages of building/learning how to do a chat client program in command line. The following is the server code:

The CServer class:

public class CServer {

private static int port=2008, maxConnections=0;
private static String shutDownServer = "no";

public static void main(String[] args) throws IOException{


    ServerSocket listen = new ServerSocket(port);
    Socket server;

    while(shutDownServer.equalsIgnoreCase("no")){

        doComm connection;

        System.out.println("\nWaiting for clients to connect...");

        server = listen.accept(); // accept incomming connections from client
        System.out.println("Client connected. Location: " + server.getInetAddress().getHostName());
        connection = new doComm(server);

        Thread thread = new Thread(connection);
        thread.start();
    }



}

public void shutDownServer(String command){

    this.shutDownServer = command;
}
}

Now the doComm class that handles each client in thread:

public class doComm implements Runnable{

Socket server;
private String clientData;

  public doComm(Socket server){

      this.server = server;
  } 

  public void run(){

     try {

      BufferedReader fromClient = new BufferedReader(new InputStreamReader(server.getInputStream()));
      DataOutputStream toClient = new DataOutputStream(server.getOutputStream());

      clientData = fromClient.readLine();
         System.out.println("Client sent: "+clientData);

(( The problem -imo- may be either this statement: ))

      toClient.writeBytes("Recieved your sentence '"+clientData+"' and more to come :)!");

    //server.close();

  } catch (IOException e) {
    System.out.println("IOException on socket listen: " + e);
    e.printStackTrace();
  }

  }
}

Now the client class CClient:

    public class CClient {

    static String address = "localhost";

static int port = 4444;
static Socket echoSocket;

     public CClient(int port, String addr){

         changePort(port);
         changeAddr(addr);
     }

     public static void main(String[] args) throws IOException, UnknownHostException{

         Scanner scan = new Scanner(System.in);

         System.out.println("Please enter the port to connect to: ");
         int temp_port = Integer.parseInt(scan.nextLine());
         System.out.println("Please enter the address of server: ");
         System.out.flush();
         String temp_addr = scan.nextLine();

         CClient client = new CClient(temp_port,temp_addr);

            PrintWriter out = null;
            BufferedReader in = null;

         try{
             System.out.flush();
             echoSocket =  new Socket(address,port);
             out = new PrintWriter(echoSocket.getOutputStream(), true);
             in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
         }
         catch(IOException e){

             System.err.println("IOException error: " + e.getStackTrace());
         }

         BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String userInput;

    while ((userInput = stdIn.readLine()) != null) {

        out.println(userInput);
            System.out.println("thingy prints right after this...");

(( or here: ))

System.out.println("echo: " + in.readLine());
}
 }

 public void changePort(int port){

     this.port=port;
 }

 public void changeAddr(String addr){

     this.address=addr;
 }
}
  • 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-18T12:51:01+00:00Added an answer on June 18, 2026 at 12:51 pm
      clientData = fromClient.readLine();
    
      toClient.writeBytes("Recieved your sentence '"+clientData+"' and more to come :)!");
    

    This is a very common problem whose root cause is the failure to document and specify the protocol being used for communication. Here you are receiving lines but sending bytes. If you had a protocol document, it would either specify that lines were exchanged or that arbitrary units of bytes were exchanged. That would show that one of these lines of code is wrong, and you could fix it. But without a protocol specification, you can’t even tell which side is wrong.

    Please, take my advice from years of painful lessons — document a protocol before you implement. Then, if it doesn’t work, you can follow this three step process:

    1. Does the client follow the documented protocol? If not, it is broken.

    2. Does the server follow the documented protocol? If not, it is broken.

    3. The protocol specification is broken.

    In this case, the protocol specification would document what constitutes a “message” for your protocol. It would then be each side’s responsibility to send complete messages and find these message boundaries on receive. However, in your case, one piece of code expects a line terminator to mark a message boundary and the other side doesn’t send one.

    Is the sender wrong to omit a message boundary? Is the receiver wrong to insist on receiving one? Nobody knows because there’s no specification to say what’s the right thing to do.

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

Sidebar

Related Questions

I have been trying to figure out a problem for about 3 hours now
I've been trying to figure this out all night, but I guess my knowledge
I've been pulling out my hair trying to figure this problem out. I have
I have been trying to figure out why I am getting this problem and
I've been trying to solve this problem for hours now and i cannot figure
Been trying to figure this out, but can't come up with a good solution
I've been trying to figure this out now for a few hours and its
I've been trying to figure this out for hours, so thought I should ask
I have been trying to figure this out for quite a while, but what
I've been trying to figure this issue out for about an hour now. I've

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.