Server.java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Server extends JFrame implements Runnable, ActionListener {
ServerSocket serverSocket = null;
Socket socket = null;
ArrayList clientHandlerList = new ArrayList();
ClientHandler clientHandler = null;
JTextField chatField = null;
JTextArea chatArea = null;
JButton sendButton = null;
public Server() {
try {
initComponents();
serverSocket = new ServerSocket(4444);
new Thread(this).start();
} catch(Exception exp) {
exp.printStackTrace();
}
}
public void initComponents() {
setSize(270,507);
setResizable(false);
setLayout(null);
chatArea = new JTextArea();
chatField = new JTextField(20);
chatArea.setForeground(Color.BLACK);
sendButton = new JButton("SEND");
chatArea.setEditable(false);
chatArea.setBounds(5,5,250,400);
chatField.setBounds(5,410,250,30);
sendButton.setBounds(175,450,80,20);
sendButton.addActionListener(this);
chatField.addKeyListener( new KeyAdapter() {
public void keyReleased(KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
sendButtonAction();
}
}
});
add(chatArea);
add(chatField);
add(sendButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(this);
}
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == sendButton) {
sendButtonAction();
}
}
public void sendButtonAction() {
String message = chatField.getText();
chatField.setText("");
chatArea.append(message + "\n");
broadcast(message);
}
public void run() {
try {
while(true) {
socket = serverSocket.accept();
clientHandler = new ClientHandler(socket, this);
clientHandlerList.add(clientHandler);
}
} catch(Exception exp) {
exp.printStackTrace();
}
}
public void broadcast(String message) {
ClientHandler client;
for(int i = 0; i < clientHandlerList.size() ; i++ ) {
client = (ClientHandler) clientHandlerList.get(i);
client.sendMessage(message);
}
}
public static void main(String[] args) {
new Server().setVisible(true);
}
}
ClientHandler.java
import java.io.*;
import java.net.*;
public class ClientHandler {
Socket socket = null;
Server server = null;
PrintWriter writeToClient = null;
public ClientHandler(Socket socket, Server server) {
try {
this.socket = socket;
this.server = server;
writeToClient = new PrintWriter(socket.getOutputStream());
} catch(IOException exp) {
exp.printStackTrace();
}
}
public void sendMessage(String message) {
writeToClient.println(message);
writeToClient.flush();
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Client extends JFrame implements Runnable {
Socket socket = null;
BufferedReader readFromServer = null;
JTextArea chatArea = null;
public Client() {
try {
initComponents();
socket = new Socket("localhost", 4444);
readFromServer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
new Thread(this).start();
} catch(UnknownHostException exp) {
exp.printStackTrace();
} catch(IOException exp) {
exp.printStackTrace();
}
}
public void initComponents() {
setSize(270,480);
setResizable(false);
setLayout(null);
chatArea = new JTextArea();
chatArea.setForeground(Color.BLACK);
chatArea.setEditable(false);
chatArea.setBounds(5,5,250,400);
add(chatArea);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(this);
}
public void run() {
try {
while(true) {
String message = readFromServer.readLine();
chatArea.append(message + "\n");
}
} catch(IOException ioExp) {
ioExp.printStackTrace();
}
}
public static void main(String[] args) {
new Client().setVisible(true);
}
}
0 Comments