Trying to do a broadcast server that echos clients

Multi tool use
Multi tool use


Trying to do a broadcast server that echos clients



I'm trying to do a simple group chat where there's a server and several clients.
The objective is when a client sends a message to the server, the server just sends that message back to all the other clients. I had the server send a message writen by him send to all the clients but he didn't send the clients message.



Server code:


package Group;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.*;

public class GroupServer extends Thread {
private ServerSocket server;
protected List<ClientHandler> clients;

public static void main(String args) throws IOException {
new GroupServer(9876);
}

public GroupServer(int port) {
try {
this.server = new ServerSocket(port);
System.out.println("New server initialized!");
clients = Collections.synchronizedList(new ArrayList<ClientHandler>());
this.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Socket client = server.accept();
System.out.println(client.getInetAddress().getHostName() + " connected");
ClientHandler newClient = new ClientHandler(client);
clients.add(newClient);
new SendMessage(clients);

} catch (Exception e) {
e.printStackTrace();
}
}
}
}

class ClientHandler {
protected Socket client;
protected PrintWriter out;
protected DataInputStream in;

public ClientHandler(Socket client) {
this.client = client;
try {
this.out = new PrintWriter(client.getOutputStream());
this.in = new DataInputStream(client.getInputStream());

} catch (IOException e) {
e.printStackTrace();
}
}
}

class SendMessage extends Thread {
protected List<ClientHandler> clients;
protected String userInput;
protected String sendMessage;
protected BufferedReader stdIn;
protected DataInputStream in;

public SendMessage(List<ClientHandler> clients) {
this.clients = clients;
this.userInput = null;
this.start();
}

public void run() {
System.out.println("New Communication Thread Started");
if (clients.size() == 1) {
System.out.println("Enter message:");
}
try {
if (clients.size() > 0) {
this.stdIn = new BufferedReader(new InputStreamReader(System.in));

while ((this.userInput = stdIn.readLine()) != null) {
if (userInput != null & userInput.length() > 0) {
for (ClientHandler client : clients) {
sendMessage = client.in.readUTF();
client.out.println(sendMessage);
client.out.flush();
Thread.currentThread();
Thread.sleep(1 * 1000);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}



The client code:


package Group;
import java.net.*;
import java.io.*;
import java.util.logging.*;

public class GroupClient {
protected Socket client;
protected BufferedReader in;

public static void main(String args) {
new GroupClient("Localhost", 9876);
}

public GroupClient(String hostName, int ip) {
try {
this.client = new Socket(hostName, ip);
this.in = new BufferedReader(new InputStreamReader(
this.client.getInputStream()));
String buffer = null;
while ((buffer = in.readLine()) != null) {
System.out.println(buffer);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}



I think the error is in the Server, more preciselly on the SendMessage class.
Thank you for the attention.





Why do you create a new SendMessage every time there is a client connects? Also if the server is only supposed to propagate client messages, why is the code taking user input running on the server side (inside SendMessage) instead of on the client side?
– jingx
Jun 30 at 15:25


SendMessage


SendMessage





If you're still having trouble, you can easily benefit from my networking framework: github.com/jhg023/SimpleNet
– Jacob G.
Jul 1 at 0:11




1 Answer
1



You have just one thread "SendMessage" for all the clients.



The very first time you call client.in.readUTF() in a loop, the thread blocks till that client has sent something. Since all the other clients are handled by the same thread. All of them are blocked too.


client.in.readUTF()



Either you have one thread per client socket or go the nio selector way (preferrable).



Also fix the issues mentioned by @jingx.



For synchronized arraylist, use CopyOnWriteArrayList. It is specifically meant for these kind of usecases. Synchronization help in concurrent addition and deletion but not during concurrent iteration. CopyOnWriteArrayList solves that problem.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

OP6,uzbEC Z9BiipXe N7 MWUQWk0RzymWhHP60Ry0Er u,fcCIR qGOXc1iQHn,2JRLOzF7Zavaj JSzg0UMTmYKPqo3U
c,Cyi9M

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters