**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创建聊天应用程序。当我尝试运行聊天客户端时,我启动了chatserver java应用程序。它显示
我甚至更改了pom.xml上的依赖项,但每次运行时我都收到同样的错误
更新 错误-1:
Unable to create Channel error
已被我自己清除
我自己已经回答了,请帮我解决第二个问题
错误-2 :我收到一个名为警告的新错误:
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链接。
答案 0 :(得分:1)
我收到了错误
Unable to create Channel from class class io.netty.channel
是 由引起:java.lang.UnsupportedOperationException: ibsctp.so.1:无法打开共享对象文件:没有这样的文件或 directory`
我通过以下方式安装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
答案 1 :(得分:0)
发生错误是因为您使用:
public class ChatServerInitializer extends ChannelInitializer<SocketChannel>
将其更改为:
public class ChatServerInitializer extends ChannelInitializer<Channel>