close

Server.java

import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author lupohsun
 */
public class Server extends JFrame{

    /**
     * @param args the command line arguments
     */
    private JTextArea jta = new JTextArea();

    public static void main(String[] args) {
        new Server();
        // TODO code application logic here
    }
    public Server() {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);

        setTitle("Server");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try {
            //creat a server socket
            ServerSocket serverSocket = new ServerSocket(8000);
            jta.append("Server started at "+ new Date()+ '\n');

            //listen for a connection request
            Socket connectToClient = serverSocket.accept();

            //creat data input and output streams
            DataInputStream isFromClient = new DataInputStream(connectToClient.getInputStream());
            DataOutputStream osToClient = new DataOutputStream(connectToClient.getOutputStream());

            while(true) {
                //receive radius from the client
                //double radius = isFromClient.readDouble();
                String input = isFromClient.readUTF();

                //compute area
                //double area = radius*radius*Math.PI;

                //send area back to client
                //osToClient.writeDouble(area);
                osToClient.writeUTF(input);

                jta.append("String receved from client: "+input+'\n');
                //jta.append("Area found: "+input+'\n');
            } //end while()
        } //end try
        catch(IOException ex) {
            System.err.println(ex);
        } //end catch
    } //end Server()
}
 
--------------------------------------------------------------------------------------------------
 
Client.java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author lupohsun
 */
public class Client extends JFrame implements ActionListener {
    //text field

    private JTextField jtf = new JTextField();
    //text area to display contents
    private JTextArea jta = new JTextArea();
    //IO stream
    DataOutputStream osToServer;
    DataInputStream isFromServer;

    public static void main(String[] args) {
        new Client();
    } //end main

    public Client() {
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(new JLabel("Enter String"), BorderLayout.WEST);
        p.add(jtf, BorderLayout.CENTER);
        jtf.setHorizontalAlignment(JTextField.RIGHT);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p, BorderLayout.NORTH);
        getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);

        jtf.addActionListener(this);

        setTitle("Client");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try {
            //creat socket to server
            Socket connectToServer = new Socket("localhost", 8000);

            //creat an input stream to receive data from server
            isFromServer = new DataInputStream(connectToServer.getInputStream());

            //creat an output stream to send data to the server
            osToServer = new DataOutputStream(connectToServer.getOutputStream());
        } //end try
        catch (IOException ex) {
            jta.append(ex.toString() + '\n');
        } //end catch
    } //end Client()

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if (e.getSource() instanceof JTextField) {
            try {
                //double radius = Double.parseDouble(jtf.getText().trim());

                //send radius to server
                osToServer.writeUTF(jtf.getText());
                osToServer.flush();

                //get area form server
                String input = isFromServer.readUTF();

                //display to the text area
                //jta.append("Radius is " + radius + "\n");
                jta.append("String received from the server is " + input + '\n');
            } //end try
            catch (IOException ex) {
                System.err.println(ex);
            } //end catch
        } //end if
    } //end actionPerformed(ActionEvent e)
} //end class
 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 lupohsunrock 的頭像
    lupohsunrock

    lupohsunrock的部落格

    lupohsunrock 發表在 痞客邦 留言(0) 人氣()