zoukankan      html  css  js  c++  java
  • Netty 2021 1129 阻塞IO 特点

     

     

     

     

     

     

     

    2、实例 client-server

    i)、SocketTcpBioServer 

    import java.io.IOException;
    import  java.net.*;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class SocketTcpBioServer {
        static byte[] bytes=new byte[1024];
        public static void main(String[] args) throws IOException {
            try {
                //1、创建ServerSocker
                final ServerSocket serverSocket=new ServerSocket();
                serverSocket.bind(new InetSocketAddress(8080));
                ExecutorService executorService=Executors.newCachedThreadPool();
                System.out.println("等客户端发送信息......");
                Socket socket=serverSocket.accept();
                int read=socket.getInputStream().read(bytes);
                String result=new String(bytes);
                System.out.println("服务端接受客户端消息:"+result);
               /* //2、等客户端发送信息
                while (true){
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                           try {
                               System.out.println("等客户端发送信息......");
                               Socket socket=serverSocket.accept();
                               int read=socket.getInputStream().read(bytes);
                               String result=new String(bytes);
                               System.out.println("服务端接受客户端消息:"+result);
                           }catch (Exception e){

                           }
                        }
                    });

                }*/
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    ii)、SocketTcpBioClient 

    import  java.io.IOException;
    import java.net.*;

    public class SocketTcpBioClient {
        public static void main(String[] args) throws IOException {
            try {
                Socket socket=new Socket();
                SocketAddress address=new InetSocketAddress(InetAddress.getLocalHost(),8080);
                socket.connect(address);
                socket.getOutputStream().write("mayikt1".getBytes());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    iii)、输出

  • 相关阅读:
    Django 类方式view进行进行用户验证
    Django-发送注册、忘记密码邮件验证-send_mail
    Django-当前菜单激活状态-模版 request | slice
    django url路由参数错误
    video.js不能控制本地视频或者音频播放时长
    Django中url使用命名空间的错误
    python_求相邻数
    scrapy_移除内容中html标签
    scrapy-redis功能简介
    Determining IP information for eth0...failed 错误解决
  • 原文地址:https://www.cnblogs.com/smallfa/p/15627660.html
Copyright © 2011-2022 走看看