I have created a simple server class and a simple client class. I have two client programs that connect to the server. The client & server class can send & receive messages.
The problem is that when both clients connect to the server, one of the clients can send multiple messages to the server whilst the other client can only send one message to the server.
Here is the constructor for my Server class:
public MyServer () {
setUpConnection ();
}
Here is the code that connects the client to a port:
public void setUpConnection () {
clientOutputStreams = new ArrayList ();
try {
serverSocket = new ServerSocket (5000);
while (true) {
Socket clientSocket = serverSocket.accept ();
printWriter = new PrintWriter (clientSocket.getOutputStream ());
clientOutputStreams.add (printWriter);
Thread thread = new Thread (new clientHandler (clientSocket));
thread.start();
System.out.println ("Client is connected to server");
}
} catch (Exception e) {
e.printStackTrace ();
}
}
This is the code that sends the messages received by the server to other clients:
public void tell everyone (String message) {
Iterator outputObject = clientOutputStreams.iterator();
while (outputObject.hasNext()) {
try {
PrintWriter writer = (PrintWriter) outputObject.next();
writer.println (message);
writer.flush ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
This is the code that runs in a separate thread that listens for messages sent by clients:
public class clientHandler implements Runnable {
Socket clientSocket;
public clientHandler (Socket socket) {
try {
clientSocket = socket;
isr = new InputStreamReader (clientSocket.getInputStream());
reader = new BufferedReader (isr);
} catch (Exception e) {
e.printStackTrace ();
}
}
public void run () {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println ("I recieved message: " + message);
tellEveryone (message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Here is the constructor for my client class:
public MyClient() {
initComponents();
setUpConnection ();
Thread thread = new Thread (new recievingMessages ());
thread.start ();
jtaRec.setEditable (false);
jtfSend.addFocusListener (this);
jtfSend.requestFocus();
}
Here is the code that runs when the client presses the send button:
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
writer.println (jtfSend.getText ());
System.out.println (jtfSend.getText ());
writer.flush ();
System.out.println ("message has sent");
} catch (Exception e) {
e.printStackTrace ();
}//CLOSE CATCH STATEMENT
jtfSend.setText ("");
jtfSend.requestFocus();
}
Here is the code that connects the client to the server:
public void setUpConnection () {
try {
socket = new Socket ("127.0.0.1", 5000);
writer = new PrintWriter (socket.getOutputStream ());
stream = new InputStreamReader (socket.getInputStream ());
reader = new BufferedReader (stream);
System.out.println ("Connection Made");
} catch (Exception e) {
e.printStackTrace ();
}
}
Here is the code runs in a separate thread and listens for messages sent by the server:
public class recievingMessages implements Runnable {
public void run () {
String message;
try {
while ((message = reader.readLine ()) != null) {
jtaRec.append (message + "\n");
System.out.println ("I recieved the message" + " " + message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have tried finding a solution but I just don't understand why one client can only send one message whilst the other client can send multiple messages even though both programs share the same class.