当前位置: 代码迷 >> 综合 >> Netty3.0.0客户端 服务端代码
  详细解决方案

Netty3.0.0客户端 服务端代码

热度:8   发布时间:2023-12-15 22:20:14.0

pom:

    <dependencies><dependency><groupId>io.netty</groupId><artifactId>netty</artifactId><version>3.3.0.Final</version></dependency></dependencies>

服务端:


package com.cssoc;import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;class ServerHanlder extends SimpleChannelHandler {
    // 通道被关闭的时候会触发@Overridepublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelClosed(ctx, e);System.out.println("channelClosed");}// 必须要建立连接,关闭通道的时候才会触发@Overridepublic void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelDisconnected(ctx, e);System.out.println("channelDisconnected");}// 接受出现异常@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    super.exceptionCaught(ctx, e);System.out.println("exceptionCaught");}// 接受客户端数据..@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    super.messageReceived(ctx, e);System.out.println("messageReceived");System.out.println("服务器获取客户端发来的参数:" + e.getMessage());ctx.getChannel().write("你好啊!");}
}// netty 服务器端
public class NettyServer {
    public static void main(String[] args) {
    // 1.创建服务对象ServerBootstrap serverBootstrap = new ServerBootstrap();// 2.创建两个线程池 第一个 监听端口号 nio监听ExecutorService boos = Executors.newCachedThreadPool();ExecutorService wook = Executors.newCachedThreadPool();// 3.将线程池放入工程serverBootstrap.setFactory(new NioServerSocketChannelFactory(boos, wook));// 4.设置管道工程serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    // 设置管道public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();// 传输数据的时候直接为string类型pipeline.addLast("decoder", new StringDecoder());pipeline.addLast("encoder", new StringEncoder());// 设置事件监听类pipeline.addLast("serverHanlder", new ServerHanlder());return pipeline;}});// 绑定端口号serverBootstrap.bind(new InetSocketAddress(8080));System.out.println("服务器端已经被启动.....");
// while (true) {
    
// try {
    
// Thread.sleep(500);
// } catch (Exception e) {
    
// // TODO: handle exception
// }
// System.out.println("每隔0.五秒打印.....");
//
// }}
}

客户端:


package com.cssoc;import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;class ClientHanlder extends SimpleChannelHandler{
    // 通道被关闭的时候会触发@Overridepublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelClosed(ctx, e);System.out.println("channelClosed");}// 必须要建立连接,关闭通道的时候才会触发@Overridepublic void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelDisconnected(ctx, e);System.out.println("channelDisconnected");}// 接受出现异常@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    super.exceptionCaught(ctx, e);System.out.println("exceptionCaught");}// 接受客户端数据..@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    super.messageReceived(ctx, e);System.out.println("messageReceived");System.out.println("服务器向客户端回复的内容:" + e.getMessage());}
}
//netty客户端 
public class NettyClinet {
    public static void main(String[] args) {
    // 1.创建服务对象ClientBootstrap clientBootstrap = new ClientBootstrap();// 2.创建两个线程池 第一个 监听端口号 nio监听ExecutorService boos = Executors.newCachedThreadPool();ExecutorService wook = Executors.newCachedThreadPool();// 3.将线程池放入工程clientBootstrap.setFactory(new NioClientSocketChannelFactory(boos, wook));// 4.设置管道工程clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    // 设置管道public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();// 传输数据的时候直接为string类型pipeline.addLast("decoder", new StringDecoder());pipeline.addLast("encoder", new StringEncoder());// 设置事件监听类pipeline.addLast("clientHanlder", new ClientHanlder());return pipeline;}});// 绑定端口号ChannelFuture connect = clientBootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));Channel channel = connect.getChannel();System.out.println("client start");Scanner scanner = new Scanner(System.in);while (true) {
    System.out.println("请输入内容:");channel.write(scanner.next());}}
}
  相关解决方案