Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. public server_frame()
  2. {
  3. initComponents();
  4. }
  5.  
  6. @SuppressWarnings("unchecked")
  7. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  8. private void initComponents() {
  9.  
  10. jScrollPane1 = new javax.swing.JScrollPane();
  11. ta_chat = new javax.swing.JTextArea();
  12. b_start = new javax.swing.JButton();
  13. b_end = new javax.swing.JButton();
  14. b_users = new javax.swing.JButton();
  15. b_clear = new javax.swing.JButton();
  16. lb_name = new javax.swing.JLabel();
  17.  
  18. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  19. setTitle("Chat - Server's frame");
  20. setName("server"); // NOI18N
  21. setResizable(false);
  22.  
  23. ta_chat.setColumns(20);
  24. ta_chat.setRows(5);
  25. jScrollPane1.setViewportView(ta_chat);
  26.  
  27. b_start.setText("START");
  28. b_start.addActionListener(new java.awt.event.ActionListener() {
  29. public void actionPerformed(java.awt.event.ActionEvent evt) {
  30. b_startActionPerformed(evt);
  31. }
  32. });
  33.  
  34. b_end.setText("END");
  35. b_end.addActionListener(new java.awt.event.ActionListener() {
  36. public void actionPerformed(java.awt.event.ActionEvent evt) {
  37. b_endActionPerformed(evt);
  38. }
  39. });
  40.  
  41. b_users.setText("Online Users");
  42. b_users.addActionListener(new java.awt.event.ActionListener() {
  43. public void actionPerformed(java.awt.event.ActionEvent evt) {
  44. b_usersActionPerformed(evt);
  45. }
  46. });
  47.  
  48. b_clear.setText("Clear");
  49. b_clear.addActionListener(new java.awt.event.ActionListener() {
  50. public void actionPerformed(java.awt.event.ActionEvent evt) {
  51. b_clearActionPerformed(evt);
  52. }
  53. });
  54.  
  55. lb_name.setText("TechWorld3g");
  56. lb_name.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  57.  
  58. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  59. getContentPane().setLayout(layout);
  60. layout.setHorizontalGroup(
  61. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62. .addGroup(layout.createSequentialGroup()
  63. .addContainerGap()
  64. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  65. .addComponent(jScrollPane1)
  66. .addGroup(layout.createSequentialGroup()
  67. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  68. .addComponent(b_end, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  69. .addComponent(b_start, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
  70. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 291, Short.MAX_VALUE)
  71. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  72. .addComponent(b_clear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  73. .addComponent(b_users, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))))
  74. .addContainerGap())
  75. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  76. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  77. .addComponent(lb_name)
  78. .addGap(209, 209, 209))
  79. );
  80. layout.setVerticalGroup(
  81. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82. .addGroup(layout.createSequentialGroup()
  83. .addContainerGap()
  84. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
  85. .addGap(18, 18, 18)
  86. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  87. .addComponent(b_start)
  88. .addComponent(b_users))
  89. .addGap(18, 18, 18)
  90. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  91. .addComponent(b_clear)
  92. .addComponent(b_end))
  93. .addGap(4, 4, 4)
  94. .addComponent(lb_name))
  95. );
  96.  
  97. pack();
  98. }// </editor-fold>
  99.  
  100. private void b_endActionPerformed(java.awt.event.ActionEvent evt) {
  101. try
  102. {
  103. Thread.sleep(5000); //5000 milliseconds is five second.
  104. }
  105. catch(InterruptedException ex) {Thread.currentThread().interrupt();}
  106.  
  107. tellEveryone("Server:is stopping and all users will be disconnected.\n:Chat");
  108. ta_chat.append("Server stopping... \n");
  109.  
  110. ta_chat.setText("");
  111. }
  112.  
  113. private void b_startActionPerformed(java.awt.event.ActionEvent evt) {
  114. Thread starter = new Thread(new ServerStart());
  115. starter.start();
  116.  
  117. ta_chat.append("Server started...\n");
  118. }
  119.  
  120. private void b_usersActionPerformed(java.awt.event.ActionEvent evt) {
  121. ta_chat.append("\n Online users : \n");
  122. for (String current_user : users)
  123. {
  124. ta_chat.append(current_user);
  125. ta_chat.append("\n");
  126. }
  127.  
  128. }
  129.  
  130. private void b_clearActionPerformed(java.awt.event.ActionEvent evt) {
  131. ta_chat.setText("");
  132. }
  133.  
  134. public static void main(String args[])
  135. {
  136. java.awt.EventQueue.invokeLater(new Runnable()
  137. {
  138. @Override
  139. public void run() {
  140. new server_frame().setVisible(true);
  141. }
  142. });
  143. }
  144.  
  145. public class ServerStart implements Runnable
  146. {
  147. @Override
  148. public void run()
  149. {
  150. clientOutputStreams = new ArrayList();
  151. users = new ArrayList();
  152.  
  153. try
  154. {
  155. ServerSocket serverSock = new ServerSocket(2222);
  156.  
  157. while (true)
  158. {
  159. Socket clientSock = serverSock.accept();
  160. PrintWriter writer = new PrintWriter(clientSock.getOutputStream());
  161. clientOutputStreams.add(writer);
  162.  
  163. Thread listener = new Thread(new ClientHandler(clientSock, writer));
  164. listener.start();
  165. ta_chat.append("Got a connection. \n");
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. ta_chat.append("Error making a connection. \n");
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement