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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:48:15+00:00 2026-05-27T05:48:15+00:00

I’m learning how to implement a multi-user chat server using TCP in Java. I

  • 0

I’m learning how to implement a multi-user chat server using TCP in Java. I came across an example of on this on the web, but the something seems to be wrong in the ChatClient.java file. Ideally once the client connects to the server, the client is supposed to provide a nick name and if this nick name is valid… the server returns an “OK” message and the chat session can commence.

However, this doesn’t seem to work. Once I enter a nick name I don’t get an “OK” message back from the server and consequently can’t chat using the client.

The link for the original tutorial website is here (it’s the last example): http://pguides.net/java/tcp-client-server-chat

I tried searching the forum thread for this article… but it seems dead. I’d really appreciate it if someone could explain to me why it won’t work.

ChatServer.java:

/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.util.Hashtable;

public class ChatServer {
private static int port = 1001; /* port to listen on */

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

    ServerSocket server = null;
    try {
        server = new ServerSocket(port); /* start listening on the port */
    } catch (IOException e) {
        System.err.println("Could not listen on port: " + port);
        System.err.println(e);
        System.exit(1);
    }

    Socket client = null;
    while(true) {
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.err.println(e);
            System.exit(1);
        }
        /* start a new thread to handle this client */
        Thread t = new Thread(new ClientConn(client));
        t.start();
    }
   }
}

class ChatServerProtocol {
private String nick;
private ClientConn conn;

/* a hash table from user nicks to the corresponding connections */
private static Hashtable<String, ClientConn> nicks = 
    new Hashtable<String, ClientConn>();

private static final String msg_OK = "OK";
private static final String msg_NICK_IN_USE = "NICK IN USE";
private static final String msg_SPECIFY_NICK = "SPECIFY NICK";
private static final String msg_INVALID = "INVALID COMMAND";
private static final String msg_SEND_FAILED = "FAILED TO SEND";

/**
 * Adds a nick to the hash table 
 * returns false if the nick is already in the table, true otherwise
 */
private static boolean add_nick(String nick, ClientConn c) {
    if (nicks.containsKey(nick)) {
        return false;
    } else {
        nicks.put(nick, c);
        return true;
    }
}

public ChatServerProtocol(ClientConn c) {
    nick = null;
    conn = c;
}

private void log(String msg) {
    System.err.println(msg);
}

public boolean isAuthenticated() {
    return ! (nick == null);
}

/**
 * Implements the authentication protocol.
 * This consists of checking that the message starts with the NICK command
 * and that the nick following it is not already in use.
 * returns: 
 *  msg_OK if authenticated
 *  msg_NICK_IN_USE if the specified nick is already in use
 *  msg_SPECIFY_NICK if the message does not start with the NICK command 
 */
private String authenticate(String msg) {
    if(msg.startsWith("NICK")) {
        String tryNick = msg.substring(5);
        if(add_nick(tryNick, this.conn)) {
            log("Nick " + tryNick + " joined.");
            this.nick = tryNick;
            return msg_OK;
        } else {
            return msg_NICK_IN_USE;
        }
    } else {
        return msg_SPECIFY_NICK;
    }
}

/**
 * Send a message to another user.
 * @recepient contains the recepient's nick
 * @msg contains the message to send
 * return true if the nick is registered in the hash, false otherwise
 */
private boolean sendMsg(String recipient, String msg) {
    if (nicks.containsKey(recipient)) {
        ClientConn c = nicks.get(recipient);
        c.sendMsg(nick + ": " + msg);
        return true;
    } else {
        return false;
    }
}

/**
 * Process a message coming from the client
 */
public String process(String msg) {
    if (!isAuthenticated()) 
        return authenticate(msg);

    String[] msg_parts = msg.split(" ", 3);
    String msg_type = msg_parts[0];

    if(msg_type.equals("MSG")) {
        if(msg_parts.length < 3) return msg_INVALID;
        if(sendMsg(msg_parts[1], msg_parts[2])) return msg_OK;
        else return msg_SEND_FAILED;
    } else {
        return msg_INVALID;
    }
 }
}

class ClientConn implements Runnable {
private Socket client;
private BufferedReader in = null;
private PrintWriter out = null;

ClientConn(Socket client) {
    this.client = client;
    try {
        /* obtain an input stream to this client ... */
        in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
        /* ... and an output stream to the same client */
        out = new PrintWriter(client.getOutputStream(), true);
    } catch (IOException e) {
        System.err.println(e);
        return;
    }
}

public void run() {
    String msg, response;
    ChatServerProtocol protocol = new ChatServerProtocol(this);
    try {
        /* loop reading lines from the client which are processed 
         * according to our protocol and the resulting response is 
         * sent back to the client */
        while ((msg = in.readLine()) != null) {
            response = protocol.process(msg);
            out.println("SERVER: " + response);
        }
    } catch (IOException e) {
        System.err.println(e);
    }
}

public void sendMsg(String msg) {
    out.println(msg);
 }
}

ChatClient.java:

/* ChatClient.java */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;

import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient {
private static int port = 1001; /* port to connect to */
private static String host = "localhost"; /* host to connect to */

private static BufferedReader stdIn;

private static String nick;

/**
 * Read in a nickname from stdin and attempt to authenticate with the 
 * server by sending a NICK command to @out. If the response from @in
 * is not equal to "OK" go bacl and read a nickname again
 */
private static String getNick(BufferedReader in, 
                              PrintWriter out) throws IOException {
    System.out.print("Enter your nick: ");
    String msg = stdIn.readLine();
    out.println("NICK " + msg);
    String serverResponse = in.readLine();
    if ("SERVER: OK".equals(serverResponse)) return msg;
    System.out.println(serverResponse);
    return getNick(in, out);
}

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

    Socket server = null;

    try {
        server = new Socket(host, port);
    } catch (UnknownHostException e) {
        System.err.println(e);
        System.exit(1);
    }

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

    /* obtain an output stream to the server... */
    PrintWriter out = new PrintWriter(server.getOutputStream(), true);
    /* ... and an input stream */
    BufferedReader in = new BufferedReader(new InputStreamReader(
                server.getInputStream()));

    nick = getNick(in, out);

    /* create a thread to asyncronously read messages from the server */
    ServerConn sc = new ServerConn(server);
    Thread t = new Thread(sc);
    t.start();

    String msg;
    /* loop reading messages from stdin and sending them to the server */
    while ((msg = stdIn.readLine()) != null) {
        out.println(msg);
    }
  }
}

class ServerConn implements Runnable {
private BufferedReader in = null;

public ServerConn(Socket server) throws IOException {
    /* obtain an input stream from the server */
    in = new BufferedReader(new InputStreamReader(
                server.getInputStream()));
}

public void run() {
    String msg;
    try {
        /* loop reading messages from the server and show them 
         * on stdout */
        while ((msg = in.readLine()) != null) {
            System.out.println(msg);
        }
    } catch (IOException e) {
        System.err.println(e);
    }
  }
}

Edit

From comment

This is what happens when I run the code:

client side:

Enter your nick: learnHK
hello
SERVER: INVALID COMMAND
not working?
SERVER: INVALID COMMAND

and on the server side:

Nick learnHK joined.

And that’s about it. The client isn’t getting the “OK” response message from the server and so can’t commence the chat. Thanks.

  • 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-27T05:48:16+00:00Added an answer on May 27, 2026 at 5:48 am

    Your problem is that this is not a simple chat server where you just write whatever you want and it’s sent back. The server wants specific commands.

    Here is an example session I just had:

    Enter your nick: arrow
    hello
    SERVER: INVALID COMMAND
    MSG arrow hello
    arrow: hello
    SERVER: OK
    

    As you can see, when I sent just the string “hello” I get an error back. But when I use “MSG arrow hello” it is parsed as a proper command by the server.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.