2014-01-24 2 views
0

나는 netty로 약간 연주했으며 채팅 서버와 클라이언트를 구축하기 위해 비디오 (https://www.youtube.com/watch?v=tsz-assb1X8)를 따라갔습니다. 서버가 제대로 작동합니다 (텔넷으로 테스트 한 결과 작동합니다). 그러나 클라이언트는 데이터를 수신하지 않습니다. . ChatClinetHandler.java의 channelRead 메소드는 결코 호출되지 않았지만 channelReadComplete가 호출되었습니다.Netty channelRead는 결코 호출되지 않았습니다.

ChatClient.java

import io.netty.bootstrap.Bootstrap; 
import io.netty.channel.Channel; 
import io.netty.channel.EventLoopGroup; 
import io.netty.channel.nio.NioEventLoopGroup; 
import io.netty.channel.socket.nio.NioSocketChannel; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.InetSocketAddress; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ChatClient { 

    public static void main(String args[]) { 
     try { 
      new ChatClient(new InetSocketAddress("localhost", 8000)).run(); 
     } catch (Exception ex) { 
      Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    private final InetSocketAddress server; 

    public ChatClient(InetSocketAddress server) { 
     this.server = server; 
    } 

    public void run() throws Exception { 
     EventLoopGroup group = new NioEventLoopGroup(); 
     try { 
      Bootstrap bootstrap = new Bootstrap() 
        .group(group) 
        .channel(NioSocketChannel.class) 
        .handler(new ChatClientInitializer()); 

      Channel channel = bootstrap.connect(server).sync().channel(); 
      System.out.println("Connected to Server: " + server.toString()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
      while (channel.isActive()) { 
       String userMessage = in.readLine(); 
       channel.writeAndFlush(userMessage + "\r\n"); 
       if (userMessage.equalsIgnoreCase("bye")) { 
         group.shutdownGracefully(); 
         break; 
       } 

      } 
     } finally { 
      group.shutdownGracefully(); 
     } 
    } 
} 

ChatClientInitializer.java

import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; 

public class ChatClientInitializer extends ChannelInitializer<SocketChannel> { 

    @Override 
    protected void initChannel(SocketChannel c) throws Exception { 
     ChannelPipeline pipeline = c.pipeline(); 
     pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); 
     pipeline.addLast(new StringDecoder()); 
     pipeline.addLast(new StringEncoder()); 
     pipeline.addLast(new ChatClientHandler()); 
    } } 

ChatClinetHandler.java 내가 보내 깜빡

import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 


public class ChatClientHandler extends ChannelInboundHandlerAdapter { 
    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
     System.out.println(msg.toString()); 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, 
      Throwable cause) { 
     cause.printStackTrace(); 
    } 

} 

답변

0

미안 해요 내 실수 "\ 연구 \ n"은 각 메시지 .

0

비록 shim_ claims to have determined that the problem is not sending "\r\n" with each message이지만 여기에 설명을 제공 할 필요가 있습니다. 그 구체적인 예에서 줄 구분 기호와 DelimiterBasedFrameDecoder 객체이다 프레이머라는 핸들러를 참조 ChatClientInitializer.java에서

(DelimiterBasedFrameDecoder에 대한 자세한 정보는 here을 찾을 수 있습니다).

그래서 클라이언트는 해당 채널에서 "\ r \ n"으로 끝나는 메시지를 기다리고 있음을 의미합니다. "\ r \ n"으로 끝나지 않는 메시지가 수신되면 channelRead() 메서드가 실행되지 않습니다.

관련 문제