Simple Chat Demo - (server to client)

Server.java
~~~~~~~~~~
package com.newChat.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
* message program server side, create server listen on special port
*
*/
public class Server {
public static void main(String[] args) {

   try {

    /*
    * create UI window
    */
    UI ui = new UI();
    ui.setTitle("server");
    ui.setVisible(true);

    /*
    * create server socket wait client connect
    */
    ServerSocket ss = new ServerSocket(8000);
    Socket socket = ss.accept();

    // /////////////////////////////////////////////////
    /*
    * common access socket method, get output stream
    */
    OutputStream os = socket.getOutputStream();
    PrintStream ps = new PrintStream(os);

    ui.setStream(ps);

    /*
    * get input stream
    */
    InputStream is = socket.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader reader = new BufferedReader(isr);

    /*
    * get remote address
    */
    InetSocketAddress address = (InetSocketAddress) socket
      .getRemoteSocketAddress();
    String host = address.getHostName();

    /*
    * get message and show it in GUI window
    */
    String line;
    while ((line = reader.readLine()) != null) {
     ui.append("From: " + host + "\nMessage: " + line);
    }

   } catch (IOException e) {
    e.printStackTrace();
   }
}
}



Client.java
~~~~~~~~~~~
package com.newChat.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;


/**
* message program client side create client
*
*/
public class Client {
public static void main(String[] args) {

   /*
   * create UI window
   */
   UI ui = new UI();
   ui.setTitle("client");
   ui.setVisible(true);

   try {
    /*
    * create client socket connect to server
    */
    Socket socket = new Socket("127.0.0.1", 8000);

    // /////////////////////////////////////////////////
    /*
    * common access socket method, get output stream
    */
    OutputStream os = socket.getOutputStream();
    PrintStream ps = new PrintStream(os);
    ui.setStream(ps);

    /*
    * get input stream
    */
    InputStream is = socket.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader reader = new BufferedReader(isr);

    /*
    * get remote address
    */
    InetSocketAddress address = (InetSocketAddress) socket
      .getRemoteSocketAddress();
    String host = address.getHostName();

    /*
    * get message and show it in GUI window
    */
    String line;
    while ((line = reader.readLine()) != null) {
     ui.append("From: " + host + ": Message: " + line);
    }

   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}
}


UI.java
~~~~~~~
package com.newChat.test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * UI window for message program, construct GUI window
 *
 *
 */
public class UI extends JFrame implements ActionListener {
    private JTextArea textArea;
    private JTextField textField;
    private JButton button;
    private PrintStream stream;

    /*
     * construct method
     */
    public UI() {
        init();
    }

    /*
     * Create Message GUI Window
     */
    private void init() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(200, 200, 400, 300);

        /*
         * add textArea for show
         */
        textArea = new JTextArea();
        textArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);

        /*
         * add send text field button
         */
        JPanel panel = new JPanel(new BorderLayout());
        add(panel, BorderLayout.SOUTH);

        textField = new JTextField();
        panel.add(textField);

        button = new JButton("Send");
        button.addActionListener(this);
        panel.add(button, BorderLayout.EAST);

    }

    public void actionPerformed(ActionEvent e) {

        /*
         * append send message to textArea clear send text field
         */
        String text = textField.getText();
        textArea.append("Send: ");
        textArea.append(text);
        textArea.append("\n");
        textArea.setAutoscrolls(true);

        textField.setText("");
        textField.requestFocus();

        /*
         * send text to remote
         */
        stream.println(text);
    }

    /*
     * get outputStream
     */
    public void setStream(PrintStream stream) {
        this.stream = stream;
    }

    /*
     * append get message to textArea
     */
    public void append(String text) {
        textArea.append("Get: ");
        textArea.append(text);
        textArea.append("\n");
    }

    /*
     * main method test GUI use
     */
    public static void main(String[] args) {
        new UI().setVisible(true);
    }
}

No comments:

Post a Comment