2014-09-04 137 views
-1

我使用netty來更改我們的服務器以支持nio。我是否也需要更改客戶端以支持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和讀取(字節[])不接收該消息。而ChannelFuture將郵件打印成發送成功。 此外,我發送簡單的字符串,字符串+「\ r \ n」,字符串+「\ n」,但我沒有收到消息在客戶端。

回答

0

是的。這都是TCP。阻塞/非阻塞/異步是API的屬性,而不是協議的屬性。

+0

請參閱已編輯的問題,讓我知道爲什麼我無法獲得作爲客戶端的響應 – gladiator 2014-09-04 10:00:41

+0

您現在問的是完全不同的問題。您沒有提供任何證據表明有任何事情正在發送。你有雙反斜槓,你應該有單。 – EJP 2014-09-04 10:19:03

相關問題