我编写了一个名为echo的tcp服务器示例,但它无法通过远程客户端连接。 这是我的代码:
public class EchoServer {
private int port = 8888;
public void startServer() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoDecoder());
ch.pipeline().addLast(new EchoHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = bootstrap.bind(port).sync();
System.out.println("bind completed.");
f.channel().closeFuture().sync();
System.out.println("channel closed.");
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("Closed");
}
}
public static void main(String[] args) throws Exception {
new EchoServer().startServer();
}
}
等待你的回答,谢谢你。
运行环境:windows7 64bit
netty版本:4.0.34