[英]Java: discrepancy between message sent from client and message received at server
[英]Message sent from server not received by client when sending outside ServerHandler
我正在尝试从Netty服务器向连接的客户端发送消息。
我想做的事情与此问题非常相似。 但是,我正在使用io.netty (带有org.jboss.netty。*包的版本) 3.10.0.Final
这是我所做的:
TelnetServer.java包含用于启动服务器的代码。
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContext.newServerContext(ssc.certificate(),
ssc.privateKey());
} else {
sslCtx = null;
}
// Configure the server.
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Configure the pipeline factory.
bootstrap.setPipelineFactory(new TelnetServerPipelineFactory(sslCtx));
// Bind and start to accept incoming connections.
Channel channel = bootstrap.bind(new InetSocketAddress(PORT));
}
TelentServerPipelineFactory.java :
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.ssl.SslContext;
import static org.jboss.netty.channel.Channels.*;
/**
* Creates a newly configured {@link ChannelPipeline} for a new channel.
*/
public class TelnetServerPipelineFactory implements ChannelPipelineFactory {
private final SslContext sslCtx;
public TelnetServerPipelineFactory(SslContext sslCtx) {
this.sslCtx = sslCtx;
}
public ChannelPipeline getPipeline() {
// Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline();
if (sslCtx != null) {
pipeline.addLast("ssl", sslCtx.newHandler());
}
// Add the text line codec combination first,
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new TelnetServerHandler());
pipeline.addLast("downstream", new TelnetServerOutgoingHandler());
return pipeline;
}
}
并在TelnetServerHandler.java中存储了绑定通道时的引用:
public class TelnetServerHandler extends SimpleChannelUpstreamHandler {
private static Channel channel;
public static Channel getChannel() {
return channel;
}
.
.
.
.
@Override
public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
// TODO Auto-generated method stub
channel = ctx.getChannel();
}
}
我试图使用以下代码在TelnetServerHandler外部发送消息:
Scanner scanner = new Scanner(System.in);
System.out.printf("Type message : ");
message = scanner.nextLine();
try{
if(channel.isWritable()){
ChannelFuture future = TelnetServerHandler.getChannel().write(message);
future.sync();
if(!future.isSuccess()){
System.out.println("Send failed : " + future.getCause());
}
}
}catch (Exception e) {
e.printStackTrace();
}
它没有打印错误。 当我调试时, future.isSucess()返回真实值。 客户端已连接到服务器。 但是,客户端从未收到来自服务器的消息。
我在这里想念什么吗? 任何帮助都非常感谢。 提前致谢! :)
我找到了解决方案。
变更:
ChannelFuture future = TelnetServerHandler.getChannel().write(message);
至 :
ChannelFuture future = TelnetServerHandler.getChannel().write(message+"\r\n");
该链接指出它需要行定界符,因此解码器将正确读出消息。
希望能帮助到你。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.