[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();
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值