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 = k;
|
}
|
|
}
|