package com.hxzkoa.udp;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.net.DatagramPacket;
|
import java.net.DatagramSocket;
|
import java.net.InetAddress;
|
import java.net.SocketException;
|
import java.net.UnknownHostException;
|
import java.util.Vector;
|
|
import com.hxzkoa.json.tb_forward_data;
|
|
/** 该类用于UDP转发数据到指定端口地址 */
|
public class Udp_Out {
|
/**
|
* 实现UDP转发
|
*
|
* @param data
|
* 需要转发的数据
|
* @param lenth需要转发的数据长度
|
* @param type需要转发的数量类型
|
* @throws IOException
|
*/
|
|
/**将数据发送cs端*/
|
public static void udp_to_cs(String message) {
|
DatagramPacket packet=null;
|
DatagramSocket socket =null;
|
InetAddress address=null;
|
int port = 7000;
|
String ip = "127.0.0.1";
|
byte[] data = null;
|
try {
|
data = message.getBytes("gbk");
|
} catch (UnsupportedEncodingException e1) {
|
// TODO 自动生成的 catch 块
|
e1.printStackTrace();
|
}
|
int length=data.length;
|
|
try {
|
address=InetAddress.getByName(ip);
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
//如果数据类型相同
|
try {
|
socket = new DatagramSocket();
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
packet=new DatagramPacket(data, length, address, port);
|
|
|
try {
|
socket.send(packet);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
socket.close();
|
}
|
|
public static void udp_out(byte[] data, int length, String type) {
|
|
// 如果存在转发全部数据的对象
|
if (type.equals("全部数据")) {
|
}
|
|
Vector<tb_forward_data> mou_data = ForwardDatas.get_mou_tb_forword("UDP", type);
|
|
DatagramPacket packet = null;
|
DatagramSocket socket = null;
|
|
if (mou_data.size() != 0) {
|
|
InetAddress address = null;
|
for (int i = 0; i < mou_data.size(); i++) {
|
tb_forward_data fordata = mou_data.get(i);
|
// 如果数据类型相同
|
try {
|
address = InetAddress.getByName(fordata.getIp());
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
int port = Integer.parseInt(fordata.getPort());
|
try {
|
socket = new DatagramSocket();
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
packet = new DatagramPacket(data, length, address, port);
|
|
try {
|
socket.send(packet);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
socket.close();
|
}
|
}
|
}
|
|
/** 将数据发给基站 */
|
public static String out(byte[] data, int length, String ip, String udp_out, String wifi_model) {
|
String datas = null;
|
DatagramPacket packet = null;
|
InetAddress address = null;
|
// int port=Integer.parseInt(Systems.sys().getUdp_out());
|
int port = Integer.parseInt(udp_out);
|
DatagramSocket socket = null;
|
// if(Systems.sys().getWifi_model().equals("1")) {
|
if (wifi_model.equals("1")) {
|
port = 8234;
|
}
|
|
// 如果数据类型相同
|
try {
|
address = InetAddress.getByName(ip);
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
|
try {
|
socket = new DatagramSocket();
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
|
packet = new DatagramPacket(data, length, address, port);
|
|
try {
|
// if (Systems.sys().getWifi_model().equals("1")) {
|
if (wifi_model.equals("1")) {
|
/* Udp_Receive.getSocket().send(packet); */
|
} else {
|
socket.send(packet);
|
}
|
|
datas = Tools.Bytes2HexString(data).substring(0, packet.getLength() * 2);
|
// if (TcpIpManage.getStar()) {
|
// if (TcpIpManage.getDatatypeis().equals("UDP转发")) {// 显示字符串格式
|
// TcpIpManage.get_text_area().append(" 发-->" + ip + " 数据:" + datas
|
// + "\n");
|
// TcpIpManage.get_text_area().setCaretPosition(TcpIpManage.get_text_area().getText().length());
|
// }
|
// }
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
return datas;
|
}
|
|
}
|