TCP上で実行されているカスタム・プロトコル検索サービスを作成しています。 EmbeddedChannelを使用してテストする場合、すべてが完璧に機能します。さらにテストするために、私はServerを書いてハンドラを追加しました。プレーンなJavaソケットクライアントからの要求で、サーバーはデータを受信し、処理して応答を返します。ただし、応答はクライアントソケットに到達しません。私は大規模なchannelPipelineを使いこなしたかもしれないと思った。ですから、インバウンド・ハンドラにのみ実装を減らします。まだ動作しません。誰かがここで助けることができますか?シンプル・ネット・サーバーがレスポンスを送信しない
サーバー:
public void start() throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
final KaiExceptionHandler kaiExceptionHandler = new KaiExceptionHandler();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new SimpleHandler());
}
});
ChannelFuture future = b.bind(new InetSocketAddress("localhost", 9400)).sync();
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if(channelFuture.isSuccess()) {
LOGGER.info("Kai Server is bounded to '{}'", "localhost:9400");
}else {
LOGGER.error("Failed to bound Kai to 'localhost:9400'", channelFuture.cause());
}
}
});
future.channel().closeFuture().sync();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
シンプルハンドラ:
public class SimpleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Charset charset = Charset.defaultCharset();
ctx.write(Unpooled.copiedBuffer("Client is not seeing this", charset));
ctx.flush();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} }
テストクライアント。きちんとした実装ではありません。しかし、テストのためだけです。
public class TestClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 9400);
InputStream is = socket.getInputStream();
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[64];
int r = 0;
socket.setSoTimeout(10000);
System.out.println("Reading...");
while ((r = is.read(buffer)) != -1) {
sb.append(new String(buffer).trim());
}
System.out.println("String: " + sb.toString());
}
}
。 'read(buffer)!= -1'は、サーバがソケットを閉じる場合にtrueを保持します。しかし、私はそれを望んでいません。プロトコルはバイナリベースです。要求と応答は17バイトの固定ヘッダーサイズとバイナリ形式の任意の本文でバイナリ形式で送信されます。ヘッダーから読み取る正確な応答の本文サイズを知っているときに区切り記号が必要ですか?はいの場合、どうですか? –