如何从客户端获取所有数据(此处客户端是我的rfid机器)

时间:2014-11-12 10:00:15

标签: java client-server client rfid server

我是java的新手。在这里,我向我的rfid机器显示了三张不同的rfid卡。但是我只得到一个正确的ans而且答案是重复我的代码有什么问题, 提前谢谢.....

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Serverc {
    static String s= "55000016910001DB00FB63ABEEAFC1EC888F1030090602111114ABC200000000";
     static ArrayList<String> al=new ArrayList<String>();
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
    public static void connection() throws IOException
    {

      ServerSocket ss=new ServerSocket(9888);//exce
      ss.setSoTimeout(300000000);//exce
      System.out.println("Waiting for client on port " +
                ss.getLocalPort() + "...");
       while(true)
       {

                  Socket server = ss.accept();//exce
                  System.out.println("Just connected to "
                             + server.getRemoteSocketAddress());
                  DataInputStream in =
                  new DataInputStream(server.getInputStream());//exce
                  int input = 0;
                  //BufferedReader br = new BufferedReader(in);

                  byte byteArr[] = new byte[32];
//                example.getBytes();
                  try{
                       while((input = in.read(byteArr)) != -1)//exce
                       {

                         System.out.println("Size read is " + input);
                         System.out.println("Data is " + bytesToHex(byteArr));

                       }


//                       System.out.println("inside finally");
                         server.close();//exce
//                       System.out.println("outside finally");
                   }
                  catch(SocketTimeoutException ex){
                   System.out.println("Socket timed out!");
                 }
                  try {
                    Thread.sleep(1000);
                  } catch (InterruptedException e) {

                   e.printStackTrace();
                  }

      }
    }


    public static void main(String args[]) throws IOException{
        Serverc obj=new Serverc();
        obj.connection();


    }
}

我的控制台如下所示(请注意我在这里写服务器代码客户端是我的RFID机器)

我得到了一个答案而不是三个在这里重复(正确的答案是三个不同的ID,这里我只得到一个id,而且id是正确的,但其他两个没有获得)

Waiting for client on port 9888...
Just connected to /223.185.14.90:7435
Size read is 9
Data is 55000003580000B8E30000000000000000000000000000000000000000000000
Size read is 24
Data is 55000012920001E200600107C47B7D520615031211141F810000000000000000
Size read is 24
Data is 55000012920001E200600107C47B7D520615031211141F810000000000000000
Size read is 24
Data is 55000012920001E200600107C47B7D520615031211141F810000000000000000
Size read is 24
Data is 55000012920001E200600107C47B7D520615031211141F810000000000000000
Size read is 24
Data is 55000012920001E200600107C47B7D520615031211141F810000000000000000

0 个答案:

没有答案