Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务器是否可以是nio和客户端阻塞_Java_Netty_Nio - Fatal编程技术网

Java 服务器是否可以是nio和客户端阻塞

Java 服务器是否可以是nio和客户端阻塞,java,netty,nio,Java,Netty,Nio,我正在使用netty更改我们的服务器以支持nio。我是否还需要更改客户机以支持nio,或者他们可以按原样工作 编辑: 我写了一个小测试,我能够将数据发送到服务器,但是服务器发送到客户端的数据没有被接收 请参见下面的代码示例: 服务器: 注意readLine和read(字节[])均未收到消息。而ChannelFuture则将消息打印为已成功发送。 我还发送了简单字符串、字符串+“\r\n”、字符串+“\n”,但我没有在客户端收到消息。是。都是TCP。阻塞/非阻塞/异步是API的属性,而不是协议的属

我正在使用netty更改我们的服务器以支持nio。我是否还需要更改客户机以支持nio,或者他们可以按原样工作

编辑: 我写了一个小测试,我能够将数据发送到服务器,但是服务器发送到客户端的数据没有被接收

请参见下面的代码示例: 服务器:

注意readLine和read(字节[])均未收到消息。而ChannelFuture则将消息打印为已成功发送。
我还发送了简单字符串、字符串+“\r\n”、字符串+“\n”,但我没有在客户端收到消息。

是。都是TCP。阻塞/非阻塞/异步是API的属性,而不是协议的属性。

请查看已编辑的问题,并告诉我为什么我无法获得响应,因为客户端您现在问的是一个完全不同的问题。你没有提供任何证据证明有人发送过任何东西。你有两个反斜杠,你应该有一个。
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);
       }