返回信息流在本机上测试netty的服务端和客户端,要实现的小功能就是类似于客户端连上服务端后,客户端发送信息给服务端,而服务端将这个信息返回给客户端,并且在客户端上显示出来。
但是发现使用自己写的客户端发送信息给服务端后,服务端并不会进入pipeline去处理信息,而使用一个别的第三方的测试工具就能正常的实现功能。。所以断定是我的客户端的错误,但是不知道错误在哪。所以把代码放上来。。希望有人能帮帮我。。
server端:
public class ServerTest {
private int port;
public ServerTest(int port) {
this.port = port;
}
public void server() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO),
new ServerHandler());
}
});
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 10008;
new ServerTest(port).server();
}
}
serverHandler:
public class ServerHandler extends ChannelHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
ctx.close();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
// TODO Auto-generated method stub
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
ctx.flush();
System.out.println("channelReadComplete");
}
}
client端:
public class ClientTest {
private String ip;
private int port;
public ClientTest(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void client() throws Exception {
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new ClientHandler());
}
});
Channel ch = b.connect(ip, port).sync().channel();
//input message
System.out.println("Enter text (quit to end)");
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
String line = in.readLine();
if (line == null || "quit".equalsIgnoreCase(line)) {
break;
}
lastWriteFuture = ch.writeAndFlush(line);
}
if (lastWriteFuture != null) {
lastWriteFuture.awaitUninterruptibly();
}
} finally {
workGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
String ip = "10.103.xxx.xxx"; //ip address
int port = 10008;
new ClientTest(ip, port).client();
}
}
clientHandler:
public class ClientHandler extends SimpleChannelInboundHandler<Object> {
@Override
protected void messageReceived(ChannelHandlerContext ctx, Object msg)
throws Exception {
// TODO Auto-generated method stub
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
ctx.close();
}
}
肯定是个小错误。。跪求高人指导呀[ema16]
这是一条镜像帖。来源:北邮人论坛 / java / #29810同步于 2014/5/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
求助一个netty的小问题..
iakuxgnaw
2014/5/8镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复