Difference between revisions of "Alien Example Client"
From RifidiWiki
Line 1: | Line 1: | ||
− | |||
You can see a list of possible alien commands by sending the 'help' command to the alien reader. However, not all of these commands are implemented in rifidi. See this [[Alien_9800#Supported Commands | List of supported commands]]. | You can see a list of possible alien commands by sending the 'help' command to the alien reader. However, not all of these commands are implemented in rifidi. See this [[Alien_9800#Supported Commands | List of supported commands]]. | ||
− | + | <pre> | |
/* | /* | ||
* AlienExampleClient.java | * AlienExampleClient.java | ||
Line 31: | Line 30: | ||
public class AlienExampleClient { | public class AlienExampleClient { | ||
− | public static final String IP_ADDRESS = new String( | + | public static final String IP_ADDRESS = new String("127.0.0.1"); |
public static final int READER_PORT = 20000; | public static final int READER_PORT = 20000; | ||
private static Socket connection = null; | private static Socket connection = null; | ||
Line 44: | Line 43: | ||
*/ | */ | ||
private void init() throws IOException, InterruptedException{ | private void init() throws IOException, InterruptedException{ | ||
− | connection = new Socket( | + | connection = new Socket("127.0.0.1", READER_PORT); |
in = new BufferedReader(new InputStreamReader(connection | in = new BufferedReader(new InputStreamReader(connection | ||
Line 52: | Line 51: | ||
Thread.sleep(500); | Thread.sleep(500); | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
− | out.write( | + | out.write("alien\n"); |
out.flush(); | out.flush(); | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
Thread.sleep(500); | Thread.sleep(500); | ||
− | out.write( | + | out.write("password\n"); |
out.flush(); | out.flush(); | ||
System.out.println(readFromReader(in)); | System.out.println(readFromReader(in)); | ||
Line 66: | Line 65: | ||
*/ | */ | ||
private void tearDown() throws IOException{ | private void tearDown() throws IOException{ | ||
− | out.write( | + | out.write("q"); |
out.flush(); | out.flush(); | ||
connection.close(); | connection.close(); | ||
Line 77: | Line 76: | ||
*/ | */ | ||
private String getTags() throws IOException{ | private String getTags() throws IOException{ | ||
− | String command = | + | String command = "t"; |
− | out.write(command + | + | out.write(command + "\n"); |
out.flush(); | out.flush(); | ||
String returnVal = readFromReader(in); | String returnVal = readFromReader(in); | ||
Line 118: | Line 117: | ||
} | } | ||
− | + | </pre> |
Revision as of 19:41, 26 November 2010
You can see a list of possible alien commands by sending the 'help' command to the alien reader. However, not all of these commands are implemented in rifidi. See this List of supported commands.
/* * AlienExampleClient.java * * Created: Dec 6, 2007 * Project: RiFidi Emulator - A Software Simulation Tool for RFID Devices * http://www.rifidi.org * http://rifidi.sourceforge.net * Copyright: Pramari LLC and the Rifidi Project * License: Lesser GNU Public License (LGPL) * http://www.opensource.org/licenses/lgpl-license.html * Author: Kyle Neumeier - kyle@pramari.com */ package sandbox; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * This class shows how to create a program to talk to the alien reader. * * @author Kyle Neumeier - kyle@pramari.com * */ public class AlienExampleClient { public static final String IP_ADDRESS = new String("127.0.0.1"); public static final int READER_PORT = 20000; private static Socket connection = null; private static PrintWriter out = null; private static BufferedReader in = null; /** * Initialize the connection and send username/password * @throws IOException * @throws InterruptedException */ private void init() throws IOException, InterruptedException{ connection = new Socket("127.0.0.1", READER_PORT); in = new BufferedReader(new InputStreamReader(connection .getInputStream())); out = new PrintWriter(connection.getOutputStream()); Thread.sleep(500); System.out.println(readFromReader(in)); out.write("alien\n"); out.flush(); System.out.println(readFromReader(in)); Thread.sleep(500); out.write("password\n"); out.flush(); System.out.println(readFromReader(in)); } /** * Tear down the connection * @throws IOException */ private void tearDown() throws IOException{ out.write("q"); out.flush(); connection.close(); } /** * Get tags back from the alien reader * @return * @throws IOException */ private String getTags() throws IOException{ String command = "t"; out.write(command + "\n"); out.flush(); String returnVal = readFromReader(in); return returnVal; } /** * @param args * @throws InterruptedException * @throws IOException */ public static void main(String[] args) throws IOException, InterruptedException { AlienExampleClient client = new AlienExampleClient(); client.init(); Thread.sleep(500); String tags = client.getTags(); System.out.println(tags); Thread.sleep(500); client.tearDown(); } /** * Read responses from the socket * @param inBuf * @return * @throws IOException */ public static String readFromReader(BufferedReader inBuf) throws IOException{ StringBuffer buf=new StringBuffer(); int ch=inBuf.read(); while((char)ch!='\0'){ buf.append((char)ch); ch=inBuf.read(); } return buf.toString(); } }