Netty:绑定不匹配
Netty: Bound Mismatch
我是 java 的新手,正在尝试使用 Netty 构建示例 tcp 服务器。这是我目前拥有的
package http_server;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import netty_tutorial.EchoServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
class server
{
ServerBootstrap bootstrap;
int port;
server(int port_)
{
port = port_;
bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup());
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.localAddress(new InetSocketAddress(port));
/**
* Add handlers using anonymous class
*/
/****PROBLEMATIC LINE*****/
bootstrap.childHandler(new ChannelInitializer<SocketChannel>()
{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
System.out.println("hello");
}
}
);
}
}
public class simple_server
{
public static void main(String args[])
{
server server_obj = new server(8080);
}
}
我打算在 initChannel 方法中添加我的处理程序,但不知何故我无法编译当前程序。当我尝试编译这个示例程序时,我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Bound mismatch: The type SocketChannel is not a valid substitute for the bounded parameter <C extends Channel> of the type ChannelInitializer<C>
at http_server.server.<init>
at http_server.simple_server.main
知道到底出了什么问题吗?
您从错误的包中导入了 SocketChannel。
替换import java.nio.channels.SocketChannel;
和import io.netty.channel.socket.SocketChannel;
我是 java 的新手,正在尝试使用 Netty 构建示例 tcp 服务器。这是我目前拥有的
package http_server;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import netty_tutorial.EchoServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
class server
{
ServerBootstrap bootstrap;
int port;
server(int port_)
{
port = port_;
bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup());
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.localAddress(new InetSocketAddress(port));
/**
* Add handlers using anonymous class
*/
/****PROBLEMATIC LINE*****/
bootstrap.childHandler(new ChannelInitializer<SocketChannel>()
{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
System.out.println("hello");
}
}
);
}
}
public class simple_server
{
public static void main(String args[])
{
server server_obj = new server(8080);
}
}
我打算在 initChannel 方法中添加我的处理程序,但不知何故我无法编译当前程序。当我尝试编译这个示例程序时,我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Bound mismatch: The type SocketChannel is not a valid substitute for the bounded parameter <C extends Channel> of the type ChannelInitializer<C>
at http_server.server.<init>
at http_server.simple_server.main
知道到底出了什么问题吗?
您从错误的包中导入了 SocketChannel。
替换import java.nio.channels.SocketChannel;
和import io.netty.channel.socket.SocketChannel;