2 Replies - 892 Views - Last Post: 02 March 2013 - 12:37 PM Rate Topic: ***-- 2 Votes

#1 RJCoder8989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-March 13

Send message to multiple clients

Posted 02 March 2013 - 12:49 AM

I want to send same message from server to multiple clients. How I can do it?




import java.io.*;
import java.net.*;


public class ServerChat implements Runnable
{
    static ServerSocket server;
    static Socket client;

    static class Read implements Runnable
    {
        public void run()
        {
            try
            {
                InputStream istream=client.getInputStream();
                InputStreamReader isreader=new InputStreamReader(istream);
                BufferedReader breader=new BufferedReader(isreader);
                String reader;
                while((reader=breader.readLine())!=null)
                {
                    System.out.println("From Client:"+reader);
                }
                breader.close();
                istream.close();
                isreader.close();
                client.close();
            }
            catch(Exception read)
            {
                System.out.println(read);
            }
        }
    }

    static class Write implements Runnable
    {
            public void run()
            {
                try
                {
                    OutputStream opstream=client.getOutputStream();
                    PrintStream pstream=new PrintStream(opstream);
                    BufferedReader breader1=new BufferedReader(new InputStreamReader(System.in));
                    String write;
                    while(!(write=breader1.readLine()).equals("exit"))
                    {
                        pstream.println(write);
                    }
                    breader1.close();
                    opstream.close();
                    pstream.close();
                    client.close();
                }
                catch(Exception write)
                {
                    System.out.println(write);
                }
            }
    }

    int n=1;
    public void run()
    {
        for(;;)/>/>
        {
            try
            {
                Read read=new Read();
                Write write=new Write();
                client=server.accept();
                System.out.println("Connection established from client:"+n);
                Thread t1=new Thread(read);
                Thread t2=new Thread(write);
                t1.start();
                t2.start();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
          n++;
        }
    }

    public static void main(String[] args)throws Exception
    {
        ServerChat chat=new ServerChat();
        server=new ServerSocket(6666);
        System.out.println("Waiting for connection");
        Thread clientthread=new Thread(chat);
        Thread clientthread1=new Thread(chat);
        clientthread.start();
        clientthread1.start();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Send message to multiple clients

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Send message to multiple clients

Posted 02 March 2013 - 12:32 PM

I am not too sure that you are setting this up right in that you are using the same instance in two different threads. Usually you create a new instance for each thread... meaning you have two individual connections each running on their own thread. Then you would store a list of this threads in a collection.

Then when one of the clients writes something to you, it comes in on their thread, that thread reads it and then triggers a mechanism up the chain. From there that mechanism will take the message and loop through all the threads, calling writes on each one and giving them the message.

I am also not too sure why you are splitting out writes and reads out onto their own threads. This seems a bit excessive to me. This would leave the "main" thread sending the message to each thread and then thread would have to pass it to another thread to write when perhaps that thread should just write it to begin with.

Perhaps you can explain more about what you are trying to do here. :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Send message to multiple clients

Posted 02 March 2013 - 12:37 PM

Save in an ArrayList<Write> all your Write object at creation time. To broiadcast simply scan that ArrayList

Also, a lot of overhead for noting to create a Thread to read and a Thread to write
You need a Thread to read the mesages from the Client but to write you don't really nead a Thread perClient. The main Server thread can do it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1