Bootstrap类没有方法(String host,int port)

时间:2014-10-13 21:20:33

标签: java networking netty

尝试制作测试客户端 - 服务器应用程序并遇到此问题

Channel channel = bootstrap.connect(host, port).sync().channel();

Bootstrap的类没有与arg类型String,int连接的方法。我怎样才能获得频道?

public class ChatClient {

    private final String host;
    private final int port;

    public static void main(String[] args) throws Exception{
        new ChatClient("localhost", 8080).run();
    }

    public ChatClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() throws Exception {
        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));

            while(true){
                channel.write(in.readLine() + "\r\n");
            }
        }
        finally{

        }
    }
}

从此处https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/securechat/SecureChatClient.java

取得此样本

提前致谢

1 个答案:

答案 0 :(得分:0)

它有......确保你依赖4 +的正确netty版本。

请参阅: https://github.com/netty/netty/blob/4.0/transport/src/main/java/io/netty/bootstrap/Bootstrap.java#L95