TCP协议实现一个回显服务器

  • TCP协议--面向字节流,有连接(例如打电话)
  • 核心类:Server Socket
  • 服务器的处理方式:
  1. 短连接:只交互一次,交互完毕,就断开连接
  2. 长连接:可交互多次,满足一定条件在断开(效率更高) 
  •  客户端:
  1. 启动客户端(客户端不要绑定端口号)和服务器建立连接 。
  2. 进入主循环:(1)读取用户输入的内容。(2)构造请求并发送。(3)读取响应数据。(4)把响应数据显示到界面上。
package TCP1102;

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

public class TcpEchoClient {
    //1.启动客户端(客户端不要绑定端口号)和服务器建立连接
    //2.进入主循环
    private Socket socket = null;

    public TcpEchoClient(String serverIP,int serverPort) throws IOException {
        //此处的实例化socket过程就是在建立TCP连接
        socket = new Socket(serverIP,serverPort);
    }
    public void start() {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            while (true) {
                //1.读取用户输入内容
                System.out.println("-> ");
                String request = scanner.nextLine();
                if (request.equals("exit")) {
                    break;
                }
                //2.构造请求并发送,此处\n为了和服务器中的readline相对应
                bufferedWriter.write(request + "\n");
                //缓冲区刷新:调用flush方法,就是把内存缓冲区中的内容写入到socket文件中,才真的通过网卡来发送数据
                bufferedWriter.flush();
                //3.读取响应数据
                String response = bufferedReader.readLine();
                //4.把响应数据显示到界面上
                System.out.println(response);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1",9090);
        client.start();
    }
}
  •  服务器:
  1.  初始化服务器。
  2. 进入主循环:(1)先去从内核中获取到一个TCP连接。(2)处理这个TCP的连接
    a.读取请求并解析。b.根据请求计算响应。c.把响应写回给客户端
package TCP1102;

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

public class TcpEchoServer {
    //1.初始化服务器
    //2.进入主循环:(1)先去从内核中获取到一个TCP连接。(2)处理这个TCP的连接
    //a.读取请求并解析。b.根据请求计算响应。c.把响应写回给客户端
    private ServerSocket serverSocket = null;

    public TcpEchoServer(int port) throws IOException {
        //这个操作和前面的UDP类似,也是要绑定端口号
        serverSocket = new ServerSocket(port);
    }

    public void start() throws IOException {
        System.out.println("服务器启动");
        while(true) {
            //1.先从内核中获取到一个TCP连接
            Socket clientSocket = serverSocket.accept();
            //2.处理这个连接
            processConnection(clientSocket);
        }
    }

    private void processConnection(Socket clientSocket) {
        System.out.printf("[%s:%d]  客户端上线\n",clientSocket.getInetAddress().toString(),
                clientSocket.getPort());
        //通过clientSocket来和客户端交互,先做好准备工作,获取到clientSocket中的流对象
        try {
            //把字节流转成字符流,再套上缓冲区
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            //长连接版本的服务器:一次连接可以处理多次请求,当客户端断开连接的时候,服务器再去调用readline或write方法都会触发异常
            while (true) {
                //1.读取请求并解析(readline对应客户端发送数据的格式,必须是按行发送)
                String request = bufferedReader.readLine();
                //2.根据请求计算响应。
                String response = process(request);
                //3.把响应写回给客户端
                bufferedWriter.write(response + "\n");
                //缓冲区刷新:调用flush方法,就是把内存缓冲区中的内容写入到socket文件中,才真的通过网卡来发送数据
                bufferedWriter.flush();

                System.out.printf("[%s:%d] req: %s; resp: %s\n",clientSocket.getInetAddress(),
                        clientSocket.getPort(),request,response);

            }
        } catch (IOException e) {
            System.out.printf("[%s:%d]  客户端下线\n",clientSocket.getInetAddress().toString(),
                    clientSocket.getPort());
        }
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        TcpEchoServer server = new TcpEchoServer(9090);
        server.start();
    }
}
  • 使用多线程可以让服务器同时和多个客户端建立连接
package TCP1102;

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


//使用多线程可以让服务器同时和多个客户端建立连接
public class TcpThreadEchoSever {
    //1.初始化服务器
    //2.进入主循环:(1)先去从内核中获取到一个TCP连接。(2)处理这个TCP的连接
    //a.读取请求并解析。b.根据请求计算响应。c.把响应写回给客户端
    private ServerSocket serverSocket = null;

   public TcpThreadEchoSever(int port) throws IOException {
       serverSocket = new ServerSocket(port);
   }

    public void start() throws IOException {
        System.out.println("服务器启动");
        while (true) {
            //1.先从内核中获取到一个TCP连接
            Socket clientSocket = serverSocket.accept();
            //2.针对这个连接,单独创建一个线程来负责处理
            Thread t = new Thread() {
                @Override
                public void run() {
                    processConnection(clientSocket);
                }
            };
            t.start();
        }
    }

    private void processConnection(Socket clientSocket) {
        System.out.printf("[%s:%d]  客户端上线\n",clientSocket.getInetAddress().toString(),
                clientSocket.getPort());
        //通过clientSocket来和客户端交互,先做好准备工作,获取到clientSocket中的流对象
        try {
            //把字节流转成字符流,再套上缓冲区
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            //长连接版本的服务器:一次连接可以处理多次请求,当客户端断开连接的时候,服务器再去调用readline或write方法都会触发异常
            while (true) {
                //1.读取请求并解析(readline对应客户端发送数据的格式,必须是按行发送)
                String request = bufferedReader.readLine();
                //2.根据请求计算响应。
                String response = process(request);
                //3.把响应写回给客户端
                bufferedWriter.write(response + "\n");
                //缓冲区刷新:调用flush方法,就是把内存缓冲区中的内容写入到socket文件中,才真的通过网卡来发送数据
                bufferedWriter.flush();

                System.out.printf("[%s:%d] req: %s; resp: %s\n",clientSocket.getInetAddress(),
                        clientSocket.getPort(),request,response);

            }
        } catch (IOException e) {
            System.out.printf("[%s:%d]  客户端下线\n",clientSocket.getInetAddress().toString(),
                    clientSocket.getPort());
        }
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        TcpEchoServer server = new TcpEchoServer(9090);
        server.start();
    }
}
  •  用线程池来解决服务器频繁创建线程和销毁线程的问题
package TCP1102;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


//用线程池来解决服务器频繁创建线程和销毁线程的问题
public class TcpThreadPoolEchoSever {
    //1.初始化服务器
    //2.进入主循环:(1)先去从内核中获取到一个TCP连接。(2)处理这个TCP的连接
    //a.读取请求并解析。b.根据请求计算响应。c.把响应写回给客户端
    private ServerSocket serverSocket = null;

    public TcpThreadPoolEchoSever(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }


    public void start() throws IOException {
        System.out.println("服务器启动");
        //先创建一个线程池实例
        ExecutorService executorService = Executors.newCachedThreadPool();

        while (true) {
            //1.先从内核中获取到一个TCP连接
            Socket clientSocket = serverSocket.accept();
            //2.针对这个连接,单独创建一个线程来负责处理
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    processConnection(clientSocket);
                }
            });
        }
    }

    private void processConnection(Socket clientSocket) {
        System.out.printf("[%s:%d]  客户端上线\n",clientSocket.getInetAddress().toString(),
                clientSocket.getPort());
        //通过clientSocket来和客户端交互,先做好准备工作,获取到clientSocket中的流对象
        try {
            //把字节流转成字符流,再套上缓冲区
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            //长连接版本的服务器:一次连接可以处理多次请求,当客户端断开连接的时候,服务器再去调用readline或write方法都会触发异常
            while (true) {
                //1.读取请求并解析(readline对应客户端发送数据的格式,必须是按行发送)
                String request = bufferedReader.readLine();
                //2.根据请求计算响应。
                String response = process(request);
                //3.把响应写回给客户端
                bufferedWriter.write(response + "\n");
                //缓冲区刷新:调用flush方法,就是把内存缓冲区中的内容写入到socket文件中,才真的通过网卡来发送数据
                bufferedWriter.flush();

                System.out.printf("[%s:%d] req: %s; resp: %s\n",clientSocket.getInetAddress(),
                        clientSocket.getPort(),request,response);

            }
        } catch (IOException e) {
            System.out.printf("[%s:%d]  客户端下线\n",clientSocket.getInetAddress().toString(),
                    clientSocket.getPort());
        }
    }

    private String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        TcpThreadPoolEchoSever server = new TcpThreadPoolEchoSever(9090);
        server.start();
    }
}

 

 

你可能感兴趣的:(tcp/ip,服务器,网络)