⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deviceudpservice.java~64~

📁 用java写编写的一个GPS源代码请大家多多指教
💻 JAVA~64~
字号:
package com.gps.center.dataservice;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.File;
import org.w3c.dom.NodeList;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.io.IOException;
import com.gps.center.baseclass.HashObj;
import com.gps.center.baseclass.MsgObj;
import com.gps.center.baseclass.ParseData;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class DeviceUdpService
    extends ParseData
    implements  Runnable {
//  private MsgObj msg;
  private int DevicePort = 0;
  private String DeviceIP = "localhost";
  public DatagramSocket udpServer;
  //  private ParseReceiveData ParseData;

  public DeviceUdpService() {
    readxml();
    initializeOperations();
  }

  public void run() {

//    nBytes = socket.read(buf);
//    buf.flip();
//    Charset charset = Charset.forName("GB18030");
//    CharsetDecoder decoder = charset.newDecoder();
//    CharBuffer charBuffer = decoder.decode(buf);
//    result = charBuffer.toString();

    try {
      while (super.openservice) {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);//buf.length
        udpServer.receive(dp);
        String rcvd = dp.toString();
        System.out.println(rcvd.length());
        if (rcvd.length() > 1) {
          MsgObj msg = new MsgObj();
          //cMsgtype:1--TCPMSG,2--DEVICEUDPMSG,3--SMSMSG
          //4--TCPSENDTODEVICE,5--UDPSENDTODEVICE,6--SMSSENDTODEVICE,7--TCPSENDTOTERMINAL,8--TCPRETURNTERMINAL
//          System.out.println("Recieve :"+rcvd);
          msg.cMsgType = DEVICEUDPMSG;
          msg.cMsg = rcvd;
//          msg.cUdpIP = dp.getAddress().getHostAddress();
          msg.cDeviceAdd = dp.getAddress();
          //cDeviceAdd
          msg.cUdpPort = dp.getPort();
          msg.cUdpSocket = udpServer;
          super.addRecieveMsg(msg);
        }
      }
    }
    catch (IOException e) {
      System.err.println("Communication error!");
      e.printStackTrace();
    }

  }

  public void initializeOperations() {
    System.out.println("Inside initialization");
    InetAddress ia = null;
    try {
      //ia = InetAddress.getLocalHost();
      ia = InetAddress.getByName(DeviceIP); //.getByName(DeviceIP);
      System.out.println("UDP use InetAddress: "+ia.toString());
    }
    catch (UnknownHostException ex) {
      ex.printStackTrace();
      System.exit( -1);
    }
    try {
      udpServer = new DatagramSocket(DevicePort, ia);
//      udpServer.set
    }
    catch (SocketException ex1) {
//      udpServer.close();
      ex1.printStackTrace();
      System.exit( -1);
    }
  }

  private void readxml() {
    String strport;
    try {
      File f = new File("CenterInfo.xml");
      if (f.exists()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(f);
        NodeList nl = doc.getElementsByTagName("Center");
        for (int i = 0; i < nl.getLength(); i++) {
          strport = (doc.getElementsByTagName("CenterPort").item(i).
                     getFirstChild().
                     getNodeValue()).trim();
          DeviceIP= (doc.getElementsByTagName("CenterIP").item(i).
                     getFirstChild().
                     getNodeValue()).trim();
          try {
            if (strport.length() > 0) {
              DevicePort = Integer.parseInt(strport);
            }
            else {
              System.out.println("centerPort err");
              System.exit( -1);
            }

          }
          catch (Exception e) {
            System.out.println("centerPort and MaxDevice err");
          }
        }
      }
      else {
        System.out.println("no find file:CenterInfo.xml");
        System.exit( -1);
      }

    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit( -1);
    }
//    try {
//      this.CenterIp = InetAddress.getLocalHost().getHostAddress();
//    }
//    catch (UnknownHostException e) {
//      e.printStackTrace();
//    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -