Socket初識-多線程實現多用戶登陸

一、ServerSocket

package com.video.video.common;

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

/**
 * 基於TCP協議的Socket通訊,實現用戶登陸
 * 服務器端
 */
public class Server {
    public static void main(String[] args) {

        try {
            //一、建立一個服務器端Socket,即ServerSocket,指定綁定的端口,並監聽此端口
            ServerSocket serverSocket = new ServerSocket(8888);
            //二、調用accept()方法開始監聽,等待客戶端的鏈接
            Socket socket = null;
            int count = 0;
            System.out.println("****服務器已啓動,等待客戶端的鏈接****");
            //循環監聽等待客戶端鏈接
            while (true){
                socket = serverSocket.accept();
                //建立一個新的線程
                ServerThread serverThread = new ServerThread(socket);
                //啓動線程
                serverThread.start();
                count ++;
                System.out.println("客戶端數量:" + count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

響應結果java

二、ClientSocket

package com.video.video.common;

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

/**
 * 客戶端服務器
 */
public class Client {
    public static void main(String[] args) {
        try {
            //一、建立客戶端Socket,指定服務器地址和端口
            Socket socket = new Socket("localhost", 8888);
            //二、獲取輸出流,向服務器端發送信息
            OutputStream os = socket.getOutputStream();//字節輸出流
            PrintWriter pw = new PrintWriter(os);//將輸出流包裝爲打印流
            pw.write("用戶名:ben,密碼:456");
            pw.flush();

            socket.shutdownOutput();//關閉輸出流

            //三、獲取輸入流,並讀取服務器端響應信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            String centont = null;
            while((centont = br.readLine()) != null){
                System.out.println("我是客戶端,服務器說:" + centont);
            }

//            socket.shutdownInput();


            //關閉資源
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();

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

響應結果服務器

三、ServerThread

package com.video.video.common;

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

/**
 * 服務器線程類
 */
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()方法將緩衝輸出

            socket.shutdownOutput();

        }catch (Exception 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 (Exception e){
                e.printStackTrace();
            }
        }

    }
}

服務器端接收到客戶端發送過來的登陸信息,服務器也將校驗結果發送給客戶端。該Socket實例是支持多用戶進行登陸socket

相關文章
相關標籤/搜索