close

import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
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 MultiThreadServer extends JFrame {

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

    public static void main(String[] args) throws IOException {
        new MultiThreadServer();
    // TODO code application logic here
    }

    public MultiThreadServer() throws IOException {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);

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

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

            //number of client
            int clientNo = 1;
            while (true) {
                //listen for a connection request
                Socket connectToClient = serverSocket.accept();

                //display the client number
                jta.append("Starting thread for client " + clientNo + " at " + new Date() + '\n');

                //find the client's host name,and IP address
                InetAddress clientInetAddress = connectToClient.getInetAddress();
                jta.append("Client " + clientNo + "'s host name is " + clientInetAddress.getHostName() + '\n');
                jta.append("Client " + clientNo + "'s IP Address is " + clientInetAddress.getHostAddress() + '\n');

                //create a new thread for the connection
                HandleAClient thread = new HandleAClient(connectToClient);

                //start a new thread
                thread.start();

                //increment clientNo
                clientNo++;
            } //end while
        } //end try
        catch (IOException ex) {
            System.err.println(ex);
        } //end catch
    } //end Server()

    //innrer class
    //define the thread class for handing new connection
    class HandleAClient extends Thread {
        //a connect socket

        private Socket connectToClient;

        //construct a thread
        public HandleAClient(Socket socket) {
            connectToClient = socket;
        } //end construct

        //run a thread
        public void run() {
            try {
                //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
                    String input = isFromClient.readUTF();

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

                    InetAddress clientInetAddress = connectToClient.getInetAddress();
                    jta.append("String receved from " + clientInetAddress.getHostAddress() + " : " + input + '\n');
                //jta.append("Area found: "+input+'\n');
                } //end while()
            } //end try
            catch (IOException e) {
                System.err.println(e);
            } //end catch
        } //end run
    } //end inner class
} //end class

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

    lupohsunrock的部落格

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