Advertisement
Guest User

Untitled

a guest
Jan 6th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class second extends JFrame {
  9.  
  10. private JTextField userText;
  11. private JTextArea chatWindow;
  12. private ObjectOutputStream output;
  13. private ObjectInputStream input;
  14. private ServerSocket server;
  15. private Socket connection;
  16.  
  17. //constructor
  18. public second(){
  19. super("Instant Messenger");
  20. userText = new JTextField();
  21. userText.setEditable(false);
  22. userText.addActionListener(
  23. new ActionListener(){
  24. public void actionPerformed(ActionEvent event){
  25. sendMessage(event.getActionCommand());
  26. userText.setText("");
  27. }
  28. }
  29. );
  30. add(userText, BorderLayout.NORTH);
  31. chatWindow = new JTextArea();
  32. add(new JScrollPane(chatWindow));
  33. }
  34.  
  35. //method for starting up the server
  36.  
  37. public void startRunning(){
  38. try{
  39. server = new ServerSocket(6789, 100);
  40. while(true){
  41. try{
  42. waitForConnection();
  43. setupStreams();
  44. whileChatting();
  45.  
  46. }catch(EOFException eofException){
  47. showMessage("\n Server Ended Connection!");
  48. }finally{
  49. closeServer();
  50. }
  51. }
  52. }catch(IOException ioException){
  53. ioException.printStackTrace();
  54. }
  55. }
  56.  
  57. private void waitForConnection() throws IOException{
  58. showMessage(" Waiting for someone to connect... \n");
  59. connection = server.accept();
  60. showMessage(" Now connected to " + connection.getInetAddress().getHostName());
  61. }
  62.  
  63. private void setupStreams() throws IOException{
  64. output = new ObjectOutputStream(connection.getOutputStream());
  65. output.flush();
  66. input = new ObjectInputStream(connection.getInputStream());
  67. showMessage("\n Streams has been setup! \n");
  68. }
  69.  
  70. private void whileChatting() throws IOException{
  71. String message = " You are now connected !";
  72. sendMessage(message);
  73. ableToType(true);
  74. do{
  75. try{
  76. message = (String) input.readObject();
  77. showMessage("\n"+ message);
  78. }catch(ClassNotFoundException ClassNotFoundException){
  79. showMessage("\n YOU WHAT MATE?");
  80. }
  81. }while(!message.equals("CLIENT - END"));
  82. }
  83.  
  84. private void closeServer(){
  85. showMessage("\n Closing connections... \n");
  86. ableToType(false);
  87. try{
  88. output.close();
  89. input.close();
  90. connection.close();
  91. }catch(IOException ioException){
  92. ioException.printStackTrace();
  93. }
  94. }
  95.  
  96. private void sendMessage(String message){
  97. try{
  98. output.writeObject("SERVER - "+ message);
  99. output.flush();
  100. showMessage("\nSERVER- "+ message);
  101. }catch(IOException ioException){
  102. chatWindow.append("\n ERROR: YOU CAN'T TYPE THAT!");
  103. }
  104. }
  105.  
  106. private void showMessage(final String text){
  107. SwingUtilities.invokeLater(
  108. new Runnable(){
  109. public void run(){
  110. chatWindow.append(text);
  111. }
  112. }
  113.  
  114. );
  115. }
  116.  
  117. private void ableToType(final boolean tof){
  118. SwingUtilities.invokeLater(
  119. new Runnable(){
  120. public void run(){
  121. userText.setEditable(tof);
  122. }
  123. }
  124.  
  125. );
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement