Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. public class ServerChat extends Thread implements ActionListener {
  2.  
  3. private Frame mainFrame;
  4. private Panel controlPanel;
  5. private Button sendButton;
  6. private JTextArea resultArea;
  7. private JTextField writeMessage;
  8. PersonList personList = new PersonList();
  9. String name ;
  10.  
  11. private static int port = 9000;
  12. private static ServerSocket server;
  13. private static Socket conn;
  14.  
  15. public ServerChat() throws UnknownHostException, IOException {
  16. // The programme recognize the user who's logged in and get his name
  17. name=personList.getPersonFromIndex(personList.getPersonLogged()).getName() +" "+personList.getPersonFromIndex(personList.getPersonLogged()).getSurname() ;
  18.  
  19. mainFrame = new Frame("Chat - SERVER area");
  20. mainFrame.setSize(500, 500);
  21. mainFrame.setLayout(new GridLayout(1, 2));
  22. mainFrame.addWindowListener(new WindowAdapter() {
  23. public void windowClosing(WindowEvent windowEvent) {
  24. System.exit(0);
  25. }
  26. });
  27. Border border = BorderFactory.createLineBorder(Color.BLACK);
  28. Dimension d = new Dimension();
  29. d.equals(mainFrame);
  30. resultArea = new JTextArea(20,40);
  31.  
  32. JScrollPane scrollingArea = new JScrollPane(resultArea);
  33. JLabel lblMessage = new JLabel("MESSAGE: ");
  34. lblMessage.setForeground(Color.BLACK);
  35. writeMessage = new JTextField(40);
  36. writeMessage.setBackground(Color.white);
  37. writeMessage.setBorder(new LineBorder(Color.BLACK, 1));
  38.  
  39. sendButton = new Button();
  40. sendButton.setLabel("SEND");
  41. sendButton.setFont(new Font("Tahoma", 1, 13));
  42.  
  43.  
  44. controlPanel = new Panel();
  45. controlPanel.setLayout(new FlowLayout());
  46.  
  47. controlPanel.add(scrollingArea, border);
  48. controlPanel.add(lblMessage);
  49. controlPanel.add(writeMessage);
  50. controlPanel.add(sendButton);
  51.  
  52. mainFrame.add(controlPanel);
  53. mainFrame.setResizable(false);
  54. mainFrame.setVisible(true);
  55.  
  56. sendButton.setActionCommand("SEND");
  57. sendButton.addActionListener(this);
  58.  
  59. server = new ServerSocket(port, 1, InetAddress.getLocalHost());
  60.  
  61. conn = server.accept();
  62.  
  63. while (true) {
  64. try {
  65. DataInputStream dis = new DataInputStream(conn.getInputStream());
  66. String string = dis.readUTF();
  67.  
  68. if (string != " " && string != null && !string.isEmpty()) {
  69. resultArea.append(string + "n");
  70. }
  71. } catch (Exception e1) {
  72. try {
  73. Thread.sleep(3000);
  74. System.exit(0);
  75. } catch (InterruptedException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80. }
  81.  
  82. public static void main(String[] args) throws IOException {
  83. // TODO code application logic here
  84. ServerChat swingControlDemo = new ServerChat();
  85. }
  86.  
  87. public void actionPerformed(ActionEvent e) {
  88. String comand = sendButton.getActionCommand();
  89.  
  90. if ((comand == "SEND") && (writeMessage.getText() != " ")) {
  91. try {
  92. DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
  93. dos.writeUTF(name + " : "+writeMessage.getText());
  94. resultArea.append(name +" : "+ writeMessage.getText());
  95. } catch (Exception e1) {
  96. writeMessage.setText("There is an error in connection.");
  97. JOptionPane.showMessageDialog(null, "There is error in something...");
  98.  
  99. try {
  100. Thread.sleep(3000);
  101. System.exit(0);
  102. } catch (InterruptedException e2) {
  103. e2.printStackTrace();
  104. }
  105. }
  106. writeMessage.setText("");
  107. }
  108. }
  109.  
  110. public class ClientChat implements ActionListener {
  111. private Frame mainFrame;
  112. private Panel controlPanel;
  113. private Button sendButton;
  114. private JTextArea resultArea;
  115. private JTextField writeMessage;
  116.  
  117. private static int port = 9000;
  118. private String sName = "localhost";
  119. private Socket conn;
  120. PersonList personList = new PersonList();
  121. String name ;
  122.  
  123.  
  124. public ClientChat() throws IOException {
  125. name= personList.getPersonFromIndex(personList.getPersonLogged()).getName() +" "+personList.getPersonFromIndex(personList.getPersonLogged()).getSurname() ;
  126. mainFrame = new Frame("Chat - CLIENT area");
  127. mainFrame.setSize(500, 500);
  128. mainFrame.setLayout(new GridLayout(1, 2));
  129. mainFrame.addWindowListener(new WindowAdapter() {
  130. public void windowClosing(WindowEvent windowEvent) {
  131. System.exit(0);
  132. }
  133. });
  134.  
  135. Border border = BorderFactory.createLineBorder(Color.BLACK);
  136. resultArea = new JTextArea(20,40);
  137.  
  138. JScrollPane scrollingArea = new JScrollPane(resultArea);
  139.  
  140. JLabel lblMessage = new JLabel("MESSAGE: ");
  141. lblMessage.setForeground(Color.BLACK);
  142.  
  143. writeMessage = new JTextField(37);
  144. writeMessage.setBackground(Color.LIGHT_GRAY);
  145. writeMessage.setBorder(new LineBorder(Color.BLACK, 1));
  146.  
  147. sendButton = new Button();
  148. sendButton.setLabel("SEND");
  149. sendButton.setFont(new Font("Tahoma", 1, 13));
  150.  
  151. controlPanel = new Panel();
  152. controlPanel.setLayout(new FlowLayout());
  153.  
  154. controlPanel.add(scrollingArea, border);
  155. controlPanel.add(lblMessage);
  156. controlPanel.add(writeMessage);
  157. controlPanel.add(sendButton);
  158.  
  159. mainFrame.add(controlPanel);
  160. mainFrame.setResizable(false);
  161. mainFrame.setBounds(620, 1, 600, 600);
  162. mainFrame.setVisible(true);
  163.  
  164. sendButton.setActionCommand("SEND");
  165. sendButton.addActionListener(this);
  166.  
  167. try {
  168. conn = new Socket(InetAddress.getLocalHost(), port);
  169. } catch (Exception e) {
  170. resultArea.setText("The server is not up.");
  171. //JOptionPane.showMessageDialog(null, "There is error in something...");
  172. }
  173.  
  174. while (true) {
  175. try {
  176. DataInputStream dis = new DataInputStream(conn.getInputStream());
  177. String string = dis.readUTF(); //it will read the data from dis
  178.  
  179.  
  180.  
  181. if (string != " " && string != null && !string.isEmpty()) {
  182. resultArea.append(string + "n");
  183. }
  184.  
  185. } catch (Exception e1) {
  186. }
  187. }
  188. }
  189.  
  190. public static void main(String[] args) throws IOException {
  191. // TODO code application logic here
  192. char a;
  193. Scanner in = new Scanner(System.in);
  194. //a = in.n
  195.  
  196. ClientChat swingControlDemo = new ClientChat();
  197. }
  198.  
  199. public void actionPerformed(ActionEvent e) {
  200.  
  201. String comand = sendButton.getActionCommand();
  202.  
  203. if ((comand == "SEND") && (writeMessage.getText() != "")) {
  204. try {
  205. DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
  206. dos.writeUTF(name + " : "+writeMessage.getText());
  207. resultArea.append(name +" : "+ writeMessage.getText());
  208. } catch (Exception e1) {
  209.  
  210. try {
  211. Thread.sleep(3000);
  212. System.exit(0);
  213. } catch (InterruptedException e2) {
  214. e2.printStackTrace();
  215. }
  216. }
  217. writeMessage.setText("");
  218. }
  219. }
  220.  
  221. private void chatRoomButtonActionPerformed(java.awt.event.ActionEvent evt) {
  222. try {
  223. Thread r = new Thread(new ServerChat());
  224. r.start();
  225.  
  226. } catch (IOException ex) {
  227. Logger.getLogger(StaffMainPage.class.getName()).log(Level.SEVERE, null, ex);
  228. }
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement