9.1
15832144755
2021-09-01 c6d9be2aac4965e847fd958288170e26e79b219d
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
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;
    }
 
}