|
@@ -18,46 +18,47 @@ import java.util.Scanner;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class TcpSocketClient {
|
|
public class TcpSocketClient {
|
|
|
|
|
|
|
|
- @Getter
|
|
|
|
|
- private Bootstrap bootstrap;
|
|
|
|
|
- @Getter
|
|
|
|
|
- private Channel channel;
|
|
|
|
|
|
|
+// @Getter
|
|
|
|
|
+// private Bootstrap bootstrap;
|
|
|
|
|
+// @Getter
|
|
|
|
|
+// private Channel channel;
|
|
|
|
|
+//
|
|
|
|
|
+// @Value("${vbdsm.socket.client.port}")
|
|
|
|
|
+// private int port;
|
|
|
|
|
+// @Value("${vbdsm.socket.client.bindIp}")
|
|
|
|
|
+// private String host;
|
|
|
|
|
+//
|
|
|
|
|
+// public void connect(){
|
|
|
|
|
+// EventLoopGroup group = new NioEventLoopGroup();
|
|
|
|
|
+// this.bootstrap = new Bootstrap();
|
|
|
|
|
+// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
|
|
|
+// .option(ChannelOption.TCP_NODELAY, true)
|
|
|
|
|
+// .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
|
|
+//// ch.pipeline().addLast("decoder",new DealMsg()); //设置自定义解码器
|
|
|
|
|
+//// ch.pipeline().addLast("encoder",new MsgEncode()); //设置自定义编码器
|
|
|
|
|
+// ch.pipeline().addLast(new TcpClientHandler());//设置客户端网络IO处理器
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// //连接服务器 同步等待成功
|
|
|
|
|
+// ChannelFuture f = bootstrap.connect(new InetSocketAddress(host, port));
|
|
|
|
|
+// //同步等待客户端通道关闭
|
|
|
|
|
+// try {
|
|
|
|
|
+// this.channel = f.sync().channel();
|
|
|
|
|
+// } catch (InterruptedException e) {
|
|
|
|
|
+// throw new RuntimeException(e);
|
|
|
|
|
+// }
|
|
|
|
|
+// //释放线程组资源
|
|
|
|
|
+// //group.shutdownGracefully();
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// public void sendMsg(String msg){
|
|
|
|
|
+// try {
|
|
|
|
|
+// this.channel.writeAndFlush(msg);
|
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
|
+// throw new RuntimeException(e);
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
- @Value("${vbdsm.socket.client.port}")
|
|
|
|
|
- private int port;
|
|
|
|
|
- @Value("${vbdsm.socket.client.bindIp}")
|
|
|
|
|
- private String host;
|
|
|
|
|
-
|
|
|
|
|
- public void connect(){
|
|
|
|
|
- EventLoopGroup group = new NioEventLoopGroup();
|
|
|
|
|
- this.bootstrap = new Bootstrap();
|
|
|
|
|
- bootstrap.group(group).channel(NioSocketChannel.class)
|
|
|
|
|
- .option(ChannelOption.TCP_NODELAY, true)
|
|
|
|
|
- .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
|
|
-// ch.pipeline().addLast("decoder",new DealMsg()); //设置自定义解码器
|
|
|
|
|
-// ch.pipeline().addLast("encoder",new MsgEncode()); //设置自定义编码器
|
|
|
|
|
- ch.pipeline().addLast(new TcpClientHandler());//设置客户端网络IO处理器
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- //连接服务器 同步等待成功
|
|
|
|
|
- ChannelFuture f = bootstrap.connect(new InetSocketAddress(host, port));
|
|
|
|
|
- //同步等待客户端通道关闭
|
|
|
|
|
- try {
|
|
|
|
|
- this.channel = f.sync().channel();
|
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- }
|
|
|
|
|
- //释放线程组资源
|
|
|
|
|
- //group.shutdownGracefully();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void sendMsg(String msg){
|
|
|
|
|
- try {
|
|
|
|
|
- this.channel.writeAndFlush(msg);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|