2017-08-03 1483 views
1

我有兩臺服務器「A」(由我的朋友建造)和「B」(由我建造,使用Netty 4.1)。這個服務器「A」和「B」會在客戶端發送命令時返回一些響應。我試圖使用簡單的JAVA客戶端套接字服務器。下面是java客戶端代碼:Netty:保持服務器套接字打開後ctx.flush()

public class SocketClient 
{ 
    public void run() 
    { 
     try { 
      ClassLoader classLoader = getClass().getClassLoader(); 
      File file = new File(classLoader.getResource("request.txt").getFile()); 
      String content = new String(Files.readAllBytes(file.toPath())); 

      System.out.println("Connecting to server:8888"); 
      Socket socket = new Socket("myserver.com", 8888); 
      DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

      out.write(content.getBytes()); 
      out.flush(); 

      while(true) { 
       int response = in.read(); 
       System.out.println(response); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

當客戶端連接到服務器A(非網狀),該read()方法給我這樣的輸出:

48 
48 
57 
56 
48 
50 
49 
48 

但是,當我嘗試連接到服務器「B」(這是使用網狀),輸出是這樣的:

48 
48 
57 
56 
48 
50 
49 
48 
-1 
-1 
-1 
-1 

爲什麼會發生這種情況?在服務器B的辦法,我用ctx.write() and ctx.flush(),以及如何使服務器B獲得與服務器A相同的行爲(不關閉連接,所以它不會返回-1)

編輯:附加信息

其實在服務器「B」中,我使用ChannelInboundHandlerAdapterChannelOutboundHandlerAdapter。做了一些實驗後,問題出現在ChannelOutboundHandlerAdapter。當我使用InboundAdapterctx.writeAndFlush()發送響應時,套接字未關閉。但是,當響應傳遞到OutboundAdapter,然後我發送迴應使用ctx.writeAndFlush()OutboundAdapterwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)方法。關閉套接字

這裏是我OutboundAdapter的樣本:

public class OutboundHandler extends ChannelOutboundHandlerAdapter { 

    private static final Logger logger = LogManager.getLogger(OutboundHandler.class); 

    //Sending or forwarding message to channel which is already configured 
    //condition is depends on which model instance 
    //packaging is worked on its packager class 
    @Override 
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception 
    { 
     String response = "Response form OutboundHandler"; 
     ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes())); 
    } } 

感謝

回答

0

經過全天調試,我發現問題的根本原因。我不小心把ctx.close()某處我InboundAdapter,所以當我將消息傳遞到OutboundAdapter和沖洗消息給客戶端,將關閉連接

try { 
    //some code 
} 
} catch (Exception ex) { 
     logger.error(ex); 
     ErrorData error = new ErrorData(); 
     error.setCode("99"); 
     error.setMessage("General System Error"); 

     //pass the message to OutboundAdapter 
     ctx.writeAndFlush(error); 
} finally { 
    ctx.close(); 
} 

所以,我只需要去除ctx.close()和服務器從停止關閉socket連接

0

在Netty的服務器實施,也許你必須處理讀/通過ChannelHandlerContext更正確,例如寫作,而不是使用ctx.write()ctx.channel().writeandflush()應該是更合適的方式,也是在成交ctx.channel().close()ctx.channel().close().sync()可以使不同的使用......

+0

'ctx.writeAndFlush()'和'ctx.channel.writeAndFlush()'有什麼區別?我的問題是netty總是在'writeAndFlush()'後自動關閉連接,所以客戶端無法發送另一條消息到服務器。 –

+0

雖然他們應該在實現相同的ChannelOutboundInvoker接口的兩種方式相同,但您的問題我投資netty的一些例子,可能是有用的https://developer.jboss.org/wiki/NettyExampleOfPingPongUsingObject –

+0

我使用netty的方式。 io(版本4.x)而不是jboss netty(版本3。x),我不知道他們是否有顯着差異。但謝謝你的建議......哦,我試圖改變'ctx.write(); ctx.flush()'轉換爲'ctx.channel()。writeAndFlush()',但套接字仍然自動關閉。 –

0

您不檢查流結束,所以您無休止地迴應它。 read()返回。覈實。它在流結束時返回-1。這就結束了。菲尼斯。 Finito。停在那裏。不要打印它。不要繼續閱讀。不要通過GO。不要收200美元。

相關問題