我正在寫TCP上運行的自定義協議搜索服務。使用EmbeddedChannel進行測試時,一切正常。爲了進一步測試,我編寫了一個服務器並添加了處理程序。通過普通的Java Socket客戶端的請求,服務器接收到數據,處理併發迴響應。但是,響應沒有到達客戶端套接字。我想可能是我已經搞亂了大通道管道。所以我只將實現簡化爲一個入站處理程序。仍然不起作用。有人可以幫忙嗎?簡單的Netty服務器不發送響應
服務器:
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'成立。但是,我不希望這樣的協議是基於二進制的。請求和響應以二進制形式發送,固定頭大小爲17字節,可選體也是二進制形式。當我知道從頭部讀取的確切響應體大小時,是否需要分隔符?如果是,如何? –