用java写编写的一个GPS源代码请大家多多指教

源代码在线查看: devicetcpservice.java~65~

软件大小: 3483 K
上传用户: foresnake
关键词: java GPS 编写 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				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.nio.channels.Selector;
				import java.nio.channels.ServerSocketChannel;
				import java.nio.channels.SocketChannel;
				import java.io.IOException;
				import java.net.InetSocketAddress;
				import java.nio.channels.SelectionKey;
				import java.util.Set;
				import java.util.Iterator;
				import java.nio.ByteBuffer;
				import java.nio.charset.Charset;
				import java.nio.charset.CharsetDecoder;
				import java.nio.CharBuffer;
				import com.gps.center.baseclass.HashObj;
				import com.gps.center.baseclass.MsgObj;
				import java.nio.channels.ClosedChannelException;
				
				public class DeviceTcpService extends Thread {
				  public Selector sel = null;
				  public ServerSocketChannel TerminalTcpServer = null;
				  public SocketChannel socket = null;
				  private String result = null;
				//  private ParseReceiveData parseReceiveData;
				  private int centerPort;
				  private int MaxConnection;
				
				  public DeviceTcpService() {
				    readxml();
				//    parseReceiveData = new ParseReceiveData();
				  }
				
				  public void run() {
				    System.out.println("Inside startserver");
				    initializeOperations();
				    System.out.println("Abt to block on select()");
				    SelectionKey acceptKey = null;
				    try {
				      acceptKey = TerminalTcpServer.register(sel,
				                                             SelectionKey.OP_ACCEPT);
				    }
				    catch (ClosedChannelException ex) {
				      ex.printStackTrace();
				    }
				
				    try {
				      while (acceptKey.selector().select() > 0) {
				
				        Set readyKeys = sel.selectedKeys();
				        Iterator it = readyKeys.iterator();
				
				        while (it.hasNext()) {
				          SelectionKey key = (SelectionKey) it.next();
				          it.remove();
				
				          if (key.isAcceptable()) {
				            System.out.println("Key is Acceptable");
				            ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
				            socket = (SocketChannel) ssc.accept();
				            socket.configureBlocking(false);
				            SelectionKey another = socket.register(sel,
				                SelectionKey.OP_READ |
				                SelectionKey.OP_WRITE);
				          }
				          if (key.isReadable()) {
				            System.out.println("Key is readable");
				            String ret = new String( (readMessage(key)).trim());
				            if (ret.length() > 0) {
				              MsgObj msg = new MsgObj();
				              //cMsgtype:1--TermimalSocketMsg,2--DeviceSocketMsg,3--DeviceUdpMsg,4--SmsMsg
				              msg.cMsgType = 2;
				              msg.cMsg = ret;
				              msg.cSelectionKey = key;
				//              msg.cSocket = socket;
				
				//              parseReceiveData.sendmsg(msg);
				
				              System.out.println(ret);
				            }
				          }
				          if (key.isWritable()) {
				            System.out.println("THe key is writable");
				            String ret = readMessage(key);
				            socket = (SocketChannel) key.channel();
				            System.out.println(ret);
				          }
				        }
				        try {
				          Thread.sleep(500);
				        }
				        catch (InterruptedException ex2) {
				          ex2.printStackTrace();
				        }
				      }
				    }
				    catch (IOException ex1) {
				      ex1.printStackTrace();
				    }
				
				  }
				
				  public void initializeOperations() {
				    System.out.println("Inside initialization");
				    try {
				      sel = Selector.open();
				      TerminalTcpServer = ServerSocketChannel.open();
				      TerminalTcpServer.configureBlocking(false);
				      InetAddress ia = InetAddress.getLocalHost();
				      InetSocketAddress isa = new InetSocketAddress(ia, centerPort);
				      TerminalTcpServer.socket().bind(isa);
				    }
				    catch (IOException ex) {
				      ex.printStackTrace();
				    }
				
				  }
				
				  private String readMessage(SelectionKey key) throws IOException {
				    int nBytes = 0;
				    socket = (SocketChannel) key.channel();
				    ByteBuffer buf = ByteBuffer.allocate(1024);
				    try {
				      nBytes = socket.read(buf);
				      buf.flip();
				      Charset charset = Charset.forName("GB18030");
				      CharsetDecoder decoder = charset.newDecoder();
				      CharBuffer charBuffer = decoder.decode(buf);
				      result = charBuffer.toString();
				    }
				    catch (IOException e) {
				      e.printStackTrace();
				      socket.close();
				    }
				    return result;
				  }
				
				  private void readxml() {
				    String strport, strmax;
				    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();
				          strmax = (doc.getElementsByTagName("MaxConnection").item(i).
				                    getFirstChild().
				                    getNodeValue()).trim();
				          try {
				            if (strport.length() != 0) {
				              centerPort = Integer.parseInt(strport);
				            }
				            else{
				              System.out.println("centerPort err");
				              System.exit(-1);
				            }
				
				            if (strmax.length() != 0) {
				              MaxConnection = Integer.parseInt(strmax);
				            }
				            else{
				              System.out.println("MaxDevice 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);
				    }
				  }
				
				}
							

相关资源