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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:28:02+00:00 2026-05-29T04:28:02+00:00

I am modifying a tutorial I found on how to create a TCP Chat

  • 0

I am modifying a tutorial I found on how to create a TCP Chat Server that accepts multiple clients. I will eventually create a client class as well, but so far I am testing it with TELNET.

I want the server to continually check for input so I can use key words to preform server functions. So string word “EXIT” to disconnect the client and the string word “Name:” to print “OK”.

This is what I was thinking but it doesn’t work:

 public void run()  
    {
         String line;
         try    
         {
            while(true)   
            {
                if (input.readline("EXIT"))//Should close and remove client
                {
                    clients.remove(this);
                    users.remove(name);
                    break;
                }
                if(input.readline("Name:"))//Should print OK with username
                {
                    System.out.println("OK");
                }
                boradcast(name,line); // method  of outer class - send messages to all
            }// end of while
         } // try
         catch(Exception e) 
         {
           System.out.println(e.getMessage());
         }
    } // end of run()
 }

}

Here is the whole server class

// Chat Server runs at port no. 9020
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;

public class  TCPServer 
{
  Vector<String> users = new Vector<String>();
  Vector<HandleClient> clients = new Vector<HandleClient>();

  int PORT = 9020;
  int NumClients = 10;

  public void process() throws Exception  
  {
      ServerSocket server = new ServerSocket(PORT,NumClients);
      out.println("Server Connected...");
      while( true) 
      {
         Socket client = server.accept();
         HandleClient c = new HandleClient(client);
         clients.add(c);
     }  // end of while
  }

  public static void main(String ... args) throws Exception 
  {
      new TCPServer().process();
  } // end of main

  public void boradcast(String user, String message)  
  {
        // send message to all connected users
        for (HandleClient c : clients)
           if (!c.getUserName().equals(user))
           {
              c.sendMessage(user,message);
           }
  }

  class HandleClient extends Thread 
  {
    String name = "";
    BufferedReader input;
    PrintWriter output;

    public HandleClient(Socket client) throws Exception 
    {
          // get input and output streams
         input = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
         output = new PrintWriter (client.getOutputStream(),true);
         output.println("Welcome to Bob's Chat Server!\n");
         // read name
         output.println("Please Enter a User Name: ");
         name  = input.readLine();
         users.add(name); // add to vector
         output.println("Welcome "+name+" we hope you enjoy your chat today");
         start();
    }

    public void sendMessage(String uname,String  msg)  
    {
        output.println( uname + ":" + msg);
    }

    public String getUserName() 
    {  
        return name; 
    }

    public void run()  
    {
         String line;
         try    
         {
            while(true)   
            {
                if (input.readline("EXIT"))
                {
                    clients.remove(this);
                    users.remove(name);
                    break;
                }
                if(input.readline(name))
                {
                    System.out.println("OK");
                }
                boradcast(name,line); // method  of outer class - send messages to all
            }// end of while
         } // try
         catch(Exception e) 
         {
           System.out.println(e.getMessage());
         }
    } // end of run()
  } // end of inner class
} // end of Server
  • 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-29T04:28:03+00:00Added an answer on May 29, 2026 at 4:28 am

    Without knowing exactly what you’re looking to do when an input line equals the current username, I think this is more what you’re looking for:

        public void run(){
            try{
                while(true){
                    String line = input.readLine();
    
                    if("EXIT".equals(line)){
                        clients.remove(this);
                        users.remove(name);
                        break;
                    }else if(name.equals(line)){
                        System.out.println("OK");
                    }
                    boradcast(name, line); // method  of outer class - send messages to all
                }// end of while
            } // try
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        } // end of run()
    

    This addresses a few issues:

    • input.readline is not a method, but input.readLine is – and it doesn’t accept any parameters. (This should have shown up as a compile error.)
    • You were never assigning anything to the line String.
    • You were reading the line multiple times. If it didn’t match “EXIT”, you read a new line to compare against name – loosing whatever the user had entered for the previous line.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am modifying a css3 nav that i found in a tutorial and I
I'm modifying a Castle-Monorail site that I've inherited and found that it would be
I am modifying some code and came across a declaration that I am having
We all know that modifying a .NET web application's web.config file restarts the app
I used the following tutorial to create a featured content slider for my homepage
Modifying working form with one spot per order to multiple spots per order I
I have followed a tutorial to create a simple blog writing application in PHP
I am modifying some code that has a lot of jQuery but I am
So I've been modifying the notepad tutorial code: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html . Basically what I want
I am following the LearnRubyTheHardWay tutorial and having a difficulty in modifying the exercise

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.