2016-12-18 3 views
-1

rfid 리더와 mysql 데이터베이스를 사용하여 시스템을 만들면 mifare 카드를 사용하여 문 잠금 시스템을 통과시킬 수 있습니다. UI를 이미 작성 했으므로 클라이언트로서버가 클라이언트와 통신 할 수 있도록 thred를 사용하는 방법

예를 메시지를 전송하는 서버. 그런 뭔가를해야만 "당신은 문을 통과은"우리는 우리의 목표를 달성하기 위해 멀티 스레딩을 사용하려고하지만 여기

Here is the error description

코드입니다 중지 계속

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

} 

    } 
DoorClient3#go()에서

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

} 

} 

답변

0

당신은 writer.close()를 사용합니다. 전체 소켓을 닫습니다. 참조 : https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getOutputStream() 의미 : 더 이상 데이터를 소켓의 OutputStream에 쓸 수 없습니다. IncomingReader#run() 메서드의 끝 부분에서만 작성기 (및 소켓)를 닫으십시오.

+0

안녕하세요, 조정했지만 지금은 44 번 라인과 62 번 도어 어댑터에서 연결 재설정을 보여줍니다. –