我正在使用netty来更改我们的服务器以支持nio。我是否还需要更改客户端以支持nio,或者他们可以按原样工作。
编辑: 我写了一个小测试,我能够将数据发送到服务器,但是没有收到服务器发送到客户端的数据。
请参阅下面的代码示例: SERVER:
public class NettyNioServer {
private static final int BACKLOG = 1000;
private final int port;
public NettyNioServer(int port){
this.port = port;
}
public void start() throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
final ChannelInitializer<SocketChannel> channelInitializer = new BasicSocketChannelInitializer();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(channelInitializer)
.option(ChannelOption.SO_BACKLOG, BACKLOG)
.option(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
System.out.println("* * * Mediation Server Started on Port: "+port+" * * *");
Main.getLogWrapper1().log(Level.INFO, "* * * Mediation Server Started on Port: "+port+" * * *");
f.channel().closeFuture().sync();
}catch(Exception e){
Main.getLogWrapper1().log(Level.FATAL, "***** Could not listen on Port ****");
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
Main.getLogWrapper1().log(Level.FATAL, sw.toString());
e.printStackTrace();
}finally{
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
public class BasicNioServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String message = (String) msg;
try{
System.out.println(message);
if (message != null && message.length()>0) {
//New Code Start Here
FrameProcessor frameProcessor = new FrameProcessor(null,Main.getLogWrapper1(),new Util());
final String msgResponse = frameProcessor.frameDataProcess(message,"");
final ChannelFuture f = ctx.writeAndFlush(msgResponse+"\r\n");
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
System.out.println("Message Sent to COMM: ACK="+msgResponse);
}
});
}
}finally{
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
Main.getLogWrapper1().log(Level.ERROR, cause.getMessage());
//ctx.close();
}
}
public class BasicSocketChannelInitializer extends ChannelInitializer<SocketChannel>{
private static final StringDecoder STRING_DECODER = new StringDecoder(CharsetUtil.UTF_8);
private static final StringEncoder STRING_ENCODER = new StringEncoder(CharsetUtil.UTF_8);
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//Decoders
pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("stringDecoder", STRING_DECODER);
pipeline.addLast(new BasicNioServerHandler());
//Encoders
pipeline.addLast("stringEncoder", STRING_ENCODER);
}
}
阻止客户:
try{
socket = new Socket("127.0.0.1", 5552);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
System.out.println("Unknown host");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
out.println(text);
System.out.println("Data Sent :" + text);
//Receive text from server
try{
//String line = in.readLine();
byte[] buffer =new byte[100];
int bytes = input.read(buffer);
System.out.println("Data Received From Server :" + new String(buffer));
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
请注意readLine和read(byte [])都不会收到消息。而ChannelFuture将消息打印为成功发送。 我也发送了简单的字符串,字符串+&#34; \ r \ n&#34;,字符串+&#34; \ n&#34;,但我没有在客户端收到消息。
答案 0 :(得分:0)
是。这都是TCP。阻塞/非阻塞/异步是API的属性,而不是协议的属性。