【问题标题】:Unable to create Channel from class io.netty.channel.sctp.nio.NioSctpChannel无法从类 io.netty.channel.sctp.nio.NioSctpChannel 创建通道
【发布时间】:2018-03-09 03:51:59
【问题描述】:
**chatclient.java**
 package com.examplechat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.sctp.nio.NioSctpChannel;
public class ChatClient {   
    public static void main(String[] args) throws InterruptedException, IOException  {
      new ChatClient("localhost", 8000).run();
    }   
    private final String host;
    private final int port;

    public ChatClient(String host, int port) {
        super();
        this.host = host;
        this.port = port;
    }
    public void run() throws InterruptedException, IOException {
  EventLoopGroup group = new NioEventLoopGroup();  
  try {
      Bootstrap bootstrap = new Bootstrap()
              .group(group)
              .channel(NioSctpChannel.class)
              .handler(new ChatClientInitilizer());       
      Channel channel = bootstrap.connect(host, port).sync().channel(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     while(true) {
         channel.write(in.readLine()+ "\r\n");
          }     
    }
  finally {
    group.shutdownGracefully();  } 
  } 
    }
**ChatServer.java**
package com.examplechat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class ChatServer {
public static void main(String[] args) throws InterruptedException  {
        new ChatServer(8000).run();
    }
    private final int port;
    public ChatServer(int port) {
        super();
        this.port = port;
    }
    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();
        }
        finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }       
    }}

我正在尝试使用 netty 创建一个聊天应用程序。当我启动聊天服务器 java 应用程序后尝试运行聊天客户端时。它显示了

  • 线程“主”io.netty.channel.ChannelException 中的异常:无法 从类类创建通道 io.netty.channel.sctp.nio.NioSctpChannel 位于 io.netty.channel.ReflectiveChannelFactory.newChannel (ReflectiveChannelFactory.java:40)

我什至更改了 pom.xml 的依赖项,但每次运行时我仍然收到相同的错误

更新 Error-1: Unable to create Channel error 已被我自己清除

我自己回答了,请帮我解决第二个问题

Error-2我收到一个名为 WARNING 的新错误:Failed to initialize a channel. Closing: [id: 0x9e4e05e0] java.lang.ClassCastException: io.netty.channel.sctp.nio.NioSctpChannel cannot be cast to io.netty.channel.socket.SocketChannel

如果你需要https://github.com/cyborgsri/Chat-Application/tree/master/netty-chat-youtube/src/main/java/com/examplechat,这里是 github 链接。

【问题讨论】:

  • 如果你想让我也包含其他文件,请告诉我

标签: java netty


【解决方案1】:

我遇到了错误

Unable to create Channel from class class io.netty.channel 哪个 是 引起:java.lang.UnsupportedOperationException: ibsctp.so.1:无法打开共享对象文件:没有这样的文件或 目录`

安装 lksctp-tools 后,此错误已通过以下方式清除:

sudo apt-get install lksctp-tools

现在频道已注册。但是我遇到了一个新错误 警告:

Failed to initialize a channel. Closing: [id: 0x9e4e05e0] java.lang.ClassCastException: io.netty.channel.sctp.nio.NioSctpChannel cannot be cast to io.netty.channel.socket.SocketChannel

【讨论】:

    【解决方案2】:

    发生错误是因为您使用:

    public class ChatServerInitializer extends ChannelInitializer<SocketChannel>
    

    改成:

    public class ChatServerInitializer extends ChannelInitializer<Channel>
    

    【讨论】:

    • 太棒了!非常感谢你,诺曼。它现在甚至可以与 一起使用。我尝试在 ChatServer 类中使用 NioSocketCannel 而不是 NioServerSocketChannel
    • 我正在尝试了解这个跟踪项目,在您的示例 ChatServer 应用程序中,我们使用 messageReceived 函数在控制台上显示输出,但在下面的链接中他们使用MainEventHandler.Java 从每个协议的管道中接收数据,但他们只在数据库中存储positionID。我不确定其他详细信息是如何保存的,你能帮我解决这个问题吗? [github.com/tananaev/traccar/tree/master/src/org/traccar]
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 2021-12-13
    • 2014-12-26
    • 2017-02-10
    • 2017-08-22
    • 2016-08-03
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多