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

📄 gpsserver.java~32~

📁 一个简单的GPS服务器程序,用来向客户端发送GPS位置信息,还可用于在模拟环境下对GPS客户端进行测试
💻 JAVA~32~
字号:
package webgis.gpsserver;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import webgis.server.service.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.jdom.*;
import javax.xml.parsers.*;

public class GpsServer extends Thread{
  private Log log = LogFactory.getLog("WebGIS");
  private ServletContext servContext;
  private ServerSocket serverSocket;
  private DataOutputStream socketOutput;
  private DataInputStream socketInput;
  private BufferedReader keyBoardIn;

  public GpsServer(){
    try{
      serverSocket = new ServerSocket(4381);
      this.servContext = servContext;
      this.start();
    }catch(Exception e){
      log.error(e);
    }
  }

  //======== 发送数据到Gps客户端 =========
  public void send(String fileName) {
    String XMLString = null;
    int l_iLen;
    byte[] l_bMsg;
    if(fileName == null) {return;}
    try{
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      org.w3c.dom.Document doc = db.parse(fileName);
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();
      StringWriter sw = new StringWriter();
      transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
      transformer.transform(new DOMSource(doc), new StreamResult(sw));
      XMLString = sw.toString();

      log.debug(XMLString);
      l_bMsg = XMLString.getBytes();
      l_iLen = l_bMsg.length;
      socketOutput.writeInt(l_iLen);
      socketOutput.writeByte(13);
      socketOutput.writeByte(10);
      socketOutput.write(l_bMsg);
      socketOutput.writeByte(13);
      socketOutput.writeByte(10);
      socketOutput.writeByte(13);
      socketOutput.writeByte(10);
      socketOutput.flush();
    } catch (Exception e) {log.error(e);}
    log.debug("发往GPS客户端的协议: "+XMLString);
  }

  private String receive() {
    try {
      int l_iLen; //接收的数据的长度
      byte[] l_bMsg; //存放接收到的数据
      String l_strXml = null; //经过转换的字符串数据
      l_iLen = socketInput.readInt();
      if (l_iLen > 0 && l_iLen < Integer.MAX_VALUE) {
        if (socketInput.readByte() != 13)return null;
        if (socketInput.readByte() != 10)return null;
        l_bMsg = new byte[l_iLen];
        for (int i = 0; i < l_iLen; i++) {
          l_bMsg[i] = socketInput.readByte();
        }
        if (socketInput.readByte() != 13)return null;
        if (socketInput.readByte() != 10)return null;
        if (socketInput.readByte() != 13)return null;
        if (socketInput.readByte() != 10)return null;
        l_strXml = new String(l_bMsg);
      }
      return l_strXml;
    } catch (Exception e) {log.error(e);}
    return null;
  }

  public void run(){
    while(true){
      try {
        Socket socket = serverSocket.accept();
        socketInput = new DataInputStream(socket.getInputStream());
        keyBoardIn = new BufferedReader(new InputStreamReader(System.in));
        socketOutput = new DataOutputStream(socket.getOutputStream());
        while(true){
          sleep(100);
          String fileName = "D:/JB/GpsServer/ccc.xml";
          //String fileName = keyBoardIn.readLine();
          this.send(fileName);
        }
      }catch(Exception e){log.error(e);}
    }
  }
  public static void main(String args[]){
    new GpsServer();
  }

  class Servicer extends Thread {
    private String proString = null;

    public Servicer(String proString) {
      this.proString = proString;
      this.start();
    }
    //========== 单独处理每个客户端的请求 =============
    public void run() {
      try {
        /*
        ProtocolDispatch protoDispatch = (ProtocolDispatch) servContext.getAttribute("ProtocolDispatch");
        ProtocolTranslation protoTranslation = (ProtocolTranslation) servContext.getAttribute("ProtocolTranslation");
        proString = protoTranslation.Translation(proString);
        protoDispatch.dispatch(proString);*/
      }catch (Exception e) {log.error(e);}
    }
  }
}

⌨️ 快捷键说明

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