[Java網絡編程] TCP協議實現簡單多用戶登錄

分爲客戶端與服務器端。客戶端向服務器端發送用戶信息,請求登錄;服務器端採用多線程處理多用戶登錄請求,並簡單匹配用戶登錄信息是否正確,返回相應的狀態。

package NET;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TCP_Server {
    public static void main(String[] args) throws IOException {
        System.out.println("-----SERVER-----");
        // 1. 指定端口,使用ServerSocket創建服務器套接字
        ServerSocket server = new ServerSocket(8888);
        while (true) {
            // 2. 阻塞式等待TCP連接
            Socket client = server.accept();
            System.out.println("建立TCP連接");
            // 3. 創建新線程處理業務
            new Thread(new Channel(client)).start();
        }
    }
}

class Channel implements Runnable {
    private Socket client;
    private DataInputStream dis;
    private DataOutputStream dos;

    Channel(Socket client) throws IOException {
        this.client = client;
        this.dis = new DataInputStream(this.client.getInputStream());
        this.dos = new DataOutputStream(this.client.getOutputStream());
    }

    private String receive() throws IOException {
        return dis.readUTF();
    }

    private void send(String msg) throws IOException {
        dos.writeUTF(msg);
        dos.flush();
    }

    private void release() throws IOException {
        if (dis != null) {
            dis.close();
        }
        if (dos != null) {
            dos.close();
        }
        if (client != null) {
            client.close();
        }
    }

    @Override
    public void run() {
        String username = null;
        String password = null;

        try {
            String[] dataArray = receive().split("&");
            for (String data: dataArray) {
                String[] userInfo = data.split("=");
                switch (userInfo[0]) {
                    case "username":
                        username = userInfo[1];
                        System.out.println("用戶:" + username);
                        break;
                    case "password":
                        password = userInfo[1];
                        System.out.println("密碼:" + password);
                        break;
                }
            }

            if (username.equals("username") && password.equals("password")) {
                send("登錄成功");
            } else {
                send("登錄失敗");
            }

            release();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package NET;

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

public class TCP_Client {
    public static void main(String[] args) throws IOException {
        System.out.println("-----CLIENT-----");
        Socket server = new Socket("localhost", 8888);
        Session session = new Session(server);
        session.getInfo();
        session.send();
        session.receive();
        session.release();
    }
}

class Session {
    private String msg;
    private Socket server;
    private DataInputStream dis;
    private DataOutputStream dos;

    Session(Socket server) throws IOException {
        this.msg = null;
        this.server = server;
        this.dis = new DataInputStream(server.getInputStream());
        this.dos = new DataOutputStream(server.getOutputStream());
    }

    void getInfo() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入用戶名:");
        String username = reader.readLine();
        System.out.println("請輸入密碼:");
        String password = reader.readLine();
        msg = String.format("username=%s&password=%s", username, password);
    }

    void send() throws IOException {
        dos.writeUTF(msg);
        dos.flush();
    }

    void receive() throws IOException {
        String res = dis.readUTF();
        System.out.println(res);
    }

    void release() throws IOException {
        if (dis != null) {
            dis.close();
        }
        if (dos != null) {
            dos.close();
        }
        if (server != null) {
            server.close();
        }
    }
}

 

發佈了115 篇原創文章 · 獲贊 157 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章