0

I am currently working on a one server many clients system. I am trying to get it so the server can send out one command, through a PrintWriter, that will go through to all of the clients connected on that socket. However in practice the command only goes through to one client. All of the clients are created on one socket, and all use the same Scanner. Is what I am trying to do possible?

Some code(incase it helps)

Creation of the socket:

serverSocketRefresh = new ServerSocket(PORTREFRESH); refresh = serverSocketRefresh.accept(); Creation of the Print Writer and the Scanner: networkOutputRefresh = new PrintWriter(refresh.getOutputStream(), true); networkInput = new Scanner(refresh.getInputStream()); 

Ceation of the clients:

do { // Wait for client... client = serverSocket.accept(); System.out.println("\nNew client accepted.\n"); handler = new ClientHandler(client,networkOutputRefresh, itemArray, bidderArray); handler.start(); } while (true); 

The command im trying to transmit to all of the clients:

public static void updatePrice() { networkOutputRefresh.println("1"); } 
1
  • what kind of different clients are connected on one socket? Commented May 1, 2013 at 3:56

2 Answers 2

0

I am not sure if I correctly understand your code but it seems you are using a single reference of client. And your client reference will be holding the last client reference and hence the printwrite is writing only for that client. Ideally if you want to publish something to all the clients then you should have a collection of client references.Whenever you get an accept on the server socket, add the new client reference to your collection. And whenever you have to publish to all the clients just iterate over your client collection and publish using their associated printwriters.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just use a BufferedReader and BufferedWriter, and make a new one each time you accept a client?

Edit: Or, if that '1' is the only thing you will ever send over that socket, just send it over the socket directly, as a byte. I believe the method is something like socket.write(new byte[] { 1 }, 0, 1), and to read on the other end, socket.read(buffer, 0, 1), where buffer is a byte array of length 1.

9 Comments

It is a uni project all about sockets, and the tutorial specifically tells us to use PrintWriters and scanners:/
@user2254372 Ah, OK. PrintWriters and Scanners should work, as long as you make a new one for each client the connects.
Is their no way to do it with a print writer that writes to several threads using the same scanner?
Basically, I have a thread on each client that sits and waits for the command, and I want the server to send one command out to all the different threads telling them all to update simultaneously, but I have no idea if it is possible:/
If I create a PrintWriter and Scanner for each new client, only one client will receive the message though?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.