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

📄 deviceudpservice.java~56~

📁 用java写编写的一个GPS源代码请大家多多指教
💻 JAVA~56~
字号:
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.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";//设备IP默认为本地
  public DatagramSocket udpServer;//发送udp数据包socket
//  private ParseReceiveData ParseData;

  public DeviceUdpService() {
    readxml();//读取CenterInfo.xml获得端口号和IP地址
    initializeOperations();//创建socket对象
  }

  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);//创建udp数据包对象
        udpServer.receive(dp);
        String rcvd = dp.toString();
        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();//封装数据发送IP
          //cDeviceAdd
          msg.cUdpPort = dp.getPort();//封装数据发送端的端断口号
          msg.cUdpSocket = udpServer;//封装接收端的socket对象
          super.addRecieveMsg(msg);//把消息包添加到接受队列
        }
      }
    }
    catch (IOException e) {
      System.err.println("Communication error!");
      e.printStackTrace();
    }

  }

//创建发送udp信息包的socket
  public void initializeOperations() {
    System.out.println("Inside initialization");
    InetAddress ia = null;
    try {
      //ia = InetAddress.getLocalHost();
      ia = InetAddress.getByName(DeviceIP);
      System.out.println("DeviceIP :"+DeviceIP);
      System.out.println("UDP use InetAddress: "+ia.toString());
    }
    catch (UnknownHostException ex) {
      ex.printStackTrace();
      System.exit( -1);
    }
    try {
      //Creates a datagram socket, bound to the specified local address.
      System.out.println("UDP使用IP地址:"DevicePort);
      udpServer = new DatagramSocket(DevicePort,ia);

    }
    catch (SocketException ex1) {
//      udpServer.close();
      ex1.printStackTrace();
      //System.exit( -1);
    }
  }

//读取CenterInfo.xml配置信息
  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();
          System.out.println("DeviceIP:"+DeviceIP);
          try {
            if (strport.length() > 0) {
              DevicePort = Integer.parseInt(strport);
              System.out.println("DevicePort :"+DevicePort);
            }
            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);
    }

  }
}

⌨️ 快捷键说明

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