Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.regex.Pattern;
- import java.io.*;
- import javax.swing.JFrame;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class ChatServer implements Runnable {
- private ChatServerThread clients[] = new ChatServerThread[50];
- private ServerSocket serverSocket = null;
- private Thread t = null;
- private int noOfClients = 0;
- private JTextArea chatField;
- private JTextField typeField;
- public ChatServer(int port) {
- JFrame frame;
- typeField = new JTextField(160);
- chatField = new JTextArea();
- frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.setTitle("Server");
- frame.setSize(500, 300);
- frame.add(chatField, BorderLayout.CENTER);
- chatField.setEditable(false);
- frame.setVisible(true);
- try {
- System.out.println("Binding to port " + port + ", please wait ...");
- chatField.setText(chatField.getText() + "\nBinding to port " + port + ", please wait ...");
- serverSocket = new ServerSocket(port);
- System.out.println("serverSocket started: " + serverSocket);
- chatField.setText(chatField.getText() + "\nserverSocket started: " + serverSocket);
- start();
- } catch (IOException ioe) {
- chatField.setText(chatField.getText() + ("Can not bind to port " + port + ": " + ioe.getMessage()));
- System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
- }
- }
- public void run() {
- while (t != null) {
- try {
- System.out.println("Waiting for a client ...");
- chatField.setText(chatField.getText() + "\nWaiting for a client ...");
- addThread(serverSocket.accept());
- } catch (IOException ioe) {
- System.out.println("serverSocket accept error: " + ioe);
- stop();
- }
- }
- }
- public void start() {
- if (t == null) {
- t = new Thread(this);
- t.start();
- }
- }
- public void stop() {
- if (t != null) {
- t.stop();
- t = null;
- }
- }
- private int findClient(int ID) {
- for (int i = 0; i < noOfClients; i++)
- if (clients[i].getID() == ID)
- return i;
- return -1;
- }
- public synchronized void handle(int ID, String input) {
- ChatServerThread client = clients[findClient(ID)];
- String userName = null, password = null, message = null;
- if (input.contains(".register")) {
- System.out.println("Registering " + input);
- String parsedInfo[] = input.split(Pattern.quote(" "));
- userName = parsedInfo[1];
- password = parsedInfo[2];
- registerUser(userName, password);
- } else if (input.contains(".login")) {
- String parsedInfo[] = input.split(Pattern.quote(" "));
- userName = parsedInfo[1];
- password = parsedInfo[2];
- if (checkLogin(userName, password) == false) {
- System.out.println("Telling user " + userName + " to register");
- client.send(".register");
- }
- client.send("SERVER: Login successful, welcome " + userName);
- } else if (input.charAt(0) != '.') {
- System.out.println(input);
- String parsedInfo[] = input.split(Pattern.quote("☼"));
- userName = parsedInfo[0];
- password = parsedInfo[1];
- message = parsedInfo[2];
- if (message.equals(".bye")) {
- client.send("Disconnecting now ...");
- client.send(".bye");
- remove(ID);
- } else if (message.contains("fuck")) {
- client.send("Rude! Timeout!\nDisconnecting now ...");
- client.send(".bye");
- remove(ID);
- } else if (message.length() > 160) {
- client.send("Client " + ID + " - too prolix!!!\nDisconnecting now ...");
- client.send(".bye");
- remove(ID);
- } else {
- for (int i = 0; i < noOfClients; i++) {
- chatField.setText(chatField.getText() + "\n" + userName + ": " + message);
- clients[i].send(userName + ": " + message);
- System.out.println("Sending message");
- }
- }
- }
- }
- public void registerUser(String username, String password) {
- try (FileWriter fw = new FileWriter("user_pass.txt", true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter out = new PrintWriter(bw)) {
- out.println(username + ":" + password);
- } catch (IOException e) {
- // File writing/opening failed at some stage.
- }
- }
- public boolean checkLogin(String username, String password) {
- boolean valid = false;
- ArrayList<String> lines = new ArrayList<String>();
- String line;
- try {
- BufferedReader reader = new BufferedReader(new FileReader("user_pass.txt"));
- while ((line = reader.readLine()) != null) {
- lines.add(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- String[] users = new String[lines.size()];
- String[] pass = new String[lines.size()];
- for (int i = 0; i < lines.size(); i++) {
- String[] output = lines.get(i).split("\\:");
- users[i] = output[0];
- pass[i] = output[1];
- }
- for (int i = 0; i < lines.size(); i++) {
- System.out.println(users[i] + ":" + pass[i]);
- if (users[i].equals(username) && pass[i].equals(password)) {
- System.out.println("found a match");
- valid = true;
- break;
- }
- }
- return valid;
- }
- public synchronized void remove(int ID) {
- int pos = findClient(ID);
- if (pos >= 0) {
- ChatServerThread toTerminate = clients[pos];
- System.out.println("Removing client " + ID + " at " + pos);
- chatField.setText(chatField.getText() + "\nRemoving client " + ID + " at " + pos);
- if (pos < noOfClients - 1)
- for (int i = pos + 1; i < noOfClients; i++)
- clients[i - 1] = clients[i];
- noOfClients--;
- try {
- toTerminate.close();
- } catch (IOException ioe) {
- System.out.println("Error closing t: " + ioe);
- }
- toTerminate.stop();
- }
- }
- private void addThread(Socket socket) {
- if (noOfClients < clients.length) {
- System.out.println("Client accepted: " + socket);
- chatField.setText(chatField.getText() + "\nClient accepted: " + socket);
- clients[noOfClients] = new ChatServerThread(this, socket);
- try {
- clients[noOfClients].open();
- clients[noOfClients].start();
- noOfClients++;
- } catch (IOException ioe) {
- System.out.println("Error opening thread: " + ioe);
- }
- } else {
- chatField.setText(chatField.getText() + "\nClient refused: maximum " + clients.length + " reached.");
- System.out.println("Client refused: maximum " + clients.length + " reached.");
- }
- }
- public static void main(String args[]) {
- ChatServer serverSocket = null;
- if (args.length != 1)
- System.out.println("Usage: java ChatServer port");
- else {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- ChatServer serverSocket = new ChatServer(Integer.parseInt(args[0]));
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement