我一直在看幾個小時的Netty javadocs,我仍然無法弄清楚這一點。如何打開普通的舊TCP連接,例如到Netty的IRC服務器?如何在Netty中打開TCP連接(客戶端)?
回答
you can find many demo in github of netty
Assumeing您正在使用的服務器的netty5。 您應該將解碼器和編碼器添加到您的管線。 像下面
socketChannel.pipeline().addLast(new StringEncoder() ,new StringDecoder() ,new LineBasedFrameDecoder(1024));
這裏是我的服務器演示
String ip ;
int port = 9999;
NioEventLoopGroup workGroup = new NioEventLoopGroup(8);
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
try {
bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.SO_BACKLOG, 100);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new StringEncoder() ,new StringDecoder() ,new LineBasedFrameDecoder(1024));
socketChannel.pipeline().addLast(new ChannelHandlerAdapter() {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println("the num" +num.getAndIncrement());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("what i say is :" + msg.toString());
ctx.channel().writeAndFlush("from server " + "reply message is " +msg.toString()+"\n");
}
});
}
});
ChannelFuture future = bootstrap.bind(port).sync();
System.out.println("Server start at port : " + port);
future.channel().closeFuture().sync();
}catch (Exception e){
System.out.println("error");
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
那麼你可以使用阻塞套接字進行連接正常。 這裏是我的客戶演示。
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress("localhost" , 9999));
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes());
outputStream.flush();
InputStreamReader reader = new InputStreamReader(inputStream) ;
char [] temChar = new char[40];
StringBuffer buffer = new StringBuffer();
while (reader.read(temChar) != -1){
buffer.append(temChar);
System.out.println(buffer.toString() +"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
然後我發送「你好」,它回覆相同的單詞。
這裏是我的客戶網狀演示
int port = 9999;
Bootstrap bootstrap ;
NioEventLoopGroup workGroup = new NioEventLoopGroup(8);
try {
bootstrap = new Bootstrap();
bootstrap.group(workGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new StringDecoder() ,new StringEncoder() , new LineBasedFrameDecoder(1024), new ChannelHandlerAdapter(){
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("recieve method of client : " +msg.toString());
}
});
}
});
NioSocketChannel nioSocketChannel ;
ChannelFuture future = bootstrap.connect("localhost" , 9999).sync();
//you can also invoke writeAndFlush() in other thread with channel ,
// it is the same as server
System.out.println("try to write hello");
Channel channel = future.channel();
channel.writeAndFlush("hello\n\r");
future.channel().closeFuture().sync(); //it will block until
// you invoke
// channel.close();
System.out.println("finish: " + port);
}catch (Exception e){
e.printStackTrace();
System.out.println("error");
}finally {
workGroup.shutdownGracefully();
}
}
'套接字套接字=新套接字();'嗯等待這些只是普通的Java套接字...我如何使* netty * *客戶*?看到每個人都在談論如何製作服務器,但我還沒有看到有人告訴我如何製作*客戶端*。 – SoniEx2
@ SoniEx2我有更新 –
你可以在github上找到許多演示https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example –
- 1. Netty客戶端連接
- 2. 處理TCP客戶端斷開連接
- 3. 客戶端連接打開時關閉Netty服務器
- 4. TCP客戶端連接
- 5. 如何在netty客戶端連接打開時保持我的jvm退出?
- 6. Netty客戶端不會連接
- 7. 在php的tcp客戶端連接
- 8. 如何在PHP中打開TCP連接
- 9. 在tcp中檢測客戶端斷開連接
- 10. netty tcp bytebuffer服務器和客戶端
- 11. 管理連接的TCP客戶端
- 12. 異步TCP客戶端連接掛斷
- 13. TCP/IP客戶端連接問題
- 14. Android tcp客戶端服務不連接
- 15. JAVA TCP客戶端 - 服務器連接
- 16. TCP客戶端使得第一連接
- 17. 當客戶端關閉TCP連接
- 18. 網絡TCP客戶端連接
- 19. 連接到TCP客戶端Elixir
- 20. 檢查TCP客戶端連接失敗
- 21. 來自客戶端的Android tcp連接
- 22. TCP客戶端連接腳本
- 23. 客戶端斷開連接
- 24. java tcp套接字檢查客戶端斷開連接
- 25. 如何限制客戶端連接的線程netty使用
- 26. 如何限制Netty客戶端的傳出連接數量?
- 27. TCP客戶端斷開
- 28. 如何在netty中斷開連接後自動連接到TCP服務器
- 29. Linux Bash:如何打開一個websocket連接作爲客戶端
- 30. 如何檢測紅寶石中的TCP客戶端斷開連接
你檢查是作爲網狀的一部分的例子嗎?它的全部在那裏。 –
@NormanMaurer我其實並沒有......我讀過javadoc。 – SoniEx2