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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:22:12+00:00 2026-05-28T20:22:12+00:00

I am working on a simple tcp chat server in java. The user can

  • 0

I am working on a simple tcp chat server in java. The user can type in commands like “push:”, “get”, “adios”,”help” etc . . . I check for these user commands by setting String Line = input.readLine() (shown below). The problem is when I type “push: ” I must go to a new line to get new input and assign it to a new string. I want to be able to get input on the same line after the user types the command.

For example: if USER 1 types in “push: ” + “Hello, how are you today?” I want to store “Hello, how are you today?” to an array so I can print its contents later. But as it is now I must type “push: ” go to a new line and type “Hello, how are you today?” to store it to the array. That’s just awkward. Is there any way to store the input to two different strings on the same line?

Here is my server code, check the run method

// 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>();
  Vector<String> Chat = new Vector<String>();

  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)  
  {
        System.out.println(user + ": " + 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 Alan's Chat Server!\n");
         // read name
         output.println("Please Enter a User Name: ");
         name  = input.readLine();
         users.add(name); // add to vector
         output.println("\nWelcome "+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)   
            {
                        line = input.readLine();
            line.toLowerCase();//check this
                if("adios".equals(line))
            {
                output.println("\nClosing Connection  . . . Goodbye");
                            clients.remove(this);
                        users.remove(name);
                break;
                    }
            else if(name.equals(line))
            {
                            output.println("OK");
                    }
            else if("help".equals(line))
            {
                output.println("\nServer Commands:" + "\n\"audios:\" close the client connection\n"+
                                               "\"Name:\" display \"OK\"\n");//Check this
            }
            else if("push: ".equals(line))
            {
                String NewLine = input.readLine();
                Chat.add(NewLine);
                output.println("OK");
            }
            else if("get".equals(line))
            {
                output.println(Chat.toString());

            }
            else
            {
                    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-28T20:22:13+00:00Added an answer on May 28, 2026 at 8:22 pm

    Possible with String.split("") or using the StringTokenizer Class

    String line = input.read();
    String [] d = s.split(":",2);
    

    access the values via the array indices;

    Edit:

    For clarity I’m passing 2 as the second argument in the split method, because, it limits the split a array to just two indices so you don’t have 3 arrays for something like this:

    String.split("PUSH: I am hungry :P"); as it would output [PUSH; I am hungry; P]

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

Sidebar

Related Questions

I have been working on a (relatively) simple tcp client/server chat program for my
new user to the site here. I'm working on a simple asynchronous tcp server.
I'm working on a simple hello world TCP/IP client server app in C# and
I'm working on a simple pair of TCP server and client, which are running
I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display
i am working on a simple web app which has a user model and
I'm working on a simple 2D game engine in Java, and having no trouble
A part of an application I'm working on is a simple pthread-based server that
I've set up a simple, single-service server which has been working just fine with
Right now I've got a simple TCP server/client. I have it set up so

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.