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)、输出

  • 相关阅读:
    滴滴Ceph分布式存储系统优化之锁优化
    滴滴数据仓库指标体系建设实践
    滴滴AI Labs斩获国际机器翻译大赛中译英方向世界第三
    滴滴数据通道服务演进之路
    可编程网卡芯片在滴滴云网络的应用实践
    GPU虚拟机创建时间深度优化
    滴滴ElasticSearch千万级TPS写入性能翻倍技术剖析
    使用zip()并行迭代
    循环结构
    选择结构
  • 原文地址:https://www.cnblogs.com/smallfa/p/15627660.html
Copyright © 2011-2022 走看看