Advertisement
TermSpar

Java SERVER Test

Mar 22nd, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. public class SERVER extends JFrame {
  7.  
  8.     //JAVA NETWORK CHAT BY BEN BOLLINGER
  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.    
  19.     public SERVER(){
  20.         super("SERVER TEST");
  21.         userText = new JTextField();
  22.         userText.setEditable(false);
  23.         userText.addActionListener(
  24.                 new ActionListener(){
  25.                     public void actionPerformed(ActionEvent event){
  26.                         sendMessage(event.getActionCommand()); //send data from text
  27.                         userText.setText(""); //reset text
  28.                     }
  29.                 }
  30.             );
  31.             add(userText, BorderLayout.NORTH);
  32.             chatWindow = new JTextArea();
  33.             add(new JScrollPane(chatWindow));
  34.             setSize(300,150);
  35.             setVisible(true);
  36.         }
  37.    
  38.     //SET UP AND RUN SERVER
  39.    
  40.     public void startRunning(){
  41.         try{
  42.             server = new ServerSocket(6789, 100); //set port to 6789, and set limit to 100
  43.             while(true){
  44.                 try{
  45.                     waitForConnection();
  46.                     setupStreams();
  47.                     whileChatting();
  48.                 }catch(EOFException eofException){
  49.                     showMessage("\n SERVER ENDED THE CONNECTION! ");
  50.                 }finally{
  51.                     closeChat();
  52.                 }
  53.             }
  54.         }catch(IOException ioException){
  55.             ioException.printStackTrace();
  56.         }
  57.     }
  58.    
  59.     //WAIT FOR CONNECTION AND DISPLAY INFO
  60.     private void waitForConnection() throws IOException{
  61.         showMessage("WAITING FOR SOMEONE TO CONNECT... \n");
  62.         connection = server.accept(); //accept socket
  63.         showMessage(" NOW CONNECTED TO " + connection.getInetAddress().getHostName());
  64.     }
  65.    
  66.     //GET STREAM TO SEND + RECIEVE DATA
  67.     private void setupStreams() throws IOException{
  68.         output = new ObjectOutputStream(connection.getOutputStream());
  69.         output.flush();
  70.         input = new ObjectInputStream(connection.getInputStream());
  71.         showMessage("\n Streams are now setup! \n");
  72.     }
  73.    
  74.     //DURING CHAT CONVERSATION
  75.     private void whileChatting() throws IOException{
  76.         String message = " You are now connected! ";
  77.         sendMessage(message);
  78.         ableToType(true);
  79.         do{
  80.             try{
  81.                 message = (String) input.readObject();
  82.                 showMessage("\n" + message);
  83.             }catch(ClassNotFoundException classNotFoundException){
  84.                 showMessage("\n ERROR: OBJECT UNABLE TO CONVERT TO DATA.STRING");
  85.             }
  86.         }while(!message.equals("CLIENT - END"));
  87.     }
  88.    
  89.     //CLOSE STREAMS AND SOCKETS
  90.     private void closeChat(){
  91.         showMessage("\n Closing connections... \n");
  92.         ableToType(false);
  93.         try{
  94.             output.close();
  95.             input.close();
  96.             connection.close();
  97.         }catch(IOException ioException){
  98.             ioException.printStackTrace();
  99.         }
  100.     }
  101.    
  102.     //SEND MESSAGE TO CLIENT
  103.     private void sendMessage(String message){
  104.         try{
  105.             output.writeObject("SERVER - " + message);
  106.             output.flush();
  107.             showMessage("\nSERVER - " + message);
  108.         }catch(IOException ioException){
  109.             chatWindow.append("\n ERROR: UNABLE TO SEND MESSAGE");
  110.         }
  111.     }
  112.    
  113.     //UPDATE CHAT WINDOW
  114.     private void showMessage(final String text){
  115.         SwingUtilities.invokeLater(
  116.             new Runnable(){
  117.                 public void run(){
  118.                     chatWindow.setEditable(false);
  119.                     chatWindow.append(text);
  120.                 }
  121.             }
  122.         );
  123.     }
  124.    
  125.     //ALLOW USER TO TYPE
  126.     private void ableToType(final boolean tof){
  127.         SwingUtilities.invokeLater(
  128.             new Runnable(){
  129.                 public void run(){
  130.                     userText.setEditable(tof);
  131.                 }
  132.             }
  133.         );
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement