Send message to all connected clients

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 1306 Views - Last Post: 02 March 2013 - 10:15 AM Rate Topic: ***** 1 Votes

#1 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Send message to all connected clients

Posted 17 February 2013 - 11:34 AM

Hello. This has probably been answered before but I can't find anything that works. I have made a simple multiple thread server and I want to send a message to all the connected clients. I can't work out how to do it as I am using threads and I have never used them before and I am a bit of a n00b with them.

Here is the code for the server class:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static final int PORT = 6789;
	Socket socket = null;

	public static void main(String[] args) throws IOException {
		new Server().runServer();
	}

	public void runServer() throws IOException {
		ServerSocket serverSocket = new ServerSocket(PORT);
		System.out.println("Server is ready");
		while (true) {
			socket = serverSocket.accept();
			new ServerThread(socket).start();
		}
	}
}


Here is the code for the serverthread class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread {
	Socket socket;
	PrintWriter printWriter = null;

	ServerThread(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			String message = null;
			printWriter = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			System.out.println(bufferedReader.readLine() + " is now connected");
			while ((message = bufferedReader.readLine()) != null) {
				if (!(message.startsWith("user") && message.endsWith("null"))) {
					printWriter.println(message);
				}
			}
			socket.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}



And here is the code for the client class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

	public static void main(String[] args) throws IOException {
		String name = args[0];
		try {
			Socket socket = new Socket("192.168.0.5", 6789);
			BufferedReader bufferedReaderFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
			printWriter.println(name);
			BufferedReader bufferedReaderFromCommandPrompt = new BufferedReader(new InputStreamReader(System.in));
			while (true) {
				String readerInput = bufferedReaderFromCommandPrompt.readLine();
				printWriter.println(name + ": " + readerInput);
				System.out.println(bufferedReaderFromClient.readLine());
			}
		} catch (ConnectException connectException) {
			System.out.println(connectException.getMessage());
		}

	}

}



(The client class is the only class in the client program)

If anybody know what I need to do that would be great :D/>
Thanks in advance
-Curly

This post has been edited by curlyfries1999: 17 February 2013 - 11:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Send message to all connected clients

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Send message to all connected clients

Posted 17 February 2013 - 11:59 AM

The Server would have to keep List<ServerThread> so it could send a message to each
Was This Post Helpful? 0
  • +
  • -

#3 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 17 February 2013 - 12:04 PM

View Postg00se, on 17 February 2013 - 11:59 AM, said:

The Server would have to keep List<ServerThread> so it could send a message to each

Could you give me an example please. I don't use this sort of thing often. Thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Send message to all connected clients

Posted 17 February 2013 - 12:08 PM

public static final int PORT = 6789;
Socket socket = null;
List<ServerSocket> clients = new ArrayList<ServerSocket>();


Add to the list after accept()
Was This Post Helpful? 0
  • +
  • -

#5 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 17 February 2013 - 12:12 PM

View Postg00se, on 17 February 2013 - 12:08 PM, said:

public static final int PORT = 6789;
Socket socket = null;
List<ServerSocket> clients = new ArrayList<ServerSocket>();


Add to the list after accept()

What do I add and how? I have never done this before.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Send message to all connected clients

Posted 17 February 2013 - 12:13 PM

Something tells me you didn't write any of the code you posted ...
Was This Post Helpful? 1
  • +
  • -

#7 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 17 February 2013 - 12:14 PM

View Postg00se, on 17 February 2013 - 12:13 PM, said:

Something tells me you didn't write any of the code you posted ...

Something told you a lie...
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,653
  • Joined: 20-September 08

Re: Send message to all connected clients

Posted 17 February 2013 - 12:24 PM

Sorry, there was a typo:

public static final int PORT = 6789;
Socket socket = null;
List<ServerThread> clients = new ArrayList<ServerThread>();


Was This Post Helpful? 0
  • +
  • -

#9 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 17 February 2013 - 12:46 PM

View Postcurlyfries1999, on 17 February 2013 - 12:12 PM, said:

What do I add and how? I have never done this before.

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Send message to all connected clients

Posted 17 February 2013 - 02:31 PM

The ArrayList class has a add() method
When you start a new ServerThread add it to the list

        while (true) {  
            socket = serverSocket.accept();  
            ServerThread st = new ServerThread(socket);
            st.start();
            clients.add(st);  
         }  
 


To broadcast simply scan the ArrayList clients and use each entry printWriter
Was This Post Helpful? 1
  • +
  • -

#11 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 17 February 2013 - 02:35 PM

View Postpbl, on 17 February 2013 - 02:31 PM, said:

The ArrayList class has a add() method
When you start a new ServerThread add it to the list

        while (true) {  
            socket = serverSocket.accept();  
            ServerThread st = new ServerThread(socket);
            st.start();
            clients.add(st);  
         }  
 


To broadcast simply scan the ArrayList clients and use each entry printWriter


Thanks a lot. That makes a lot of sense.I will try it tomorrow. :D
Was This Post Helpful? 0
  • +
  • -

#12 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 18 February 2013 - 03:36 AM

View Postcurlyfries1999, on 17 February 2013 - 02:35 PM, said:

View Postpbl, on 17 February 2013 - 02:31 PM, said:

The ArrayList class has a add() method
When you start a new ServerThread add it to the list

        while (true) {  
            socket = serverSocket.accept();  
            ServerThread st = new ServerThread(socket);
            st.start();
            clients.add(st);  
         }  
 


To broadcast simply scan the ArrayList clients and use each entry printWriter

Thanks a lot. That makes a lot of sense.I will try it tomorrow. :D/>/>

That seemed to work but how do I send a message from there?

This post has been edited by curlyfries1999: 18 February 2013 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Send message to all connected clients

Posted 18 February 2013 - 11:28 AM

ypur sderver can have

   void broadcast(String msg) {
     for(int i = list.size(); --i >=0; ) {
        ServerThread st = list.get(i);
        if(st is still connected)
            st.printWriter.xxxxx(msg);
        else
            list.remove(i);
     }


You'll need check if the Client disconnected (you might have to save the Socket in your Client instance variable). If it is the case remove it from the list (that is why I made it in reverse order.

This post has been edited by pbl: 18 February 2013 - 11:32 AM

Was This Post Helpful? 1
  • +
  • -

#14 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 18 February 2013 - 12:40 PM

View Postpbl, on 18 February 2013 - 11:28 AM, said:

ypur sderver can have

   void broadcast(String msg) {
     for(int i = list.size(); --i >=0; ) {
        ServerThread st = list.get(i);
        if(st is still connected)
            st.printWriter.xxxxx(msg);
        else
            list.remove(i);
     }


You'll need check if the Client disconnected (you might have to save the Socket in your Client instance variable). If it is the case remove it from the list (that is why I made it in reverse order.


I think i'm derping :stupid:/>. I can't work out why i'm getting a NullPointerException on line
st.printWriter.print(msg);


EDIT: And now I'm not getting it..... I don't know what the hell I changed.......

This post has been edited by curlyfries1999: 18 February 2013 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#15 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: Send message to all connected clients

Posted 18 February 2013 - 12:57 PM

View Postpbl, on 18 February 2013 - 11:28 AM, said:

ypur sderver can have

   void broadcast(String msg) {
     for(int i = list.size(); --i >=0; ) {
        ServerThread st = list.get(i);
        if(st is still connected)
            st.printWriter.xxxxx(msg);
        else
            list.remove(i);
     }


You'll need check if the Client disconnected (you might have to save the Socket in your Client instance variable). If it is the case remove it from the list (that is why I made it in reverse order.


The line
st.printWriter.println("msg");
only sends a message to the client who triggered the void. Any ideads?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3