Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication12;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import java.io.*;
- import java.net.*;
- import java.util.ArrayList;
- public class Server extends JFrame implements ActionListener {
- static ServerSocket serversocket;
- static Socket socket;
- static int totalnum;
- JPanel panel;
- JTextField newmsg;
- JTextArea history;
- JButton stop;
- DataInputStream dis;
- DataOutputStream dos;
- boolean infiniteloop;
- Message msg;
- ArrayList<ConnectionHandler> al;
- public Server() throws Exception {
- panel = new JPanel();
- newmsg = new JTextField();
- history = new JTextArea();
- stop = new JButton("Stop");
- this.setSize(520,520);
- this.setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- panel.setLayout(null);
- this.add(panel);
- history.setBounds(20, 20, 450, 400);
- panel.add(history);
- newmsg.setBounds(20, 430, 340, 30);
- panel.add(newmsg);
- stop.setBounds(375, 430,95, 30);
- panel.add(stop);
- this.setTitle("Server");
- stop.addActionListener(this);
- infiniteloop = true;
- while(infiniteloop) {
- System.out.println(infiniteloop);
- serversocket = new ServerSocket(8080, 1, InetAddress.getLocalHost());
- history.setText("Server is Up, Running & Waiting....");
- socket = serversocket.accept();
- if (infiniteloop = false)
- {
- break;
- }
- ConnectionHandler handler = new ConnectionHandler(socket);
- al.add(handler);
- handler.start();
- history.append("\nClient Found");
- } //while
- serversocket.close();
- for(int i = 0; i< al.size(); i++)
- {
- ConnectionHandler ch = al.get(i);
- ch.toClient.close();
- ch.fromClient.close();
- }
- System.out.println("Server shutdown");
- } //server()
- public class ConnectionHandler extends Thread {
- Socket client;
- ObjectInputStream fromClient;
- ObjectOutputStream toClient;
- String username;
- String password;
- String valid;
- int num;
- ConnectionHandler(Socket socket) {
- totalnum++;
- num = totalnum;
- client = socket;
- }
- // RUN METHOD
- public void run() {
- try {
- fromClient = new ObjectInputStream(client.getInputStream());
- toClient = new ObjectOutputStream(client.getOutputStream());
- User user = (User) fromClient.readObject();
- history.append("\n Server is authenticating user " +user.getusername());
- toClient.writeObject(validation(user));
- toClient.flush();
- if(user.getvalid() == false)
- {
- client.close();
- history.append("Incorrect Username & Password");
- throw new Exception("Incorrect Username & Password");
- }
- else
- {
- history.append("\n" + user.getusername() + "has connected to the server.");
- }
- // WHEN CHATTING
- boolean chatting = true;
- while(chatting)
- {
- try {
- msg = (Message) fromClient.readObject();
- writemsg(msg);
- }
- catch(IOException e) {
- history.append("Error in reading message" +e);
- break;
- }
- }
- }catch(Exception e)
- {
- System.out.println(e);
- }
- // After everything, close everything
- try {
- fromClient.close();
- toClient.close();
- client.close();
- remove(num);
- }catch(Exception e) {}
- } //run
- public void writemsg(Message m) throws Exception{
- int target = m.gettarget();
- for (int i = 0; i < al.size(); i++) {
- ConnectionHandler ch = al.get(i);
- if (ch.num == target) {
- toClient.writeObject(m);
- }
- }
- toClient.flush();
- }
- } // connectionhandler
- public static void main(String[] args) throws Exception{
- new Server();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- try{
- serversocket.close();
- }
- catch (Exception ex)
- {
- System.out.println(ex);
- }
- // System.exit(0);
- }
- public User validation(User u) {
- String username = u.getusername();
- String password = u.getpw();
- User user;
- if (username.equalsIgnoreCase("Distributed") && password.equals(123456))
- {
- user = new User(1,username,password,true);
- }
- else
- {
- user = new User(username,password,false);
- }
- return user;
- }
- public void remove(int removal)
- {
- for (int i = 0; i< al.size(); i++)
- {
- ConnectionHandler ch = al.get(i);
- if(ch.num == removal) {
- al.remove(i);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement