Advertisement
TermSpar

Java CLIENT Test

Mar 22nd, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 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.  
  7. public class CLIENT extends JFrame {
  8.    
  9.     //JAVA NETWORK CHAT BY BEN BOLLINGER
  10.    
  11.     private JTextField userText;
  12.     private JTextArea chatWindow;
  13.     private ObjectOutputStream output;
  14.     private ObjectInputStream input;
  15.     private String message = "";
  16.     private String serverIP;
  17.     private Socket connection;
  18.    
  19.     //CONSTRUCTOR
  20.     public CLIENT(String host){
  21.         super("CLIENT TEST");
  22.         serverIP = host;
  23.         userText = new JTextField();
  24.         userText.setEditable(false);
  25.         userText.addActionListener(
  26.             new ActionListener(){
  27.                 public void actionPerformed(ActionEvent event){
  28.                     sendMessage(event.getActionCommand());
  29.                     userText.setText("");
  30.                 }
  31.             }
  32.         );
  33.         add(userText, BorderLayout.NORTH);
  34.         chatWindow = new JTextArea();
  35.         add(new JScrollPane(chatWindow), BorderLayout.CENTER);
  36.         setSize(300,150);
  37.         setVisible(true);
  38.     }
  39.    
  40.     //RUN CONNECTION
  41.     public void startRunning(){
  42.         try{
  43.             connectToServer();
  44.             setupStreams();
  45.             whileChatting();
  46.         }catch(EOFException eofException){
  47.             showMessage("\n CLIENT TERMINATED CONNECTION");
  48.         }catch(IOException ioException){
  49.             ioException.printStackTrace();
  50.         }finally{
  51.             closeSocket();
  52.         }
  53.     }
  54.    
  55.     //CONNECT TO SERVER
  56.     private void connectToServer() throws IOException{
  57.         showMessage("ATTEMPTING CONNECTION... \n");
  58.         connection = new Socket(InetAddress.getByName(serverIP), 6789);
  59.         showMessage(" CONNECTED TO " + connection.getInetAddress().getHostName()); 
  60.     }
  61.    
  62.     //SETUP STREAMS
  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 NOW READY \n");
  68.     }
  69.    
  70.     //WHILE CHATTING
  71.     private void whileChatting() throws IOException{
  72.         ableToType(true);
  73.         do{
  74.             try{
  75.                 message = (String) input.readObject();
  76.                 showMessage("\n" + message);
  77.             }catch(ClassNotFoundException classNotFoundException){
  78.                 showMessage("\n ERROR: UNKOWN OBJECT TYPE");
  79.             }
  80.         }while(!message.equals("SERVER - END"));
  81.     }
  82.    
  83.     //CLOSE STREAMS AND SOCKETS
  84.     private void closeSocket(){
  85.         showMessage("\n CLOSING STREAMS AND SOCKETS...");
  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.     //SEND MESSAGES
  97.     private void sendMessage(String message){
  98.         try{
  99.             output.writeObject("CLIENT - " + message);
  100.             output.flush();
  101.             showMessage("\nCLIENT - " + message);
  102.         }catch(IOException ioException){
  103.             chatWindow.append("\n ERROR: UNKNOWN SENDING ERROR");
  104.         }
  105.     }
  106.    
  107.     //UPDATE CHAT WINDOW
  108.     private void showMessage(final String m){
  109.         SwingUtilities.invokeLater(
  110.             new Runnable(){
  111.                 public void run(){
  112.                     chatWindow.append(m);
  113.                     chatWindow.setEditable(false);
  114.                 }
  115.             }
  116.         );
  117.     }
  118.    
  119.     //ALLOW USER TO TYPE
  120.     private void ableToType(final boolean tof){
  121.         SwingUtilities.invokeLater(
  122.             new Runnable(){
  123.                 public void run(){
  124.                     userText.setEditable(tof);
  125.                 }
  126.             }
  127.         );
  128.     }
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement