Java 模拟基于TCP的Socket通信

效果图:


Java 模拟基于TCP的Socket通信
单线程服务器
Java 模拟基于TCP的Socket通信
多线程服务器

Java 模拟基于TCP的Socket通信
客户端模拟

多线程服务器代码

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

public class MutiThreadServer implements Runnable {

    Socket socket;

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

    public static void main(String[] args) throws IOException {
        int count=0;
        ServerSocket serverSocket = new ServerSocket(1234);
        System.out.println("listening");
        while (true){
            Socket socket1 = serverSocket.accept();
            System.out.println("connected");
            new Thread(new MutiThreadServer(socket1)).start();
            count++;
            System.out.println("客户端的数量:"+count);
            InetAddress address = socket1.getInetAddress();
            System.out.println("当前客户端的IP为:"+ address.getHostAddress());
        }
    }

    @Override
    public void run() {
        InputStream inputStream=null; //字节输入流
        InputStreamReader inputStreamReader=null;  //把字节输入流转化为字符输入流
        BufferedReader bufferedReader=null;  //为输入流添加缓冲
        OutputStream outputStream=null;
        PrintWriter printWriter=null;
        try {
            /**
             * 获取客户端的输入信息
             */
            inputStream = socket.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String info = null;
            while ((info = bufferedReader.readLine()) != null){
                System.out.println("我是服务器,收到客户端信息:"+info);
            }
            socket.shutdownInput(); //关闭输入流

            /**
             * 响应客户端
             */
            //获取输出流,用于向服务器发送请求
            outputStream = socket.getOutputStream();
            //把输出流转化为打印流
            printWriter = new PrintWriter(outputStream);
            printWriter.write("用户名是:Harry 欢迎您!!!");
            printWriter.flush();



        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            try {
                if (inputStream != null)
                    inputStream.close();

                if (inputStream != null)
                    inputStreamReader.close();

                if (bufferedReader != null) {
                    bufferedReader.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }

                if (printWriter != null) {
                    printWriter.close();
                }

                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

单线程服务器

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

public class Server {
    public static void main(String[] args) throws IOException {
        /**
         * 创建一个服务器端的Socket,并指定要监听的端口
         */
        ServerSocket serverSocket = new ServerSocket(1234);
        System.out.println("````````````服务器即将启动````````````");
        /**
         * 启动监听
         */
        Socket socket = serverSocket.accept();

        /**
         * 获取客户端的输入信息
         */
        InputStream inputStream = socket.getInputStream(); //字节输入流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);  //把字节输入流转化为字符输入流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  //为输入流添加缓冲
        String info = null;
        while ((info = bufferedReader.readLine()) != null){
            System.out.println("我是服务器,收到客户端信息:"+info);
        }
        socket.shutdownInput(); //关闭输入流

        /**
         * 响应客户端
         */
        OutputStream outputStream = socket.getOutputStream(); //获取输出流,用于向服务器发送请求
        PrintWriter printWriter = new PrintWriter(outputStream); //把输出流转化为打印流
        printWriter.write("用户名是:Harry 欢迎您!!!");
        printWriter.flush();

        //关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}

客户端

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

public class ClientSocket {
    public static void main(String[] args) throws IOException {
        /**
         * 创建一个客户端的Socket,并指定服务器地址和端口号
         */
        Socket socket = new Socket("localhost",1234);

        OutputStream outputStream = socket.getOutputStream(); //获取输出流,用于向服务器发送请求
        PrintWriter printWriter = new PrintWriter(outputStream); //把输出流转化为打印流
        printWriter.write("用户名是:Harry,密码是:123");
        printWriter.flush();

        socket.shutdownOutput(); //关闭输出流

        /**
         * 获取服务器的信息
         */
        InputStream inputStream = socket.getInputStream(); //字节输入流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);  //把字节输入流转化为字符输入流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  //为输入流添加缓冲
        String info = null;
        while ((info = bufferedReader.readLine()) != null){
            System.out.println("我是客户端,收到服务器信息:"+info);
        }
        socket.shutdownInput(); //关闭输入流

        /**
         * 关闭资源
         */
        inputStream.close();
        inputStreamReader.close();
        bufferedReader.close();
        printWriter.close();
        outputStream.close();
        socket.close();
    }
}

注:测试的时候只需要启动其中一个服务器就即可。

上一篇:引入超融合技术解决虚拟机性能不足问题


下一篇:stream流对map的key和value进行排序(支持字符串)