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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:42:46+00:00 2026-05-23T00:42:46+00:00

I have a chat program. Now the code works for communicate between client and

  • 0

I have a chat program. Now the code works for communicate between client and server via command line. But it gives an exception (java.net.SocketException: Socket is closed) while running. Please help me to fix that problem.

In a java chat program,how will the communication be implemented between client and server?

ie.

client<–>server (between server and client)

             or

client A<–>server<–>client B (server act as a bridge between two clients)

Is the 2 way communication can be implemented through a single socket?
Are there any other methods ?
How to communicate more than one client simultaneously?

server code

class Server
{
ServerSocket server;
Socket client;
public Server()
{
    try
    {
        server = new ServerSocket(2000);
        System.out.println("\tServer Started..........");
        while (true)
        {
            client = server.accept();
            Send objsend = new Send(client);
            Recive objrecive = new Recive(client);
            //client.close();
        }
    }
    catch (Exception e)
    {
        System.out.println("Exception4 " + e);
    }
}
    public static void main(String arg[])
    {
        new Server();
    }
}
class Recive implements Runnable
{
    Socket client;
    public Recive(Socket client1)
    {
        client=client1;
        Thread trsend=new Thread(this);
        trsend.start();
    }
    public void run()
    {
        ObjectInputStream ois;
        Message M=new Message();
        try
        {
            ois = new ObjectInputStream(client.getInputStream());
            M = (Message)ois.readObject();
            M.display();
            ois.close();
        }
        catch (Exception e)
        {
            System.out.println("Exception1 " + e);
        }
    }
}    
class Send implements Runnable
{
    Socket client;
    public Send(Socket client1)
    {
        client=client1;
        Thread trrecive=new Thread(this);
        trrecive.start();
    }
    public void run()
    {
        Message M=new Message();
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        try
        {
            System.out.println("Me(server)");
            M.strmessage=br.readLine();
            ObjectOutputStream oos=new ObjectOutputStream(cli    ent.getOutputStream());
            oos.writeObject((Message)M);
            oos.flush();
            oos.close();
        }
        catch (Exception e)
        {
            System.out.println("Exception " + e);
        }
    }
}

client code

class Client
{
public static void main(String arg[])
{
    try
    {

        Send objsend=new Send();
        Recive objrecive=new Recive();
    }
    catch(Exception e)
    {
        System.out.println("Exception "+e);
    }

}
}
class Send implements Runnable
{

public Send()
{
    Thread trsend=new Thread(this);
    trsend.start();
}
public void run()
{

    try
    {
        Message M=new Message();
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        while(true)
        {
            System.out.println("Me(client)");
            M.strmessage=br.readLine();

            Socket client=new Socket("localhost",2000);
            ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
            oos.writeObject((Message)M);

            oos.flush();
            oos.close();
        }
    }
    catch(Exception e)
    {
        System.out.println("Exception "+e);
    }
}
}
class Recive implements Runnable
{
public Recive()
{
    Thread trrecive=new Thread(this);
    trrecive.start();
}
public void run()
{

    try
    {
        while(true)
        {
        Socket client=new Socket("localhost",2000);
        ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
        Message CNE=(Message)ois.readObject();
        CNE.display();

        ois.close();
        }

    }
    catch(Exception e)
    {
        System.out.println("Exception "+e);
    }
}
}
  • 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-23T00:42:47+00:00Added an answer on May 23, 2026 at 12:42 am

    First of all, don’t close the streams in every run().

    Secondly, check whether port for server which you are using is free.

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

Sidebar

Related Questions

I have implemented a chat service and desktop client using Java, the program works
I have been working on a (relatively) simple tcp client/server chat program for my
I have been written chat program code for wired network. I want to write
I am doing a client-server voice chat program(unmanaged C++,win32) in which clients connects to
I have written a server and a client both as separate apps. They communicate
We have a chat program that works with only a couple of browsers right
I have seen lots of chat examples in Erlang but what about lists, like
Lets say I have a chat program that every time someone sends a message,
I have to program websites, but I rather don't like the static HTML nature.
I have developed a small chat client using WPF. In each chat window, it

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.