First of all I apologize as my explainations is going to be lengthy
I have a server that listens to incoming clients and they are connected to localhost.
Assuming that I only have 2 clients and assuming they only enter a text ONCE
so each time the client is connected, the client will send a message to the server.
the server will receive the message and send back to the client.
everything is fine but now instead of the server receiving and sending back the message to the individual client,
I want the server to wait for the 2nd client to enter finish the text and then send back the messages
that both clients have typed.
I stored the messages received from the respective client in an array.
for(int i = 0; i <array.length; i++) {
if(array[i] == null && flagNull) {
array[i] = msg;
flagNull = false;
}
System.out.println(array[i]);
}
flagNull = true;
Maybe the next question you guys will ask? why do I write it like that to store the
messages? because if I do something like this,
for(int i = 0; i <array.length; i++) {
if(array[i] == null) {
array[i] = msg;
}
}
then if first client is connected and types a message let's say "a",
by right it should detect the first null position in the array and store "a" and
exit, so array[0] = "a" and array[1] = "null" but however my array[0] and array[1] will store "a".
I have a list that stores my clientThread and will then send the messages to the clients only if the clientId = 2.
for(int i = 0; i < clientThreadList.size(); i++) {
//synchronized(this) {
if(clientId == 2) {
clientThreadList.get(i).output.println(array[i]);
}
//}
}
But despite doing this, my client will only get back the message they typed.
For example
client A types "a" -> he will get back "a"
client B types "b" -> he will get back "b"
expected output
If client A types "a", the server will not output first.
If client B types "b", the server will then output "a" and "b" to both clients
I am not really sure also if I need to use -> synchronized,please tell me how should I use it if I ever need thanks.
Server.java
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class Server extends JPanel {
private Socket threadSocket;
private JTextArea textArea = new JTextArea(30,30);
private String[] array = new String[2];
private List<ClientThread> clientThreadList = new ArrayList<ClientThread>();
private int id = 0;
public Server() {
add(textArea);
textArea.setEditable(false);
Thread t = new Thread(new acceptClient());
t.start();
}
class acceptClient implements Runnable {
@Override
public void run() {
try {
ServerSocket sSocket = new ServerSocket(4444);
textArea.append("Server started at: " + new Date());
while(true) {
Socket socket = sSocket.accept();
id++;
ClientThread cT = new ClientThread(socket,id);
cT.start();
clientThreadList.add(cT);
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
class ClientThread extends Thread {
private String msg = " ";
private PrintWriter output = null;
private BufferedReader input = null;
private boolean flagNull = true;
private int clientId = 0;
private int limit = 2;
//private ClientThread ct;
public ClientThread(Socket socket,int id) {
threadSocket = socket;
clientId = id;
}
@Override
public void run() {
try {
output = new PrintWriter(threadSocket.getOutputStream(), true);
try {
input = new BufferedReader(new InputStreamReader(threadSocket.getInputStream()));
output.println("You have connected at: " + new Date());
textArea.append("\nClient connected\n");
while(true) {
msg = input.readLine();
textArea.append("Message From client " + clientId + ":" + msg);
for(int i = 0; i <array.length; i++) {
if(array[i] == null && flagNull){
array[i] = msg;
flagNull = false;
}
System.out.println(array[i]);
}
flagNull = true;
for(int i = 0; i < clientThreadList.size(); i++) {
//synchronized(this) {
if(clientId == 2) {
clientThreadList.get(i).output.println(array[i]);
}
//}
}
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new Server());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
});
}
}
Client.java
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Client extends JPanel {
private PrintWriter output = null;
private BufferedReader input = null;
private Socket socket;
private JTextField textField = new JTextField(10);
private String msg = " ";
public Client() {
setLayout(new FlowLayout());
try {
socket = new Socket("localhost",4444);
output = new PrintWriter(socket.getOutputStream(), true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
output.println(textField.getText());
}
});
add(textField);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try {
msg = input.readLine();
System.out.println(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});t.start();
}catch (IOException exception) {
System.out.println("Error: " + exception);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new Client());
frame.pack();
frame.getPreferredSize();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}