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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:06:47+00:00 2026-06-12T13:06:47+00:00

I am in the processes of making a Chat Program in Java in style

  • 0

I am in the processes of making a Chat Program in Java in style with IM programs like MSN. Now, my program won’t be near as full of features as MSN is, nor will i dive into the extensive GUI design either.

The problem I have is, that I might be going for the wrong kind of design to do this.

I am currently designing a Session Object. Basically, users make contact with the server and are kept in “limbo” until they decide to invite someone to a chat. Then it’s the servers job (haven’t gotten to that part yet) to establish a Session between the users involved.

My problem is, that I have to listen for changes in multiple InputStreams and update everyone’s display accordingly. How would I manage to listen on all InputStreams and update when one of them “sends a message” to the Session?

Here is my code so far. It’s not very long.

package server;

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 *
 * @author Vipar
 */
public class Session implements Runnable {
    private static User[] users;
    private static PrintWriter[] outputs;
    private int sessionId;

    public Session(User[] users) {
        this.users = users;
        outputs = new PrintWriter[users.length];
        sessionId = this.hashCode();

        try {
            for(int i = 0; i < users.length; i++) {
            outputs[i] = new PrintWriter(users[i].getSocket().getOutputStream(), true);
            }
        } catch (IOException ioe) {
            ChatServer.print("An error occured in session " + sessionId);
            ChatServer.print(ioe.getMessage());
        }
    }

    @Override
    public void run() {
        ChatServer.print("SessionId: " + sessionId + " have initiated.");
        ExecutorService service = Executors.newFixedThreadPool(users.length);
        for(int i = 0; i < users.length; i++) {
            service.submit(new InputStreamListener(users[i]));
        }
        do {
            if(users.length < 2) {
                ChatServer.print("SessionId: " + sessionId + " have ended.");
                break;
            }
        } while(true);
    }

    public static void update(User u, String message) {
        for(int i = 0; i < users.length; i++) {
            outputs[i].println("new");
            outputs[i].println(u.getUserName());
            outputs[i].println(message);
        }
    }
}

The InputStreamListener I just made:

package server;

import java.util.Scanner;
import java.io.*;
import java.util.NoSuchElementException;

/**
 *
 * @author Vipar
 */
public class InputStreamListener implements Runnable {
    private Scanner scanner;
    private User user;
    public InputStreamListener(User user) {
        this.user = user;
        try {
            scanner = new Scanner(user.getSocket().getInputStream());
        } catch (IOException ioe) {
            ChatServer.print("An Error occured for the InputStream: "
                    + user.getUserName());
            ChatServer.print(ioe.getMessage());
        }

    }

    @Override
    public void run() {
        String s = "";
        do {
            try {
                s = scanner.nextLine();
                Session.update(user, s);
            } catch(NoSuchElementException e) {
                continue;
            }
        } while(!s.equals("/DISCONNECT"));
        try {
            user.getSocket().close();
        } catch(IOException ioe) {
            ChatServer.print("Problem closing users InputStream: "
                    + user.getUserName());
            ChatServer.print(ioe.getMessage());
        }
    }
}
  • 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-06-12T13:06:48+00:00Added an answer on June 12, 2026 at 1:06 pm

    The simplest approach is use one thread for each client. Each thread reads from its own InputStream and inform session about new messages. Such thread also can know how to send messages back to client.
    If you expect many clients, you can try something more lightweight than thread, like actors (eg Akka). Or try to use async IO with NIO.

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

Sidebar

Related Questions

I'm making an editor-like program. If the user chooses File->Open in the main window
I've used PPerl for deamon like processes. This program turns ordinary perl scripts into
This is similar to my last question. I'm making a simple tcp/ip chat program
I am making a Process-Viewer like app. in windows which displays all the processes
I'm making a program which controls other processes (as far as stopped and started
I am making a server and I use fork() for creating child processes but
I am making a program for handheld PDAs using .net 2.0 compact framework and
I am making a simple chat bot in Python. It has a text file
I am making a chat, and I have a system that notifies the users
I am making a mostly client side JavaScript / JQuery app that processes data

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.