基于TCP协议的Socket通讯,实现多线程简单登录

感谢慕课网 《java Socket应用 – 通讯是这样练成的》

目录结构

这里写图片描述

Server.java
/**
 * 基于TCP协议的Socket通讯,实现登录
 * 服务端
 */
public class Server {
    public static void main(String[] args) {
        try {
            //1创建一个服务器端的socket,即serverSocket,只能绑定的端口,并监听此端口。
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = null;
            int count = 0;
            //2调用accept()方法开始监听,等待客户端的连接
            System.out.println("****************服务器即将启动,等待客户端的连接*************");
            while (true) {
                //调用accept()方法开始监听,等待客户端的连接
                socket = serverSocket.accept();
                //穿件一个新的线程
                ServerThread serverThread = new ServerThread(socket);
                //启动线程
                serverThread.start();
                count++;
                System.out.println("客户端的数量:"+count);
                InetAddress address = socket.getInetAddress();
                System.out.println("当前客户端的ip"+address);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
ServerThread .java
/**
 * 服务器线程处理类
 */
public class ServerThread extends Thread {
    Socket socket = null;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            //3.获取输入流,并读取客户端信息
            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();//关闭输入流
            //4.获取输出流,响应客户端请求
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("欢迎您!");
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭资源
            try {
                if (pw != null)
                    pw.close();
                if (os != null)
                    os.close();
                if (br != null)
                    br.close();
                if (isr != null)
                    isr.close();
                if (isr != null)
                    is.close();
                if (isr != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }

}
Client.java
/**
 * 基于TCP协议的Socket通讯,实现登录
 * 客户端
 */
public class Client {
    public static void main(String[] args) {

        try {
            //1.创建客户端Socket,指定服务器地址和端口
            Socket socket = new Socket("localhost", 8888);
            //2.获取输出流。用来向服务器发送信息
            OutputStream os = socket.getOutputStream();//字节输出流
            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
            pw.write("用户名:admin2 密码: 123");
            pw.flush();
            socket.shutdownOutput();//关闭输出流
            //3.获取输入流,用来读取服务器的响应信息
            InputStream inputStream = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String info = null;
            while ((info = br.readLine()) != null) {
                System.out.println("我是客户端,服务器说" + info);
            }

            //4.关闭其他资源
            pw.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_18115729/article/details/78961665