まず最初に、これを読んでくれてありがとう。
私は現在、非常に奇妙なバグに悩まされています。私はJavaには少し慣れていますが、Nettyは初めてです。例の SecureChat に似たチャット プログラムを作成しています。
私の問題は次のとおりです。サーバーを起動できます。サーバーはエラーなしで正しいアドレスにバインドします。しかし、クライアントを起動すると、サーバーに接続して数秒待ってから終了し、エラーの種類を指示するコンソール出力はありません。チャネルは確立され、アクティブになっているように見えます。少なくとも、対応するメソッドが呼び出されます。チャネル/接続がアクティブな場合、サーバーはグリーティング パケットを送信する必要があります。このグリーティング パケットは、クライアントによって受信されていません。または、少なくともそうではないようです。
ChatServer.java:
package main;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
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.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
public class ChatServer {
private final int port;
public ChatServer(int port){
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
new ChatServerHandler());
}
});
b.bind(port).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0){
port = Integer.parseInt(args[0]);
} else {
port = 3000;
}
new ChatServer(port).run();
}
}
ChatServerHandler.java:
package main;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import net.Packet;
import net.server.ChannelIntercommunicationHandler;
import net.server.PacketGreetings;
public class ChatServerHandler extends ChannelInboundMessageHandlerAdapter<Packet>{
private static final Logger logger = Logger.getLogger(ChatServerHandler.class.getName());
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception{
ctx.write(new PacketGreetings("Hello! Welcome in Chat. Please log in"));
ChannelIntercommunicationHandler.addChannel(ctx.channel());
logger.log(Level.INFO, "Sent greetings packet to User with ChannelID: "+ctx.channel().id());
}
@Override
public void messageReceived(ChannelHandlerContext ctx, Packet request) throws Exception {
ChannelIntercommunicationHandler.packetReceived(ctx.channel(), request);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("Error, Exiting.");
logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
ChannelIntercommunicationHandler.cleanupChannel(ctx.channel());
ctx.close();
}
}
ChatClient.java
package main;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
public class ChatClient implements Runnable{
private final String host;
private final int port;
public ChatClient(String host, int port){
this.host = host;
this.port = port;
}
@Override
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
new ChatClientHandler());
}
});
b.connect(host, port).sync().channel().closeFuture().sync();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new ChatClient("localhost", 3000).run();
}
}
ChatClientHandler.java:
package main;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.Packet;
import net.client.ClientPacketHandler;
import net.client.PacketLoginCredentials;
public class ChatClientHandler extends ChannelInboundMessageHandlerAdapter<Packet>{
private static final Logger logger = Logger.getLogger(ChatClientHandler.class.getName());
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.log(Level.INFO, "Channel Active with with ChannelID: "+ctx.channel().id());
ctx.write(new PacketLoginCredentials("martin", "123456"));
}
@Override
public void messageReceived(ChannelHandlerContext ctx, Packet p) throws Exception{
logger.log(Level.INFO, "messageReceived with ClassEntity: "+p.getClass().getName());
ClientPacketHandler.packetReceived(p, ctx.channel());
}
}
私はすでにサーバーにtelnetしようとしました。接続が確立され、すぐに閉じられます。サーバーの問題だと思います。
間違いを2日間探していますが、見つかりません。
どんな助けでも大歓迎
ありがとうございました