我正在使用Netty服务器/客户端框架。但我遇到了netty客户端的问题。
当我[ClientClass.connect();]
时,客户端和服务器的连接非常好。但是,当我想要停止我的客户端时(不要断开但只是停止保持连接),我不知道如何解决它。
我只想在这里这样
//[ClientClass.connect();]
- >连接到服务器(只是连接)
//[ClientClass.stop();]
- >停止客户
//[ClientClass.start();]
- >重启客户端
public class ContainerClient {
String server;
int port;
String channelName;
public ContainerClient(String server, int port, String channelName) {
this.server = server;
this.port = port;
this.channelName = channelName;
}
public void connect() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap().group(group)
.channel(NioSocketChannel.class)
.handler(new ClientAdapterInitializer());
Channel channel = bootstrap.connect(server, port).sync().channel();
byte[] bytes = channelName.getBytes();
channel.write(bytes);
channel.flush();
while(true){
Scanner in = new Scanner(System.in);
String tmp = in.nextLine();
byte[] t = tmp.getBytes();
channel.write(tmp1);
channel.flush();`
}
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
public static void main(String[] args) {
ContainerClient containerClient = new ContainerClient("localhost",port);
containerClient.connect();
}