簡體   English   中英

無法發送帶有凈值的對象

[英]Not able to send an object with netty

我使用了一些示例來發送和接收字符串,並且它們運行良好。 問題是我試圖修改代碼(閱讀api和示例)以發送可序列化的對象,但它們不起作用。 我沒有收到任何錯誤消息,通道讀取方法從不調用,因此服務器從不接收消息。 我讀到它可能與某種距離有關,但我找不到任何線索

這是代碼,我不會添加和刪除處理程序,因為它們可以正常工作

服務器初始化器

protected void initChannel(SocketChannel arg0) throws Exception {
    ChannelPipeline pipeline = arg0.pipeline();
    pipeline.addLast("decoder", new ObjectDecoder(null));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new ChatServerHandler());
}

頻道讀取

public void channelRead0(ChannelHandlerContext arg0, Message message)
        throws Exception {
    Channel incoming = arg0.channel();
    System.out.println("received "+ message.getContent() + "from " + message.getSender() + "\r\n" );            

}

運行服務器

public void run() throws InterruptedException{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try{
        ServerBootstrap bootstrap = new ServerBootstrap()
        .group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .childHandler(new ChatServerInitializer());         
        bootstrap.bind(port).sync().channel().closeFuture().sync();

    }

這是客戶

protected void initChannel(SocketChannel arg0) throws Exception {
    ChannelPipeline pipeline = arg0.pipeline();


    pipeline.addLast("decoder", new ObjectDecoder(null));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new ChatClientHandler());

}

和主服務器(服務器說客戶端已連接)

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

            Channel channel = bootstrap.connect(host,port).sync().channel();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            Message message = new Message();
            message.setReceiver("user");
            message.setSender("user 2");
                    try {
                        message.setContent(in.readLine());

                        channel.write(message);

                        System.out.println("Sent " + System.currentTimeMillis());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

最后,沒有獲取/設置的消息類

public class Message implements Serializable {

private static final long serialVersionUID = 1L;
private String sender;
private String receiver;
private String content;
public Message(String sender, String receiver, String content){     
    this.setSender(sender);
    this.setReceiver(receiver);
    this.setContent(content);       
}

非常感謝您的時間

檢查從寫操作返回的ChannelFuture。 我敢打賭它失敗了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM