?? gpsserver.java
字號:
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(500);
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 + -