1. 程式人生 > >Java Socket應用(三)多執行緒實現多客戶端的通訊

Java Socket應用(三)多執行緒實現多客戶端的通訊

伺服器執行緒處理類ServerThread.java :

package com.yijia;

import java.io.*;
import java.net.Socket;

/**
 * 建立時間:2018/10/4 14:59
 * 作者:
 * 郵箱:[email protected]
 * <p>
 * 伺服器執行緒處理類
 */
public class ServerThread extends Thread {
    //和本執行緒相關的Socket
    Socket socket = null;

    //構造方法
    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    //執行緒執行的操作,響應客戶端的請求
    @Override
    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        OutputStream os = null;
        PrintWriter pw = null;
        try {
            //獲取輸入流,讀取客戶端資訊
            is = socket.getInputStream();//位元組輸入流
            isr = new InputStreamReader(is);//將位元組流轉換為字元流
            br = new BufferedReader(isr);//為輸入流新增緩衝
            String info = null;
            while ((info = br.readLine()) != null) {//迴圈讀取客戶端的資訊
                System.out.println("我是伺服器,客戶端說:" + info);
            }
            socket.shutdownInput();//關閉輸入流

            //獲取輸出流,響應客戶端的請求
            os = socket.getOutputStream();
            pw = new PrintWriter(os);//包裝為列印流

            pw.write("歡迎您!");
            pw.flush();//呼叫flush()方法將緩衝輸出


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關閉資源
            try {
                if (pw != null)
                    pw.close();
                if (os != null)
                    os.close();
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (is != null)
                    is.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

 修改伺服器端程式碼Service.java:

package com.yijia;

import java.beans.IndexedPropertyChangeEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 建立時間:2018/10/4 12:25
 * 作者:
 * 郵箱:[email protected]
 * <p>
 * 基於TCP協議的Socket通訊,實現使用者登入
 * 伺服器端
 */
public class Service {
    public static void main(String[] args) {

        try {
            //建立一個伺服器端Socket,即ServiceSocket,指定繫結的埠,並監聽此埠
            ServerSocket serviceSocket = new ServerSocket(8888);
            Socket socket = null;
            //記錄客戶端的數量
            int count = 0;
            System.out.println("***伺服器即將啟動,等待客戶端的連線***");

            //迴圈監聽等待客戶端的連線
            while (true) {
                //呼叫accept()方法開始監聽,等待客戶端的連線
                socket = serviceSocket.accept();
                //建立一個新的執行緒
                ServerThread serverThread = new ServerThread(socket);
                //啟動執行緒
                serverThread.start();

                count++;//統計客戶端的數量
                System.out.println("客戶端的數量:" + count);
                InetAddress address = socket.getInetAddress();
                System.out.println("當前客戶端的IP:"+address.getHostAddress());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}