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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:47:49+00:00 2026-05-24T19:47:49+00:00

I have implemented a simple chat program in Java . However when I run

  • 0

I have implemented a simple chat program in Java . However when I run the code and try to send messages from client I get this as output on Server side

For example:

Client : hi

Server: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=2000]

I get such a response from the client for any message I send..I am basically working on localhost

Can anyone help solve my problem?

My java code :

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

           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           System.out.println("enter the key value");
           int key=Integer.parseInt(br.readLine());
           int random=(int)(Math.random()*50);
           System.out.println(random);
           int response=((int)random)%(key);
           System.out.println(key);
           System.out.println("response generated is "+response);
           System.out.println("Authentication begins");
           Socket echoSocket = new Socket("127.0.0.1", 2500);
           BufferedReader sin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
           PrintStream sout=new PrintStream(echoSocket.getOutputStream());
           BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
           PrintWriter out = null;
           String s;
           DataOutputStream clientout=new DataOutputStream(echoSocket.getOutputStream());
           clientout.writeInt(random);
           clientout.writeInt(key);
          clientout.writeInt(response);
          System.out.println("client is"+response);
          System.out.println("chat is started");

        while (true)
    {
        System.out.print("Client : ");
        s=stdin.readLine();
        sout.println(s);
        s=sin.readLine();
        System.out.print("Server : "+s+"\n");
        if ( s.equalsIgnoreCase("BYE") )
           break;
    }

            echoSocket.close();
            sin.close();
            sout.close();
            stdin.close();
         }
    }

          class Server
         {
            public static void main(String args[]) throws IOException
              {
              int random3=(int)(Math.random()*50);
              int response2;
              int response3;
              int random2;
              int key2;
              ServerSocket s= new ServerSocket(2500);
              Socket echoSocket=s.accept();
              DataInputStream clientin=new     DataInputStream(echoSocket.getInputStream());
              BufferedReader cin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
         PrintStream cout=new PrintStream(echoSocket.getOutputStream());
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
      String s1;
            random2=clientin.readInt();
            key2=clientin.readInt();
            response2=clientin.readInt();
            System.out.println(key2);
         response3=(random2)%(key2);
            System.out.println("server is"+response2);
            if(response2==response3)
           {
            System.out.println("client is authenticated..chat starts");
                 while (true)
       {
        s1=cin.readLine();
        if (s1.equalsIgnoreCase("END"))
        {
            cout.println("BYE");
                break;
         }
        System. out.print("Client : "+s+"\n");
        System.out.print("Server : ");
        s1=stdin.readLine();
        cout.println(s1);
       }
          }

             s.close();
               echoSocket.close();
               cin.close();
            cout.close();
            stdin.close();
         }
       }
  • 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-24T19:47:50+00:00Added an answer on May 24, 2026 at 7:47 pm

    You get the output because you are using the wrong variables. The variable you should print from the server is s1 and not s.

    Variable s refers to the socket, that’s why you are getting the socket info instead of the client’s response

    
    s1=cin.readLine();
    if (s1.equalsIgnoreCase("END"))
    {
        cout.println("BYE");
        break;
    }
    
    System. out.print("Client : "+s1+"\n"); // note that this should be s1 and not s
    

    As a good practice, you should name your variable clearly so you and others can always read the code. Having s, s1 and so on will just get you confused later as the code gets larger. It’s also a good habit to have and make other engineers who are working with you happier 🙂

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

Sidebar

Related Questions

I have implemented a simple server-client script like this: Server: class Server(Protocol): def connectionMade(self):
I have created a simple chat server that is driven by client polling. Clients
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I have Implemented a simple program which simulates $ls -l | wc -c command
I am building a simple ajax chat client for a school project and have
We have implemented a simple chat room feature in Rails using Simple Ajax updates.
Hi have implemented simple file exchange over a client/server connection in c++. Works fine
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a simple linux shell in c. Now, I am adding some
In our desktop application, we have implemented a simple search engine using an inverted

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.