Advertisement
strikero

java server

Mar 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. package javaserver;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.util.ArrayList;
  8. import javax.swing.*;
  9.  
  10. public class JavaServer extends JFrame {
  11.  
  12. private JTextField userText;
  13. private JTextArea chatWindow;
  14. private ServerSocket server;
  15. private int clientNo = 0;
  16. private ArrayList<Connection> AllConnections = new ArrayList<Connection>();
  17.  
  18. //constructor
  19. public JavaServer(){
  20. super("Messenger");
  21. userText = new JTextField();
  22. userText.addActionListener(
  23. new ActionListener(){
  24. @Override
  25. public void actionPerformed(ActionEvent e) {
  26.  
  27. showMessage("\nSERVER - " + e.getActionCommand());
  28.  
  29. for (int i = 0; i < AllConnections.size(); i++) {
  30. try {
  31. AllConnections.get(i).output.writeObject("SERVER - " + e.getActionCommand());
  32. AllConnections.get(i).output.flush();
  33. } catch (IOException ex) {
  34. showMessage("\nError occured in sending message");
  35. }
  36. }
  37. userText.setText("");
  38. }
  39. }
  40. );
  41. add(userText, BorderLayout.NORTH);
  42. chatWindow = new JTextArea();
  43. add(new JScrollPane(chatWindow));
  44. setSize(300,150);
  45. setVisible(true);
  46. }
  47.  
  48. public void startRunning(){
  49. try{
  50. server = new ServerSocket(6789, 100);
  51. while(true){
  52. showMessage("\nWaiting for a connection");
  53. Socket newConnection = server.accept();
  54. try{
  55. AllConnections.add(new Connection(newConnection));
  56. clientNo++;
  57.  
  58. }catch(IOException iOException){
  59. showMessage("\n Error: A client failed to connect");
  60. }
  61. }
  62. }catch(IOException ioException){
  63. ioException.printStackTrace();
  64. }
  65. }
  66.  
  67. private class Connection extends Thread{
  68.  
  69. private int ClientID = clientNo;
  70. private ObjectOutputStream output;
  71. private ObjectInputStream input;
  72. private Socket clientSocket;
  73.  
  74. //Creates a thread for the client
  75. Connection(Socket clientThread) throws IOException{
  76. clientSocket = clientThread;
  77. output = new ObjectOutputStream(clientThread.getOutputStream());
  78. output.flush();
  79. input = new ObjectInputStream(clientThread.getInputStream());
  80. showMessage("\n Stream are now setup!");
  81.  
  82.  
  83. start();
  84. }
  85.  
  86. //Starts conversation
  87. @Override
  88. public void run(){
  89. String message = "";
  90. showMessage("\nCLIENT " + ClientID + " - has joined the server");
  91.  
  92. do{
  93. try{
  94. message = (String) input.readObject();
  95. //Display message to server
  96. showMessage("\nCLIENT " + ClientID + " - " + message);
  97.  
  98. //Send message to all clients
  99. sendMessage(ClientID, message);
  100.  
  101. }catch(ClassNotFoundException classNotFoundException){
  102. showMessage("\n idk what that user send!");
  103. }catch(EOFException ex){
  104. //Client properly closed connection
  105. break;
  106. }catch(SocketException ex){
  107. //Client prematurely closed connection
  108. break;
  109. }catch (IOException ex) {
  110. ex.printStackTrace();
  111. }
  112. }while(!message.equals("END"));
  113.  
  114. showMessage("\nCLIENT " + ClientID + " - has left the connection");
  115. //When chat is over. close all connections.
  116. try {
  117. output.close();
  118. input.close();
  119. clientSocket.close();
  120. AllConnections.remove(this);
  121. } catch (IOException ex) {
  122. ex.printStackTrace();
  123. }
  124. }
  125. }
  126.  
  127. //updates chatWindow
  128. private void showMessage(final String text){
  129. SwingUtilities.invokeLater(
  130. new Runnable(){
  131. public void run(){
  132. chatWindow.append(text);
  133. }
  134. }
  135. );
  136. }
  137.  
  138. //Send message to all clients
  139. private void sendMessage(int ClientID, final String text){
  140. for (int i = 0; i < AllConnections.size(); i++) {
  141. try {
  142. AllConnections.get(i).output.writeObject(text);
  143. AllConnections.get(i).output.flush();
  144. } catch (IOException ex) {
  145. showMessage("\nError occured in sending message");
  146. }
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement