Saturday, March 5, 2011

Java - Public chat using GUI

Server.java

import java.io.*;
import java.net.*;
import java.util.*;

public class Server implements Runnable {
ServerSocket serverSocket = null;
Socket socket = null;
ArrayList clientHandlerList = new ArrayList();
ClientHandler clientHandler = null;

public Server() {
try {
serverSocket = new ServerSocket(4444);
new Thread(this).start();
} catch(Exception exp) {
exp.printStackTrace();
}
}

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 sourceClient) {
ClientHandler client;
for(int i = 0; i < clientHandlerList.size() ; i++ ) {
client = (ClientHandler) clientHandlerList.get(i);
if (client != sourceClient) {
client.sendMessage(message);
}
}

}

public static void main(String[] args) {
new Server();
}
}


ClientHandler.java


import java.io.*;
import java.net.*;

public class ClientHandler implements Runnable {
Socket socket = null;
Server server = null;
BufferedReader readFromClient = null;
PrintWriter writeToClient = null;

public ClientHandler(Socket socket, Server server) {
try {
this.socket = socket;
this.server = server;
readFromClient = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
writeToClient = new PrintWriter(socket.getOutputStream());

new Thread(this).start();
} catch(IOException exp) {
exp.printStackTrace();
}
}

public void run() {
try {
while(true) {
String message = readFromClient.readLine();
server.broadcast(message, this);
}
} 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, ActionListener {
Socket socket = null;
BufferedReader readFromClient = null;
BufferedReader readFromServer = null;
PrintWriter writeToServer = null;
JTextField chatField = null;
JTextArea chatArea = null;
JButton sendButton = null;

public Client() {
try {
initComponents();
socket = new Socket("localhost", 4444);

readFromServer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
writeToServer = new PrintWriter(socket.getOutputStream());
readFromClient = new BufferedReader(
new InputStreamReader(System.in));

new Thread(this).start();


} catch(UnknownHostException exp) {
exp.printStackTrace();
} catch(IOException 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");
writeToServer.println(message);
writeToServer.flush();
}

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:

Post a Comment

Contact Form

Name

Email *

Message *