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

📄 forwarderlisten.java

📁 无线传感器网络操作系统源代码
💻 JAVA
字号:
/* * Author: Mike Chen <mikechen@cs.berkeley.edu> * Inception Date: October 22th, 2000 * * This software is copyrighted by Mike Chen and the Regents of * the University of California.  The following terms apply to all * files associated with the software unless explicitly disclaimed in * individual files. * * The authors hereby grant permission to use this software without * fee or royalty for any non-commercial purpose.  The authors also * grant permission to redistribute this software, provided this * copyright and a copy of this license (for reference) are retained * in all distributed copies. * * For commercial use of this software, contact the authors. * * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR * MODIFICATIONS. *///==============================================================================//===   listen.java   ==============================================package net.tinyos.util;import java.util.*;import java.io.*;import java.net.*;/** * * Init the serial port and reads data from it. * * @author  <A HREF="http://www.cs.berkeley.edu/~mikechen/">Mike Chen</A> *		(<A HREF="mailto:mikechen@cs.berkeley.edu">mikechen@cs.berkeley.edu</A>) * @since   1.1.6 * * modified by bwhull to work with the serialforwarder */public class ForwarderListen {  //===========================================================================  //===   CONSTANTS   =========================================================private static final String  CLASS_NAME                  = "listen";private static final String  VERSION     	         = "v0.1";    private static int MSG_SIZE = 36;  // 3 header bytes, 31 msg bytes, 2 crc bytes, 2 strength bytes  //===   CONSTANTS   =========================================================  //===========================================================================  //===========================================================================  //===   PRIVATE VARIABLES   =================================================String strAddr;int nPort;Socket socket;InputStream in;OutputStream out;  //===   PRIVATE VARIABLES   =================================================  //===========================================================================  //===========================================================================  //===   NONLOCAL INSTANCE VARIABLES   =======================================  //===   NONLOCAL INSTANCE VARIABLES   =======================================  //===========================================================================  //===========================================================================  //===   CONSTRUCTORS   ======================================================  /**   * Default constructor.   */  //===   CONSTRUCTORS   ======================================================  //===========================================================================  //===========================================================================  /**   *  .   */public ForwarderListen(String host, String port) {  this.nPort = Integer.parseInt( port );  this.strAddr = host;}  //===========================================================================public boolean open(){    try {        System.out.println ("Connecting to host " + strAddr + ":" + nPort + "\n");        socket = new Socket (strAddr, nPort);        in = socket.getInputStream();        out = socket.getOutputStream();    } catch ( IOException e ) {        System.out.println ("Unable to connect to host\n");        return false;    }    return true;}  //===========================================================================  //===========================================================================public void read() throws IOException {  int i;  int count = 0;  byte[] packet = new byte[MSG_SIZE];  while ((i = in.read()) != -1) {    if(i == 0x7e || count != 0){    	packet[count] = (byte)i;    	System.out.print(Hex.toHex(i) + " ");    	//System.out.print(i + " ");    	count++;	if (count == MSG_SIZE) {      		System.out.println();      		count = 0;	}    }else{	System.out.println("extra byte " + Hex.toHex(i));    }  }}  //===========================================================================  //===   MAIN    =============================================================public static void main(String args[]) {  if (args.length < 2) {    System.err.println("usage: java listen [forwarder address] [port] [msg size (optional - default=" + MSG_SIZE +"]");    System.exit(-1);  }  if ( args.length == 3 ) {    MSG_SIZE = Integer.parseInt( args[2] );  }  boolean bSuccess = false;  System.out.println("\nlisten started");  ForwarderListen reader = new ForwarderListen(args[0],args[1]);  bSuccess = reader.open();  try {    if ( bSuccess ) reader.read();  }  catch (Exception e) {    e.printStackTrace();  }}}//===   MAIN    =============================================================//===========================================================================//////////////////////////////////////////////////class Hex {  public static final String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};  public static String toHex(int i) {    int q = i/16;    int r = i % 16;    return (hex[q] + hex[r]);  }  public static String toHex(byte[] bytes) {    return toHex(bytes, bytes.length);  }  public static String toHex(byte[] bytes, int length) {    String result ="";    for (int i = 0; i < length; i++) {      byte b = bytes[i];      int h = ((b & 0xf0) >> 4);      int l = (b & 0x0f);      result += hex[h] + hex[l] + " ";    }    return result;  }}

⌨️ 快捷键说明

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