import java.awt.BorderLayout;
import java.awt.GridLayout;
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.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import javax.swing.*;
/* The code is able to run single client but not multiple client. When I run this program with my client class it is able to handle only one client. While I run multiple threads it lost its communication with the first thread. I don't know why. */
public class MultipleChatClient extends JFrame{
Vector<HandleAClient> clients = new Vector<HandleAClient>();
JButton btnSend = null;
JButton btnExit = null;
JTextArea taMessages = null;
JTextField tfInput = null;
BufferedReader br = null;
PrintWriter pw = null;
ServerSocket server = null;
Socket socket = null;
int clientNumber = 0;
public MultipleChatClient(){
this.Interface(); /* creates GUI */
try{
server = new ServerSocket(8900);
while(true){
socket = server.accept();
HandleAClient task = new HandleAClient(socket); /* add every client to vector */
clients.add(task);
new Thread(task).start();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
public class HandleAClient implements Runnable{ /* multithreaded class to handle multiclients */
private Socket socket;
public HandleAClient(Socket socket){
clientNumber++;
this.socket = socket;
}
public void run(){
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
while(true){
String line = br.readLine();
taMessages.append("Client Number " + clientNumber + " said: ");
taMessages.append(line + "\n");
}
}
catch(IOException ex){
ex.printStackTrace();
}
catch(Exception e){
taMessages.append("Connection Lost");
}
}
public void sendMessage() {
pw.println(tfInput.getText());
}
}
public void Interface(){ /* to build GUI */
setLayout(new BorderLayout());
btnSend = new JButton("Send");
btnExit = new JButton("Exit");
taMessages = new JTextArea();
taMessages.setRows(10);
taMessages.setColumns(50);
taMessages.setEditable(false);
tfInput = new JTextField(50);
JPanel but = new JPanel(new GridLayout(2,1,5,10));
but.add(btnSend);
but.add(btnExit);
JScrollPane sp = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp,"Ce`enter code here`nter");
JPanel bp = new JPanel(new BorderLayout());
bp.add(tfInput, BorderLayout.CENTER);
bp.add(but, BorderLayout.EAST);
add(bp, BorderLayout.SOUTH);
btnSend.addActionListener(new buttonListner());
//btnSend.addKeyListener(new buttonListner());
btnExit.addActionListener(new buttonListner());
setSize(600,600);
setTitle("Server");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
//pack();
}
public class buttonListner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnExit){
System.exit(1);
}
else{
taMessages.append("You Said: ");
taMessages.append(tfInput.getText() + "\n");
for(HandleAClient c: clients){
c.sendMessage();
}
tfInput.setText(null);
}
}
}
public static void main(String[] args) {
new MultipleChatClient();
}
}