有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从服务器发送的java消息在发送到ServerHandler外部时未被客户端接收

我正在尝试将消息从netty服务器发送到连接的客户端

我想做的与this question非常相似。 然而,我使用的是io。netty(带有org.jboss.netty.*包的那一个)版本3.10.0。决赛

以下是我所做的:

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();
}

它没有打印错误。当我调试时,未来。issuccess()返回true值。客户端已连接到服务器。然而,客户端从未从服务器收到消息

我错过什么了吗?非常感谢您的帮助。提前感谢!:)


共 (1) 个答案

  1. # 1 楼答案

    我找到了解决办法

    改变:

    ChannelFuture future = TelnetServerHandler.getChannel().write(message);
    

    致:

    ChannelFuture future = TelnetServerHandler.getChannel().write(message+"\r\n");
    

    This link指出它需要行分隔符,以便解码器正确地读取消息

    希望有帮助。:)