Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法发送带有netty的对象_Java_Network Programming_Netty - Fatal编程技术网

Java 无法发送带有netty的对象

Java 无法发送带有netty的对象,java,network-programming,netty,Java,Network Programming,Netty,我使用了一些示例来发送和接收字符串,它们工作得非常好。问题是,我试图修改代码(阅读api和示例)以发送可序列化对象,但它们不起作用。我没有收到任何错误消息,通道读取方法从未被调用,所以服务器从未收到消息。我读到它可能与某种纤度计有关,但我找不到任何关于它的线索 这是代码,我不会包括添加和删除的处理程序,因为它们可以工作 服务器初始化器 protected void initChannel(SocketChannel arg0) throws Exception { ChannelPipe

我使用了一些示例来发送和接收字符串,它们工作得非常好。问题是,我试图修改代码(阅读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());

}
主服务器(表示客户端已连接)

最后,没有get/set的消息类

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。我打赌它失败了。

你说得对,先生,它确实失败了。你知道为什么它会失败吗?我认为我写的方法是正确的,原因方法是抛出null,如果失败了,应该是一次性的。你能给我看一下stacktrace吗?它不会扔东西,或者我不知道怎样抓住它。我试图捕获任何异常,但它不会捕获任何异常。我还要求输入isDone(),它表示false,所以这可能就是我没有得到任何异常的原因。感谢您,我在(!future.isDone())之后写了一个循环(future是写入返回的channelfuture,它永远不会出来。现在我明白了…您需要调用channel.writeAndFlush(..)将其实际刷新到套接字中。请参阅apidocs;)
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);       
}