Socket Client 怎么连接 Netty Server

AlenCheng AlenCheng 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题,想在需要用Netty做一个Server端。用来接收Socket Client发送的Request.
感觉已经连接上了,但是就是没有把请求文本打出来。不知道是怎么回事了。

Netty Server

package server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class MyNettyServer {

ChannelFuture f = null;

EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

private void run(int port) {
try {
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
// 有连接到达时会创建一个Channel
@Override
protected void initChannel(SocketChannel channel) throws Exception {
System.out.println("has a request....");
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
// 在Channel队列中,添加自己的Handler处理业务
pipeline.addLast(new HttpServerInboundHandler());
}
});

// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
f = b.bind(port).sync();
System.out.println("Server statrted ...");
// 应用程序会一直等待,直到channel关闭
f.channel().closeFuture().sync();
} catch (Exception e) {

}
}

private void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
MyNettyServer server = new MyNettyServer();
server.run(8000);
}
}



Netty handler

package server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HttpServerInboundHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("server receive message : "+ msg);
ctx.channel().writeAndFlush("server response message : " + msg + "...");
ctx.close();
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("read complete...");
ctx.close();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println("throw exption...");
ctx.close();
}
}


Socket Client

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MySocketClient {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket client=new Socket("127.0.0.1", 8000);
PrintWriter pw=new PrintWriter(client.getOutputStream());
pw.write(“hello!”);
pw.flush();
pw.close();
br.close();
}

}


运行完Client以后的结果
Server statrted ...
has a request....

说明已经连接到了Netty,但是请求的文本没有过来。
麻烦各位帮忙看下了,谢谢!
...全文
评论 16
收藏 已收藏
分享
返回顶部
AlenCheng
Java SE 62,621 成员 307,251 内容