TCP简介

TCP简介

TCP是一种面向连接,安全,可靠的传输数据的协议
传输前,采用三次握手,结束后,采用四次挥手

TCP入门案例演示

案例一

客户端

public static void main(String[] args) throws Exception {
        System.out.println("客户端启动成功");
        //创建Scoket通信管道
        Socket socket=new Socket("127.0.0.1",7777);

        //创建一个字节输出流
        OutputStream outputStream=socket.getOutputStream();

        //将低级的字节流包装成打印流
        PrintStream ps= new PrintStream(outputStream);

        ps.println("asdadad");

        //关闭资源
        ps.close();
        socket.close();
    }

服务端

public static void main(String[] args) throws Exception {
        System.out.println("服务端启动成功");
        //注册端口
        ServerSocket serverSocket=new ServerSocket(7777);

        //调用accept接口,等待接受客户端的Socket连接请求,建立Socket通信管道
        Socket socket=serverSocket.accept();

        //创建字节输入流
        InputStream is=socket.getInputStream();

        //将字节输入流转换成字符输入流,并包装成字符缓冲输入流
        BufferedReader br=new BufferedReader(new InputStreamReader(is));

        String rs;
        while ((rs=br.readLine())!=null){
            System.out.println(socket.getRemoteSocketAddress()+":"+rs);
        }
    }

案例二

上述案例因为每次只能处理一个客户的Socket通道,因而无法是实现服务端于多个客户端通讯

所以我们引入多线程

 public static void main(String[] args) throws Exception {
        System.out.println("服务端启动成功");
        //注册端口
        ServerSocket serverSocket=new ServerSocket(7777);

        while (true){
            Socket socket=serverSocket.accept();
            new MyThread(socket).start();
        }
    }

创建线程对象

public class MyThread extends Thread{
    private Socket socket;
    public MyThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            //创建字节输入流
            InputStream is=socket.getInputStream();

            //将字节输入流转换成字符输入流,并包装成字符缓冲输入流
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String rs;
            while ((rs=br.readLine())!=null){
                System.out.println(socket.getRemoteSocketAddress()+":"+rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

案例三

很显然我们可以在案列二的基础上加上线程池
实现runnable接口

public class Myrunnable implements Runnable{
    private Socket socket;
    public Myrunnable(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            //创建字节输入流
            InputStream is=socket.getInputStream();

            //将字节输入流转换成字符输入流,并包装成字符缓冲输入流
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String rs;
            while ((rs=br.readLine())!=null){
                System.out.println(socket.getRemoteSocketAddress()+":"+rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

客户端

public static void main(String[] args) throws Exception {
        System.out.println("客户端启动成功");
        //创建Scoket通信管道
        Socket socket=new Socket("127.0.0.1",6666);

        //创建一个字节输出流
        OutputStream outputStream=socket.getOutputStream();

        //将低级的字节流包装成打印流
        PrintStream ps= new PrintStream(outputStream);

        Scanner scanner=new Scanner(System.in);

        while (true){
            System.out.println("请说");
            String s=scanner.nextLine();
            if("exit".equals(s)){
                System.out.println("客户端已退出");
                ps.close();
                socket.close();
                break;
            }
            ps.println(s);
            ps.flush();
        }
    }

用户端

private  static  final ExecutorService pool=new ThreadPoolExecutor(3,5,6,
            TimeUnit.SECONDS,new ArrayBlockingQueue<>(2),
            Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws Exception {
        System.out.println("服务端启动成功");
        //注册端口
        ServerSocket serverSocket=new ServerSocket(6666);

        while (true){
            Socket socket=serverSocket.accept();
            Runnable target=new Myrunnable(socket);
            pool.execute(target);
        }
    }

你可能感兴趣的:(计算机网络,tcp/ip,网络,网络协议)