分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

让Netty入门变得简单

发布时间:2023-09-06 02:00责任编辑:傅花花关键词:暂无标签

让Netty入门变得简单

https://mp.weixin.qq.com/s/MBnbLmCmFJo0QK9WNwXrXQ

如果先启动nettyClient就不会有nettyServer输出了;

package com.stono;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;public class IOServer { ???public static void main(String[] args) throws IOException { ???????ServerSocket serverSocket = new ServerSocket(8000); ???????new Thread(() -> { ???????????while (true) { ???????????????try{ ???????????????????Socket socket = serverSocket.accept(); ???????????????????new Thread(() -> { ???????????????????????try{ ???????????????????????????byte[] data = new byte[1024]; ???????????????????????????InputStream inputStream = socket.getInputStream(); ???????????????????????????while (true) { ???????????????????????????????int len; ???????????????????????????????while ((len = inputStream.read(data)) != -1) { ???????????????????????????????????System.out.println(new String(data, 0, len)); ???????????????????????????????} ???????????????????????????} ???????????????????????}catch (Exception e){ ???????????????????????} ???????????????????}).start(); ???????????????}catch (Exception e){ ???????????????} ???????????} ???????}).start(); ???}}
package com.stono;import java.net.Socket;import java.util.Date;public class IOClient { ???public static void main(String[] args) { ???????new Thread(() -> { ???????????try { ???????????????Socket socket = new Socket("127.0.0.1", 8000); ???????????????while (true) { ???????????????????try { ???????????????????????socket.getOutputStream().write((new Date() + ": hello world").getBytes()); ???????????????????????socket.getOutputStream().flush(); ???????????????????????Thread.sleep(2000); ???????????????????} catch (Exception e) { ???????????????????????e.printStackTrace(); ???????????????????} ???????????????} ???????????} catch (Exception e) { ???????????????e.printStackTrace(); ???????????} ???????}).start(); ???}}
package com.stono;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;public class NettyServer { ???public static void main(String[] args) { ???????ServerBootstrap serverBootstrap = new ServerBootstrap(); ???????NioEventLoopGroup boos = new NioEventLoopGroup(); ???????NioEventLoopGroup worker = new NioEventLoopGroup(); ???????serverBootstrap ???????????????.group(boos, worker) ???????????????.channel(NioServerSocketChannel.class) ???????????????.childHandler(new ChannelInitializer<NioSocketChannel>() { ???????????????????@Override ???????????????????protected void initChannel(NioSocketChannel ch) throws Exception { ???????????????????????ch.pipeline().addLast(new StringDecoder()); ???????????????????????ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() { ???????????????????????????@Override ???????????????????????????protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception { ???????????????????????????????System.out.println(s); ???????????????????????????} ???????????????????????}); ???????????????????} ???????????????}) ???????????????.bind(8000); ???}}
package com.stono;import io.netty.bootstrap.Bootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.string.StringEncoder;import java.util.Date;import java.util.concurrent.TimeUnit;public class NettyClient { ???public static void main(String[] args) throws InterruptedException { ???????Bootstrap bootstrap = new Bootstrap(); ???????NioEventLoopGroup group = new NioEventLoopGroup(); ???????bootstrap.group(group) ???????????????.channel(NioSocketChannel.class) ???????????????.handler(new ChannelInitializer<Channel>() { ???????????????????@Override ???????????????????protected void initChannel(Channel channel) throws Exception { ???????????????????????channel.pipeline().addLast(new StringEncoder()); ???????????????????} ???????????????}); ???????Channel channel = bootstrap.connect("127.0.0.1", 8000).channel(); ???????while (true) { ???????????channel.writeAndFlush(new Date() + ": hello world"); ???????????TimeUnit.SECONDS.sleep(2); ???????} ???}}

让Netty入门变得简单

原文地址:https://www.cnblogs.com/stono/p/9198407.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved