We use the rfid reader and mysql database to create a system can let me use my mifare card to pass the door lock system, because we already build an UI, we need to let the server sending message back to client
ex."You pass the door" somthing like that,we try to use multi threading to achieve our goal but it continue stop
Here is the code
public class DoorServer4 {
static{
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("connect ti MySQLToJava");
}
catch (Exception e){
System.out.println("Error loading MySQL Driver");
e.printStackTrace();
}
}
public class EchoThread extends Thread {
PrintWriter out;
String wel;
public EchoThread(Socket ss, String wel) throws IOException{
this.wel = wel;
out = new PrintWriter(new OutputStreamWriter(ss.getOutputStream()));
}
public void printText() throws IOException{
out.println(wel);
out.flush();
}
public void run(){
try {
printText();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new DoorServer4().go();
}
public void go() throws IOException{
String wel = null;
ArrayList<String> list=new ArrayList<String>();
ServerSocket ss = new ServerSocket(25566);
System.out.println("start listening...");
while(true){
Socket socket = ss.accept();
System.out.println("client connected...");
InputStream rawIn = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(rawIn));
String line=null;
while ((line=in.readLine()) != null) {
list.add(line);
//System.out.println(line);
}
System.out.println(list.get(0));
System.out.println(list.get(1));
try{
DBConnection DBCon = new
DBConnection("jdbc:mysql://localhost/lanyang?useUnicode=true&characterEncoding=big5","root","1234");
Connection conn = DBCon.makeConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT `m_name`,`card_num` FROM memberdata WHERE `card_num` LIKE '"+list.get(0)+"%'") ;
if(rs.next()){
String card = rs.getString("card_num");
if(card !="")
wel = "welcome"+rs.getString("m_name");
System.out.println("welcome"+rs.getString("m_name"));
Statement st = conn.createStatement();
String qry1 = "INSERT INTO door VALUES ('" +list.get(0)+"','" +list.get(1)+"')";
st.executeUpdate(qry1);
}
else{
wel = "member not found";
System.out.println("member not found");
}
}
catch(Exception e){
e.printStackTrace();
}
EchoThread echo = new EchoThread(socket, wel);
echo.start();
list.clear();
}
}
}
ss
public class DoorClient3 {
PrintStream writer;
BufferedReader in;
Socket socket;
public static void main(String[] args) throws CardException {
// TODO Auto-generated method stub
DoorClient3 client = new DoorClient3();
client.go();
}
public void go() throws CardException {
setUpNetworking();
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String nowDate = sdFormat.format(new Date());
// Display the list of terminals
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
// Use the first terminal
CardTerminal terminal = terminals.get(0);
// Connect wit hthe card
Card card = terminal.connect("*");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
// Send Select Applet command
ResponseAPDU answer = channel.transmit(new CommandAPDU(new byte[] { (byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00 } ));
System.out.println("answer: " + answer.toString());
// Send test command
answer = channel.transmit(new CommandAPDU(new byte[] { (byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00 } ));
System.out.println("answer: " + answer.toString());
//byte r[] = answer.getData();
String hex = DatatypeConverter.printHexBinary(answer.getBytes());
writer.println((String)hex);
writer.print((String)nowDate);
writer.flush();
writer.close();
// Disconnect the card
card.disconnect(false);
try{
Thread.sleep(10000);
}catch (InterruptedException e){
e.printStackTrace();
}
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
}
private void setUpNetworking() {
try {
socket = new Socket("localhost", 25566);
InputStreamReader rawIn = new InputStreamReader(socket.getInputStream());
in = new BufferedReader(rawIn);
writer = new PrintStream(socket.getOutputStream());
}catch(IOException ex) {
ex.printStackTrace();
}
}
public class IncomingReader implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
String message;
try {
while ((message = in.readLine()) != null) {
System.out.println("read" + message);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
In
DoorClient3#go()
you usewriter.close()
. This closes the whole socket. See: https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getOutputStream() Means: You cannot write any more data to the socket'sOutputStream
. Close your writer (and socket) only at the end of theIncomingReader#run()
method.