zoukankan      html  css  js  c++  java
  • Socket网络通信之线程池

    public static void main(String[] args) throws IOException {
    System.out.println("tcp协议服务器端启动..");
    ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();//线程池
    // 创建服务器端连接
    ServerSocket serverSocket = new ServerSocket(8080);
    try {
    while (true) {
    // 接受客户端请求? 阻塞功能
    Socket accept = serverSocket.accept();
    newCachedThreadPool.execute(new Runnable(){
    
    @Override
    public void run() {
    try {
    InputStream inputStream = accept.getInputStream();
    // 将字节流转换成String类型
    byte[] bytes = new byte[1024];
    int len = inputStream.read(bytes);
    String result=new String(bytes,0,len);
    System.out.println("服务器端接受客户端内端?:"+result);
    OutputStream outputStream = accept.getOutputStream();
    outputStream.write("我是曹操哦.getBytes());
    } catch (Exception e) {
    // TODO: handle exception
    }
    
    }
    });
    
    }
    } catch (Exception e) {
    // TODO: handle exception
    }finally {
    serverSocket.close();
    }
    
    }
    
    }
    
     
    客户端
    
     
    
     
    
     
    
     
    
    public static void main(String[] args) throws IOException {
    System.out.println("socket tcp客户端启成功?....");
    //创建socket客户端?
    Socket socket=new Socket("127.0.0.1",8080);
    OutputStream outputStream = socket.getOutputStream();
    outputStream.write("我是曹操".getBytes());
    socket.close();
    }
  • 相关阅读:
    mac+chrome 最常用快捷键
    关于<form> autocomplete 属性
    MAC vim修改hosts文件
    git 使用详解(3)—— 最基本命令 + .gitignore 文件
    git 使用详解(2)——安装+配置+获取帮助
    vue动态生成组件
    slot插槽
    provide 和 inject高阶使用
    js正则验证表达式验证
    angular常用命令整理
  • 原文地址:https://www.cnblogs.com/nancheng/p/9232489.html
Copyright © 2011-2022 走看看