J'écris un service de recherche de protocole personnalisé fonctionnant sur TCP. Tout fonctionne parfaitement lorsque vous utilisez EmbeddedChannel pour les tests. Pour tester plus loin, j'ai écrit un serveur et ajouter les gestionnaires. À la demande d'un client Socket java simple, le serveur reçoit les données, traite et renvoie une réponse. Cependant, la réponse n'atteint pas le socket client. Je pensais que je me suis trompé avec le grand canalPipeline. Je réduis donc l'implémentation à un seul gestionnaire entrant. Ça ne marche toujours pas. Quelqu'un peut-il aider ici?Simple Netty Server ne pas envoyer de réponse
Serveur:
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();
}
Handler simple:
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();
} }
Client test. Une implémentation non-soignée. Cependant, juste pour le test.
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());
}
}
Je vois. 'read (buffer)! = -1' est vrai si le serveur ferme le socket. Cependant, je ne veux pas que ... Le protocole est basé sur les binaires. La requête et la réponse sont envoyées en binaire avec une taille d'en-tête fixe de 17 octets et un corps optionnel qui est également en binaire. Ai-je besoin d'un délimiteur, lorsque je connais la taille exacte du corps de la réponse à lire dans l'en-tête? Si oui, comment? –