美文网首页
netty网络编程-1.BIO

netty网络编程-1.BIO

作者: 笨鸡 | 来源:发表于2020-03-30 21:13 被阅读0次

    TCPServer

    package com.ctgu.bio;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class TCPServer {
        public static void main(String[] args) throws Exception {
    
            final int port = 9999;
            ServerSocket ss = new ServerSocket(port);
            Scanner sc = new Scanner(System.in);
            ThreadLocal<String> threadLocal = new ThreadLocal<>();
            threadLocal.set("threadLocal");
    
            while (true) {
                System.out.println("正在监听:" + port + "端口!");
                Socket ac = ss.accept();
                System.out.println("客户端已连接!");
    
                PrintWriter writer = new PrintWriter(
                        new OutputStreamWriter(ac.getOutputStream()), true);
    
                new Thread(() -> {
                    try {
                        System.out.println(threadLocal.get());
                        String msg;
                        while ((msg = sc.nextLine()) != null) {
                            if (msg.equals("88")) {
                                if (!ac.isClosed()) {
                                    System.out.println("服务器手动与客户端断开");
                                    ac.close();
                                }
                                break;
                            }
                            writer.println(msg);
                        }
                    } catch (IOException io) {
                        System.out.println("关闭socket出现问题");
                    } catch (Exception e) {
                        System.out.println("异常关闭客户端(可能是直接关闭退出窗口)");
                        e.printStackTrace();
                    }
                }).start();
    
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(ac.getInputStream()));
    
                System.out.println("局部线程变量:" + threadLocal.get());
                try {
                    String msg;//客户端发过来的信息
                    while ((msg = bufferedReader.readLine()) != null) {
                        System.out.println("##客户端:" + msg);
                    }
                } catch (IOException e) {
                    try {
                        if (!ac.isClosed()) {
                            ac.close();
                        }
                    } catch (IOException e1) {
                        System.out.println("关闭socket出现错误");
                    }
                }
                System.out.println("提示:当前客户端已经断开连接,服务器正等待下一个客户端的连接。");
            }
        }
    }
    

    TCPClient

    package com.ctgu.bio;
    
    import java.io.*;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class TCPClient {
        public static void main(String[] args) throws Exception {
            while (true) {
                Socket s = new Socket("localhost", 9999);
                PrintWriter writer = new PrintWriter(
                        new OutputStreamWriter(s.getOutputStream()), true);
                Scanner sc = new Scanner(System.in);
                new Thread(() -> {
                    try {
                        String msg;
                        while ((msg = sc.nextLine()) != null) {
                            if (msg.equals("88")) {
                                if (!s.isClosed()) {
                                    System.out.println("客户端手动与服务器断开");
                                    s.close();
                                }
                                break;
                            }
                            writer.println(msg);
                        }
                    } catch (IOException io) {
                        System.out.println("关闭socket出现问题");
                    } catch (Exception e) {
                        System.out.println("异常关闭客户端(可能是直接关闭退出窗口)");
                    }
                }).start();
    
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(s.getInputStream()));
                try {
                    String msg;  //服务器发过来的信息
                    while ((msg = bufferedReader.readLine()) != null) {
                        System.out.println("##服务器:" + msg);
                    }
                } catch (IOException e) {
                    System.out.println("警告:断开连接!");
                    try {
                        if (!s.isClosed()) {
                            s.close();
                        }
                    } catch (IOException e1) {
                        System.out.println("读取线程:关闭socket出现错误");
                    }
                }
                System.exit(1);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:netty网络编程-1.BIO

          本文链接:https://www.haomeiwen.com/subject/hlybuhtx.html