| | |
| | | System.out.println("InverterPowerUDPServer_Thread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
| | | try {
|
| | | byte[] data = null;
|
| | | int conn_error_count = 0;
|
| | | while(true) {
|
| | | DatagramSocket ds = null;
|
| | | try {
|
| | |
| | | DatagramPacket packet = new DatagramPacket(data, data.length);
|
| | | //监听指定的端口号
|
| | | ds = new DatagramSocket(server_port);
|
| | | //ds.setSoTimeout(5000);
|
| | | ds.setSoTimeout(5000);
|
| | | conn_error_count = 0;
|
| | | while (true) {
|
| | | ds.receive(packet); |
| | | byte[] headCount = new byte[2];
|
| | | |
| | | System.arraycopy(data, 0, headCount, 0, headCount.length);
|
| | | BaseData basedata = new BaseData();
|
| | | ByteBuffer bf = ByteBuffer.allocate(createPackHeadCount(data));
|
| | | if(createPackHeadCount(data) > data.length) {
|
| | | continue; //数据帧超标判断
|
| | | }
|
| | | bf.order(ByteOrder.LITTLE_ENDIAN);
|
| | | bf.put(data,0,createPackHeadCount(data));
|
| | | bf.flip();
|
| | | //BaseData.udpCheck(bf.array());
|
| | | //System.out.println(packet.getSocketAddress());
|
| | | //System.out.println(ds.getLocalSocketAddress().toString());
|
| | | //System.out.println("接收到逆变电源系统:"+ComFn.bytesToHexString(data, createPackHeadCount(data)));
|
| | | //System.out.println("");
|
| | | if(basedata.putNiBianByteBuffer(bf,cfg.isEquie_device_udppackage_cheak())) {
|
| | | //System.out.println(basedata.data.length);
|
| | | String client_ip = packet.getSocketAddress().toString();
|
| | | if(basedata.packtype == BaseData.NiBianPower_DevType) {
|
| | | RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_InverPower, client_ip, bf.array());
|
| | | |
| | | InverterPower_Thread power_thread = new InverterPower_Thread(conn_pool, devices, basedata,client_ip);
|
| | | new Thread(power_thread).start();
|
| | | Dev_UDPServer_Thread.thread_count++;
|
| | | try {
|
| | | ds.receive(packet); |
| | | byte[] headCount = new byte[2];
|
| | | |
| | | System.arraycopy(data, 0, headCount, 0, headCount.length);
|
| | | BaseData basedata = new BaseData();
|
| | | ByteBuffer bf = ByteBuffer.allocate(createPackHeadCount(data));
|
| | | if(createPackHeadCount(data) > data.length) {
|
| | | continue; //数据帧超标判断
|
| | | }
|
| | | bf.order(ByteOrder.LITTLE_ENDIAN);
|
| | | bf.put(data,0,createPackHeadCount(data));
|
| | | bf.flip();
|
| | | //BaseData.udpCheck(bf.array());
|
| | | //System.out.println(packet.getSocketAddress());
|
| | | //System.out.println(ds.getLocalSocketAddress().toString());
|
| | | //System.out.println("接收到逆变电源系统:"+ComFn.bytesToHexString(data, createPackHeadCount(data)));
|
| | | //System.out.println("");
|
| | | if(basedata.putNiBianByteBuffer(bf,cfg.isEquie_device_udppackage_cheak())) {
|
| | | //System.out.println(basedata.data.length);
|
| | | String client_ip = packet.getSocketAddress().toString();
|
| | | if(basedata.packtype == BaseData.NiBianPower_DevType) {
|
| | | RecordUDPPackage.RecordUDPPackageData(RecordUDPPackage.PackageType_InverPower, client_ip, bf.array());
|
| | | |
| | | InverterPower_Thread power_thread = new InverterPower_Thread(conn_pool, devices, basedata,client_ip);
|
| | | new Thread(power_thread).start();
|
| | | Dev_UDPServer_Thread.thread_count++;
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | conn_error_count ++;
|
| | | if(conn_error_count >=6) {
|
| | | break;
|
| | | }
|
| | | //e.printStackTrace();
|
| | | }
|
| | | //测试程序异常情况下,程序能否继续运行
|
| | | //System.out.println(1/0);
|
| | | Thread.sleep(10);
|
| | | }
|
| | | } catch (Exception e) {
|
| | | Thread.sleep(1000);
|
| | | Thread.sleep(100);
|
| | | e.printStackTrace();
|
| | | } finally {
|
| | | if(ds != null) {
|