Advertisement
Guest User

Untitled

a guest
May 6th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1.  
  2. package javaapplication12;
  3.  
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.*;
  7. import java.io.*;
  8. import java.net.*;
  9. import java.util.ArrayList;
  10.  
  11. public class Server extends JFrame implements ActionListener {
  12. static ServerSocket serversocket;
  13. static Socket socket;
  14. static int totalnum;
  15. JPanel panel;
  16. JTextField newmsg;
  17. JTextArea history;
  18. JButton stop;
  19. DataInputStream dis;
  20. DataOutputStream dos;
  21. boolean infiniteloop;
  22. Message msg;
  23. ArrayList<ConnectionHandler> al;
  24.  
  25.  
  26. public Server() throws Exception {
  27. panel = new JPanel();
  28. newmsg = new JTextField();
  29. history = new JTextArea();
  30. stop = new JButton("Stop");
  31. this.setSize(520,520);
  32. this.setVisible(true);
  33. setDefaultCloseOperation(EXIT_ON_CLOSE);
  34. panel.setLayout(null);
  35. this.add(panel);
  36. history.setBounds(20, 20, 450, 400);
  37. panel.add(history);
  38. newmsg.setBounds(20, 430, 340, 30);
  39. panel.add(newmsg);
  40. stop.setBounds(375, 430,95, 30);
  41. panel.add(stop);
  42. this.setTitle("Server");
  43. stop.addActionListener(this);
  44. infiniteloop = true;
  45. while(infiniteloop) {
  46. System.out.println(infiniteloop);
  47. serversocket = new ServerSocket(8080, 1, InetAddress.getLocalHost());
  48. history.setText("Server is Up, Running & Waiting....");
  49. socket = serversocket.accept();
  50. if (infiniteloop = false)
  51. {
  52. break;
  53. }
  54. ConnectionHandler handler = new ConnectionHandler(socket);
  55. al.add(handler);
  56. handler.start();
  57. history.append("\nClient Found");
  58.  
  59. } //while
  60.  
  61.  
  62. serversocket.close();
  63. for(int i = 0; i< al.size(); i++)
  64. {
  65. ConnectionHandler ch = al.get(i);
  66. ch.toClient.close();
  67. ch.fromClient.close();
  68. }
  69. System.out.println("Server shutdown");
  70.  
  71.  
  72. } //server()
  73.  
  74. public class ConnectionHandler extends Thread {
  75. Socket client;
  76. ObjectInputStream fromClient;
  77. ObjectOutputStream toClient;
  78. String username;
  79. String password;
  80. String valid;
  81. int num;
  82. ConnectionHandler(Socket socket) {
  83. totalnum++;
  84. num = totalnum;
  85. client = socket;
  86. }
  87. // RUN METHOD
  88. public void run() {
  89. try {
  90. fromClient = new ObjectInputStream(client.getInputStream());
  91. toClient = new ObjectOutputStream(client.getOutputStream());
  92. User user = (User) fromClient.readObject();
  93. history.append("\n Server is authenticating user " +user.getusername());
  94. toClient.writeObject(validation(user));
  95. toClient.flush();
  96. if(user.getvalid() == false)
  97. {
  98. client.close();
  99. history.append("Incorrect Username & Password");
  100. throw new Exception("Incorrect Username & Password");
  101. }
  102. else
  103. {
  104. history.append("\n" + user.getusername() + "has connected to the server.");
  105. }
  106. // WHEN CHATTING
  107. boolean chatting = true;
  108. while(chatting)
  109. {
  110. try {
  111. msg = (Message) fromClient.readObject();
  112. writemsg(msg);
  113.  
  114. }
  115. catch(IOException e) {
  116. history.append("Error in reading message" +e);
  117. break;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. }
  124.  
  125. }catch(Exception e)
  126. {
  127. System.out.println(e);
  128. }
  129.  
  130. // After everything, close everything
  131. try {
  132. fromClient.close();
  133. toClient.close();
  134. client.close();
  135. remove(num);
  136. }catch(Exception e) {}
  137.  
  138. } //run
  139.  
  140. public void writemsg(Message m) throws Exception{
  141. int target = m.gettarget();
  142. for (int i = 0; i < al.size(); i++) {
  143. ConnectionHandler ch = al.get(i);
  144. if (ch.num == target) {
  145. toClient.writeObject(m);
  146. }
  147. }
  148. toClient.flush();
  149. }
  150. } // connectionhandler
  151. public static void main(String[] args) throws Exception{
  152. new Server();
  153.  
  154. }
  155.  
  156. @Override
  157. public void actionPerformed(ActionEvent e) {
  158. try{
  159. serversocket.close();
  160.  
  161. }
  162. catch (Exception ex)
  163. {
  164. System.out.println(ex);
  165. }
  166. // System.exit(0);
  167. }
  168.  
  169. public User validation(User u) {
  170.  
  171. String username = u.getusername();
  172. String password = u.getpw();
  173. User user;
  174. if (username.equalsIgnoreCase("Distributed") && password.equals(123456))
  175. {
  176. user = new User(1,username,password,true);
  177. }
  178. else
  179. {
  180. user = new User(username,password,false);
  181. }
  182. return user;
  183. }
  184.  
  185.  
  186. public void remove(int removal)
  187. {
  188. for (int i = 0; i< al.size(); i++)
  189. {
  190. ConnectionHandler ch = al.get(i);
  191. if(ch.num == removal) {
  192. al.remove(i);
  193. return;
  194. }
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement