Send message to all connected clients

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

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

#16 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 18 February 2013 - 01:13 PM

How does the Client inform the actual server that it should broadcast a message ? By sending it a message of az particular type ?

I guess you will have to repost your updated code ?
Was This Post Helpful? 0
  • +
  • -

#17 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 19 February 2013 - 03:36 AM

View Postpbl, on 18 February 2013 - 01:13 PM, said:

I guess you will have to repost your updated code ?


Server:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {

	public static final int PORT = 6789;
	Socket socket = null;
	List<ServerThread> clients = new ArrayList<ServerThread>();
	PrintWriter printWriter = null;
	ServerThread st = 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();
			printWriter = new PrintWriter(socket.getOutputStream(), true);
			st = new ServerThread(socket);
			st.start();
			clients.add(st);
		}
	}

	public void broadcast(String msg) {
		System.out.println(clients.size());
		for (int i = 0; i < clients.size(); i++) {
			st = clients.get(i);
			st.printWriter.println(msg);
		}
	}
}


ServerThread:
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 {
			Server s = new Server();
			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");
			s.clients.add(this);
			while ((message = bufferedReader.readLine()) != null) {
				if (!(message.startsWith("user") && message.endsWith("null"))) {
//					printWriter.println(message);
					s.broadcast("TEST");
				}
			}
			socket.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			if (e.getMessage() == "Connection reset"){
				Server s = new Server();
				s.clients.remove(this);
			}
		}
	}
}


This post has been edited by curlyfries1999: 19 February 2013 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#18 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 19 February 2013 - 04:52 AM

That does not make sense

Server s = new Server();

each ServerThread creates its own server ? So you will have as many Server as you have Client. And sure their clients List will contain only that Client

3 choices:
- You have to make a communication protocol. If the first byte of a message is something then it is a regular message, if it is something else, it means it should be broadcast.
- Pass the Server object to the ClientThread constructor so it can reference it
- Make the Server a Singleton so you can reference it

This post has been edited by pbl: 19 February 2013 - 04:57 AM
Reason for edit:: Oups.. some correction

Was This Post Helpful? 0
  • +
  • -

#19 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 19 February 2013 - 08:10 AM

I've found this and it's exactly what I'm looking for. Thanks for all of you help anyway :D/>
Was This Post Helpful? 0
  • +
  • -

#20 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: Send message to all connected clients

Posted 19 February 2013 - 08:20 AM

Quote

I've found this and it's exactly what I'm looking for.
That's a completely different paradigm, so you'll need to start over
Was This Post Helpful? 0
  • +
  • -

#21 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 19 February 2013 - 09:12 AM

View Postg00se, on 19 February 2013 - 08:20 AM, said:

Quote

I've found this and it's exactly what I'm looking for.
That's a completely different paradigm, so you'll need to start over

What?! It does what I want it to do. Just needs a few tweeks

EDIT: Sorry. You're right. After looking at it for a while I've realised it's not what I want.

This post has been edited by curlyfries1999: 19 February 2013 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#22 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 19 February 2013 - 09:37 AM

It is a completlete different application and algorithm.
It will need more than a few tweek (a lot more than correcting your actual code)

The message will be brodcast to all hosts on your network (not onl;y your client)
You will need to modify your Client so they will also listen to datagram

If your Server holds 10 different chat room, the message on any room will be broadcast to all the Clients of all the rooms

Have fun
Was This Post Helpful? 0
  • +
  • -

#23 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 19 February 2013 - 09:43 AM

View Postpbl, on 19 February 2013 - 09:37 AM, said:

It is a completlete different application and algorithm.
It will need more than a few tweek (a lot more than correcting your actual code)

The message will be brodcast to all hosts on your network (not onl;y your client)
You will need to modify your Client so they will also listen to datagram

If your Server holds 10 different chat room, the message on any room will be broadcast to all the Clients of all the rooms

Have fun


Yeah, I've realised now that it's not what I want so I've changed my code back.
Was This Post Helpful? 0
  • +
  • -

#24 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 19 February 2013 - 09:54 AM

So what am I doing wrong? I have changed my code:
Server:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {

	public static final int PORT = 6789;
	Socket socket = null;
	public static List<ServerThread> clients = new ArrayList<ServerThread>();
	PrintWriter printWriter = null;
	static ServerThread st = 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();
			printWriter = new PrintWriter(socket.getOutputStream(), true);
			st = new ServerThread(socket);
			st.start();
			clients.add(st);
		}
	}

	public static void broadcast(String msg) {
		for (int i = 0; i < clients.size(); i++) {
//			System.out.println(clients.size());
			System.out.println(i);
			st = clients.get(i);
			st.printWriter.println(msg);
		}
	}
}

ServerThread:
package org.curlyfries.Socket;

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);
					Server.broadcast("TEST");
				}
			}
			socket.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			if (e.getMessage() == "Connection reset"){
				Server s = new Server();
				s.clients.remove(this);
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 19 February 2013 - 10:08 AM

I don't like it :)

Avoid static make it that way... pass your actual Server object to the ServerThread
Have the ServerThread to save it in its instance variable like it does for the Socket

     st = new ServerThread(this, socket);


     // ServerThread instance variables
     Socket socket;  
     Server server;


     ServerThread(Server server, Socket socket) {
        this.server = server;
        this.socket = socket;
....

        server.broadcast("Test");



Back to your problem... the order into which the Client and the Server open their Input and Output stream does matter.
When an OutputStream is open the open "freeze" there until the counter part opens the InputStream so they have to be done in order like:

Client open input
Server open output
Server open input
Client open output

or

Server open input
Client open output
Client open input
Server open output


so your code just freeze executing

printWriter = new PrintWriter(socket.getOutputStream(), true);
Was This Post Helpful? 0
  • +
  • -

#26 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 19 February 2013 - 10:12 AM

View Postpbl, on 19 February 2013 - 10:08 AM, said:

I don't like it :)/>/>

Avoid static make it that way... pass your actual Server object to the ServerThread
Have the ServerThread to save it in its instance variable like it does for the Socket

     st = new ServerThread(this, socket);


     // ServerThread instance variables
     Socket socket;  
     Server server;


     ServerThread(Server server, Socket socket) {
        this.server = server;
        this.socket = socket;
....

        server.broadcast("Test");



Back to your problem... the order into which the Client and the Server open their Input and Output stream does matter.
When an OutputStream is open the open "freeze" there until the counter part opens the InputStream so they have to be done in order like:

Client open input
Server open output
Server open input
Client open output

or

Server open input
Client open output
Client open input
Server open output


so your code just freeze executing

printWriter = new PrintWriter(socket.getOutputStream(), true);


So are you saying that if 'Client 1' sends a message, the server IS sending TEST back to both clients but 'Client 2' isn't expecting it so it doesn't recieve it?

EDIT:
Do you think that if I told you what I am using this for it might be easier to find a soloution?

This post has been edited by curlyfries1999: 19 February 2013 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#27 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 19 February 2013 - 12:02 PM

All we discussed is on the way communication is established.

You can have two different approach:
- a synchronous approach where the protocol between the Server and the Client is quite clear and you know what to read and when. So the Client send a message, the server send and answer and that's it.
- an asynchronous approach where you don't know who will talk and when and in which order (like when you use a broadcast). In that case the Client also needs to be multithreaded. One thread listen to the socket always waiting for a message from the Server. The master thread, wait for user input and send it to the Server.

I have a Chat Room tutorial explaining all that.
and yes you can tell what you try to achieve
Was This Post Helpful? 0
  • +
  • -

#28 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 19 February 2013 - 12:07 PM

View Postpbl, on 19 February 2013 - 07:02 PM, said:

All we discussed is on the way communication is established.

You can have two different approach:
- a synchronous approach where the protocol between the Server and the Client is quite clear and you know what to read and when. So the Client send a message, the server send and answer and that's it.
- an asynchronous approach where you don't know who will talk and when and in which order (like when you use a broadcast). In that case the Client also needs to be multithreaded. One thread listen to the socket always waiting for a message from the Server. The master thread, wait for user input and send it to the Server.

I have a Chat Room tutorial explaining all that.
and yes you can tell what you try to achieve


Ok. I am going to merge the client into a multiplayer part of a game I am making. I am doing this so I can send co-ordinates to all other clients so they can render the other player. Do you think I am doing this the right way or do you think there is a better way?
Was This Post Helpful? 0
  • +
  • -

#29 pbl  Icon User is offline

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

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

Re: Send message to all connected clients

Posted 19 February 2013 - 12:18 PM

Depends of the game
If it is a chess game or a TicTacToe et is quite clear, you can you synchronous communication one player plays then the other one.

If you have a game with multiple players that can play not all in order then you need thread in your client side
Was This Post Helpful? 0
  • +
  • -

#30 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 19 February 2013 - 01:13 PM

View Postpbl, on 19 February 2013 - 07:18 PM, said:

Depends of the game
If it is a chess game or a TicTacToe et is quite clear, you can you synchronous communication one player plays then the other one.

If you have a game with multiple players that can play not all in order then you need thread in your client side

Well, basicly a bunch of squares bouncing around controlled with the arrow keys
Was This Post Helpful? 0
  • +
  • -

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