zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tcp;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import tbDataModel.Tb_forward_data;
 
import java.util.concurrent.TimeUnit;
 
import Frame.TcpIpManage;
import JNADell.Dell_usetaganchor;
import Method.BaoWenShow;
import Method.GetNowTime;
import PbuliClass.Systems;
import VectroData.ForwardDatas;
import home.ChuShiHua;
 
/**
 ÓÃÓÚTCPЭÒéÏò·þÎñÆ÷·¢ËÍÐÅÏ¢
 */
public class TcpClientOneToMany {
 
    //¿Í»§¶ËµÄͨµÀ£¬¿ÉÓÃÀ´·¢ËÍÏûÏ¢
    static ChannelFuture channelFuture = null;
    static Tb_forward_data forward_data=ForwardDatas.get_mou_tb_forword("TCP").get(0);
    static String ip=forward_data.getIp();
    public static String getIp() {
        return ip;
    }
 
 
    public static int getPort() {
        return port;
    }
 
 
    static int port=Integer.parseInt(forward_data.getPort());
    static boolean sucss=false;
    static Channel channel;
    static EventLoopGroup workerGroup;
    static boolean acis=false;
    static int k=0;
 
 
    /**
     * Æô¶¯ÁÄÌì¿Í»§¶Ë
     * @param ip ·þÎñÆ÷IP
     * @param port ·þÎñÆ÷¶Ë¿ÚºÅ
     */
    @SuppressWarnings("rawtypes")
    public void bootstrapClient(String ip, int port) {
 
        //¿Í»§¶ËµÄÏß³Ì×é
        workerGroup = new NioEventLoopGroup();
 
        //¿Í»§¶ËÆô¶¯Àà
        Bootstrap boot = new Bootstrap();
 
        try {
            channelFuture = boot
 
                    //´¦ÀíÏß³Ì×é·Ö×é
                    .group(workerGroup)
 
                    //ÉèÖÿͻ§¶ËͨµÀÀàÐÍ
                    .channel(NioSocketChannel.class)
                    //TCP_NODELAY¾ÍÊÇÓÃÓÚÆôÓûò¹ØÓÚNagleËã·¨¡£Èç¹ûÒªÇó¸ßʵʱÐÔ£¬ÓÐÊý¾Ý·¢ËÍʱ¾ÍÂíÉÏ·¢ËÍ£¬¾Í½«¸ÃÑ¡ÏîÉèÖÃΪtrue¹Ø±ÕNagle£»
                    .option(ChannelOption.TCP_NODELAY, true)
                    .option(ChannelOption.SO_KEEPALIVE, true)
 
 
                    //´¦ÀíÆ÷
                    .handler(new ChannelInitializer() {
 
                        //³õʼ»¯¿Í»§¶Ëchannel
                        protected void initChannel(Channel channel) throws Exception {
                            acis=channel.isActive();
 
                            //»ñµÃpipeline
                            ChannelPipeline pipeline = channel.pipeline();
 
                            //¼ÓÈë×Ö·û´®±à½âÂëÆ÷
                            channel.pipeline().addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new StringDecoder());
 
 
                            //¼ÓÈëÐÅÏ¢´¦ÀíÆ÷
                            pipeline.addLast(new ChannelInboundHandlerAdapter() {
 
                                //¶Áµ½·þÎñ¶ËÏûϢʱ
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
                                    //Ö±½Ó´òÓ¡·þÎñ¶ËÏûÏ¢
                                    if(TcpIpManage.getStar() && TcpIpManage.getDatatypeis().equals("TCPת·¢")) {
                                        TcpIpManage.get_text_area().append(ip+","+port+","+GetNowTime.now()+": ÊÕµ½·þÎñÆ÷·¢À´µÄÐÅÏ¢"+message+ "\n");
                                    }
 
                                    //ÊÕµ½µÄ·þÎñÆ÷¶ËÐÄÌø°ü
                                    String megup=message.toString().toUpperCase();
                                    if(megup.startsWith("55BBTCP")) {
                                        int atcp=Systems.getTcpheart_time();
                                        ChuShiHua.setTcp(atcp*10);
                                    }
                                    //½«Êý¾Ý¸øµ½´¦Àí
                                    Dell_usetaganchor.dell_udp_message(megup);
                                }
                            });
                        }
                    })
 
                    //Á¬½Ó·þÎñ¶ËIPºÍ¶Ë¿Ú
                    .connect(ip, port).sync();
            TcpIpManage.get_text_area().append("\n TCPÊý¾Ýת·¢"+ ip+", "+port+"Á¬½Ó³É¹¦......\n");
            ChuShiHua.setTcp(Systems.getTcpheart_time()*10);
            k=0;            
        } catch (Exception e) {
            workerGroup.shutdownGracefully();
        } 
 
    }
 
 
    /**Ïò·þÎñÆ÷·¢ËÍÏûÏ¢*/
    public  static void send(String message) {
        if(channelFuture !=null  ) {
            channelFuture.channel().writeAndFlush(message+"\n");
            BaoWenShow.show(message, "UDPת·¢","Ñ¡Ôñ±êÇ©");
        }        
    }
 
 
 
    public static void startTCPCliet() {        
        new TcpClientOneToMany().bootstrapClient(ip, port);
        send("#INIT,583d2a524f8e486ca8c5d057c631657c");
        if(k!=0) {
            TcpIpManage.get_text_area().append("\nµÚ"+k+"´ÎÁ¬½Ó·þÎñÆ÷£º"+ip+","+port+","+GetNowTime.now());            
        }
        k++; 
    }
 
 
    public static int getK() {
        return k;
    }
 
 
    public static void setK(int k) {
        TcpClientOneToMany.= k;
    }
 
}