I am trying to build a server using ServerSocket class in Java and making an app like Discord where you launch the program ServeurDialogue in the shell and connect to it via telnet Client using the local address and the port: 2021 and for that, I used two threads one to stay in a listening mode where is accepts new clients and stocks them in an array, and the second thread sends a message whenever a client sends a message it transfers it to the rest of the clients.
Now my problem is that the thread that I named sendToAll is not going through the for loop
Thread sendToAll = new Thread(){
public void run(){
String tmpMsg;
while(true){
try{
for (ThreadClient c : clients) {
if (c.getPendingMsg()) {
for (ThreadClient cl : clients) {
if (c != cl) {
cl.getSocket().getOutputStream().write(c.getMessage().getBytes());
}
}
}
}
} catch(IOException e){
System.out.println(e);
}
}
}
};
Even though in my second Thread connectionThread I have the same loop but it goes through the clients and sends the message new Client with no problem
Thread connectionThread = new Thread(){
public void run(){
Socket socketRecu;
while (true){
System.out.println("Server is waiting for a connection...");
try{
socketRecu = serveur.accept();
System.out.println("Client Accepted!\nAddress: "+ socketRecu.getLocalAddress() + "\nPort: "+ socketRecu.getPort());
socketRecu.getOutputStream().write("Welcome in my Server!\n".getBytes());
ThreadClient client = new ThreadClient(socketRecu);
for (ThreadClient c : clients) {
c.getSocket().getOutputStream().write("new Client\n".getBytes());
}
clients.add(client);
client.start();
} catch(IOException e){
System.out.println(e);
}
}
}
};
So why is the second thread going across the loop as it should do but not in the first thread?
For Your Information: ThreadClient is a class I made that extends the Thread class and keeps listening if the client wants to send the message.
Here below you will find the full two classes that you need.
the files:
ServeurDialogue.java:
import java.net.ServerSocket;
import java.io.IOException;
import java.net.Socket;
import java.lang.Thread;
import java.util.Arrays;
import java.util.ArrayList;
import util.ThreadClient;
import util.ServerThread;
public class ServeurDialogue {
private static final int PORT = 2021;
public static void main(String[] args) {
try{
ServerSocket serveur = new ServerSocket(PORT);
ArrayList<ThreadClient> clients = new ArrayList<>();
Thread sendToAll = new Thread(){
public void run(){
String tmpMsg;
while(true){
try{
for (ThreadClient c : clients) {
if (c.getPendingMsg()) {
for (ThreadClient cl : clients) {
if (c != cl) {
cl.getSocket().getOutputStream().write(c.getMessage().getBytes());
}
}
}
}
} catch(IOException e){
System.out.println(e);
}
}
}
};
sendToAll.start();
Thread connectionThread = new Thread(){
public void run(){
Socket socketRecu;
while (true){
System.out.println("Server is waiting for a connection...");
try{
socketRecu = serveur.accept();
System.out.println("Client Accepted!\nAddress: "+ socketRecu.getLocalAddress() + "\nPort: "+ socketRecu.getPort());
socketRecu.getOutputStream().write("Welcome in my Server!\n".getBytes());
ThreadClient client = new ThreadClient(socketRecu);
for (ThreadClient c : clients) {
c.getSocket().getOutputStream().write("new Client\n".getBytes());
}
clients.add(client);
client.start();
} catch(IOException e){
System.out.println(e);
}
}
}
};
connectionThread.start();
} catch(IOException e){
System.out.println(e);
}
}
}
ThreadClient.java:
package util;
import java.lang.Thread;
import java.net.Socket;
import java.lang.Thread;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.IOException;
public class ThreadClient extends Thread {
private Socket socket;
private byte[] buffer = new byte[1024];
private boolean pendingMsg;
private String message;
public ThreadClient(Socket socket){
super();
this.socket = socket;
this.pendingMsg = false;
}
public Socket getSocket(){
return this.socket;
}
public boolean getPendingMsg(){
return this.pendingMsg;
}
public byte[] getBuffer(){
return this.buffer;
}
public String getMessage(){
this.pendingMsg = false;
return this.message;
}
public void sendMessage(){
this.pendingMsg = true;
this.message = new String(this.buffer);
}
public void run(){
while(true){
Arrays.fill(this.buffer, (byte) 0);
try{
this.socket.getInputStream().read(this.buffer);
this.sendMessage();
System.out.print("Message received: "+ this.message);
} catch(IOException e) {System.out.println(e);}
}
}
}