Difference between revisions of "Alien Example Client"

From RifidiWiki

Jump to: navigation, search
(Removing advertising and spam edits)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=[http://yjucofi.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
 
 
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>
+
<pre>
 
/*
 
/*
 
  *  AlienExampleClient.java
 
  *  AlienExampleClient.java
Line 31: Line 30:
 
public class AlienExampleClient {
 
public class AlienExampleClient {
  
public static final String IP_ADDRESS = new String(&quot;127.0.0.1&quot;);
+
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(&quot;127.0.0.1&quot;, READER_PORT);
+
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(&quot;alien\n&quot;);
+
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(&quot;password\n&quot;);
+
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(&quot;q&quot;);
+
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 = &quot;t&quot;;
+
String command = "t";
out.write(command + &quot;\n&quot;);
+
out.write(command + "\n");
 
out.flush();
 
out.flush();
 
String returnVal = readFromReader(in);
 
String returnVal = readFromReader(in);
Line 118: Line 117:
 
}
 
}
  
&lt;/pre&gt;
+
</pre>

Latest revision as of 22:12, 30 June 2014

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();
	}

}

Personal tools