• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Java Client-to-Client Chat Program through Server

April 11, 2020 by

Questions › Java Client-to-Client Chat Program through Server
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I am writing a simple chat Java chat program. I have message objects, which are sent from clients through ObjectStreams. The message object are assumed to have the client source and destination addresses. The server is multi-threaded, with one thread handling client requests and a thread for each accepted connection.

I have no problem with the code involving a client sending a message object to a server and the server sending an acknowledgement message back to the same client. But I am having a lot of difficulty setting up the code for have a client, say client 1, sending an object to the server, and the server forwarding the output object stream to client 2's object input stream.

Here is the message object:

public Message(String name, String text, InetAddress source, InetAddress destination) { 
    this.name = name;
    this.text = text;
    this.destination = destination;
    this.source = source;}

Here is the server code snipped:

new Thread(() -> {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("Server started at " + new Date());

            while (true) {
                // listen for connection request
                Socket socket = serverSocket.accept();

                //System.out.println(socket);

                //System.out.println("Client " + socket.getInetAddress().getHostAddress());

                new Thread(() -> {
                    // read data from client
                    try (ObjectInputStream fromClient = new ObjectInputStream(socket.getInputStream());) {

                        while(true) {
                            // get message from client
                            Message message = (Message) fromClient.readObject();

I have tried saving the sockets or socket output streams in an array list, but I am not having any luck.

How can I complete the server so that it can forward client x's message object to client y?

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: chat, client-server, java, multithreading, server

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023