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

  • Home
  • SEARCH
  • 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 7399083
In Process

The Archive Base Latest Questions

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

I have written a chat server that works with telnet. I wrote a client

  • 0

I have written a chat server that works with telnet. I wrote a client to connect to the server. In the terminal, I run the client by giving it the name of the file and the ip address and port. It appears to connect because it closes the server and the following error occurs:

  Connection reset
    Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:130)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:153)
        at java.io.BufferedReader.readLine(BufferedReader.java:316)
        at java.io.BufferedReader.readLine(BufferedReader.java:379)
        at ChatServer$HandleClient.<init>(ChatServer.java:47)
        at ChatServer.process(ChatServer.java:18)
        at ChatServer.main(ChatServer.java:23)

My chat server code is:

 import java.io.*;
    import java.util.*;
    import java.net.*;
    import static java.lang.System.out;

    public class  ChatServer {
      ArrayList<String> users = new ArrayList<String>();
      ArrayList<String> buffer = new ArrayList<String>();
      ArrayList<HandleClient> clients = new ArrayList<HandleClient>();
      Socket client;

      public void process() throws Exception  {
          ServerSocket server = new ServerSocket(9020,10);
          out.println("Server Started...");
          while( true) {
             client = server.accept();
             HandleClient c = new HandleClient(client);
             clients.add(c);
         }  // end of while
      }
      public static void main(String ... args) throws Exception {
          new ChatServer().process();
      } // end of main

      public void bc(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
         start();
         input = new BufferedReader( new InputStreamReader( client.getInputStream())) ;
         output = new PrintWriter ( client.getOutputStream(),true);
         output.println("Welcome to Kimberly's Chat room");
         // receives the name of the client
         output.println("Enter your name: ");
         // read name of the client
         name  = input.readLine();
         users.add(name); // adds the name of the client to the vector
         output.println("Thanks for joining " + name + ". If you need help, type \"help\" for a list of commands.");
         //start();
            }

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

            public String getUserName() {
                return name;
            }

        public String toString() {
                return name;
            }

            public void run()  {
                 String line;
             try    {
                   while(true)   {
                line = input.readLine();
                String[] temp;
                temp = line.split(":");
                    //checks different input from the client
                //checks to see if the client wants to terminate their connection
                //removes the client's name from the list
                if ("adios".equals(line)){
                    output.println("Server closing connection...");
                    clients.remove(this);
                    users.remove(name);
                    break;
                        }
                //checks to see if the client typed in help to receive a list of the commands
                else if("help".equals(line)){
                    output.println("Here is a list of user commands:");
                    output.println("adios: exit");
                    output.println("help: lists the commands and their syntax");
                    output.println("get: receives a response of the entire chat buffer");
                    output.println("name: receives a response of \"OK\" and adds the name to a list");
                }
                else if("getNames".equals(line)){
                    output.println(users.toString());
                }
                else if("name".equals(temp[0])){
                    users.add(temp[1]);
                    output.println("OK");
                }
                else if("push".equals(temp[0])){
                    buffer.add(name + ":" + temp[1]);
                    output.println("OK");
                }
                else if("get".equals(line)){
                    output.println(buffer.toString());
                }
                else if("test".equals(temp[0])){
                    output.println(temp[1].toString());
                }
                else{
                    bc(name,line); // method  of outer class - send messages to all
                }
               } // end of while
             } // try
             catch(Exception e) {
               System.out.println(e.getMessage());
             }
             try{
            client.close();
             }
             catch(Exception e) {
               System.out.println(e.getMessage());
             }
            } // end of run()
       } // end of inner class
}

My client code is:

public class  ChatClient {
    PrintWriter output;
    BufferedReader input;
    Socket client;

    public ChatClient(String ip, int port) throws Exception {
    client = new Socket(ip,port);
    input = new BufferedReader( new InputStreamReader( client.getInputStream()) ) ;
        output = new PrintWriter(client.getOutputStream(),true);

    }

    public static void main(String args[]) {
        try {
        String ip= args[0];
        int port= Integer.parseInt(args[1]);
            new ChatClient(ip,port);
        } catch(Exception ex) {
            out.println( "Error --> " + ex.getMessage());
        }

    } // end of main
  • 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:00:43+00:00Added an answer on May 29, 2026 at 4:00 am

    add

    input.readLine();
    

    to your ChatClient class after you send data to server

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

Sidebar

Related Questions

I have existing chat server code written in C/C++ that communicates over TCP/IP with
I have written a UDP client server chat, I am able to communicate to
I have written a small chat app that uses mysql + php to facilitate
I have written an AIR Application that downloads videos and documents from a server.
I have written a server and a client both as separate apps. They communicate
I have written an app that reads incoming chat(somewhat like an instant messenger), formats
I have a pretty intensive chat socket server written in Twisted Python, I start
I am making a simple chat GUI and I have written both a server
I have a server written in C++, and when receiving a chat string, I'd
I have written a packet reader that uses libpcap to read a capture file.

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.