package udp_out;
|
|
|
import Method.BaoWenShow;
|
import Method.SaveFIleInTxt;
|
import PbuliClass.ShowMessage;
|
import PbuliClass.Systems;
|
import home.Open_soft_dialog;
|
|
import java.io.IOException;
|
import java.net.*;
|
import java.util.Vector;
|
|
/**
|
* 2023.07.12 zsh
|
*/
|
public class udpAndTcpout implements Runnable {
|
static int k = 0;
|
static int sleeptime = 1;
|
//ת·¢Êý¾Ý
|
static Vector<udppojo> udppojoVector = new Vector<>();
|
static boolean savebaowen = Systems.sys().getOpensave().equals("1");
|
static DatagramSocket socket = null;
|
static DatagramPacket packet = null;
|
|
static {
|
try {
|
socket = new DatagramSocket();
|
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* Æô¶¯Ï̵߳ķ½·¨
|
*/
|
public void startThread() {
|
Thread t = new Thread(this);
|
t.start();
|
Open_soft_dialog.addara("Æô¶¯Êý¾Ýת·¢Ïß³Ì...");//×Ô¶¯Ôö¼ÓÀúÊ·¹ì¼£±í
|
}
|
|
|
public void run() {
|
while (true) {
|
try {
|
outDisData();
|
Thread.sleep(sleeptime);//ÐÝÃßʱ¼ä
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
|
}
|
}
|
|
|
public static void outDisData() {
|
if (udppojoVector.size() > 10000) {
|
udppojoVector.removeAllElements();
|
} else {
|
if (udppojoVector.size() == 0) {
|
sleeptime = 100;
|
} else {
|
sleeptime = 1;
|
udpout();
|
|
}
|
}
|
}
|
|
|
public static void udpout() {
|
int size = udppojoVector.size();
|
if (size == 0) {
|
return;
|
}
|
|
udppojo udppojo = udppojoVector.get(0);
|
byte[] data = udppojo.getData();
|
int length = udppojo.getLength();
|
InetAddress ip = udppojo.getIp();
|
int port = udppojo.getPort();
|
String message = udppojo.getMessage();
|
String tagid = udppojo.getTagid();
|
try {
|
packet = new DatagramPacket(data, length, ip, port);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
try {
|
socket.send(packet);
|
if (!message.equals("0000") && !tagid.equals("0000")) {
|
BaoWenShow.show(message, "UDPת·¢", tagid);
|
if (savebaowen) {
|
SaveFIleInTxt.save_display(message, tagid);
|
}
|
}
|
} catch (IOException e) {
|
if (k == 0) {
|
ShowMessage.zidingyi(ip + ":" + port + "·¢ËÍʧ°Ü...");
|
k++;
|
}
|
}
|
udppojoVector.remove(udppojo);
|
|
}
|
|
|
public static Vector<udppojo> getUdppojoVector() {
|
return udppojoVector;
|
}
|
|
|
}
|