zoukankan      html  css  js  c++  java
  • Dart连接socket(netty)

    Dart连接socket(netty)

    Client:

    import 'dart:io';
    import 'dart:async';
    import 'dart:convert' show utf8;
    
    Socket socket;
    
    void main() {
      Socket.connect("localhost", 8888).then((Socket sock) {
        socket = sock;
        socket.listen(dataHandler,
            onError: errorHandler,
            onDone: doneHandler,
            cancelOnError: false);
      }).catchError((AsyncError e) {
        print("Unable to connect: $e");
      });
      //发报文
      stdin.listen((data) => socket.write( utf8.decode(data)));
    }
    
    //接收报文
    void dataHandler(data){
      print(utf8.decode(data));
    }
    
    void errorHandler(error, StackTrace trace){
      print(error);
    }
    
    void doneHandler(){
      socket.destroy();
    }
    

    Server:

    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;
    
    public class MyMain {
        public static void main(String[] args) {
    
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                    .group(new NioEventLoopGroup(),new NioEventLoopGroup())
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        protected void initChannel(NioSocketChannel ch) {
                            ch.pipeline().addLast(new StringDecoder());
                            ch.pipeline().addLast(new StringEncoder());
                            ch.pipeline().addLast(new MyHandler());
                        }
                    }).bind(8888).addListener((e)->{
                            if(e.isSuccess()){
                                System.out.println("server is successfully");
                            }
                        });
    
        }
    
    }
    
    
    class MyHandler extends SimpleChannelInboundHandler<String> {
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("有新连接");
            super.channelActive(ctx);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    //        super.exceptionCaught(ctx, cause);
        }
    
        protected void channelRead0(ChannelHandlerContext ctx, String msg){
            System.out.println("有人说:"+msg);
            ctx.writeAndFlush("你说'"+msg+"'?");
        }
    }
    
  • 相关阅读:
    堆和堆排序
    快速排序
    Sublime Text3汉化好的绿色免安装版使用和破解教程+下载链接
    复杂数据类型(signal)的解读-C语言基础
    C/C++命名规范-C语言基础
    fgets()函数的详解以及使用时需要注意的一些细节-C语言基础
    scanf()函数的详解以及使用时需要注意的一些细节-C语言基础
    getchar()函数的详解以及使用时需要注意的一些细节-C语言基础
    如何用算法把一个十进制数转为十六进制数-C语言基础
    杨辉三角形实现过程详解-C语言基础
  • 原文地址:https://www.cnblogs.com/AngeLeyes/p/10820459.html
Copyright © 2011-2022 走看看