2

ソケットで書き込みと読み取りを行うアプリケーションを開発しています。ただし、タスクを11回しか実行していないため、スリープします。

PollThread.java

public class PollThread {

    static String result;
    private static Timer myTimer;
    static String ip = "192.168.1.19";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("PollThread");
        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                ClientThread cThread = new ClientThread(ip);
                String status = cThread.readStatus();
                System.out.println("Staus :: "+status);
            }
        }, 0, 2000);

    }
}

ClientThread.java

public class ClientThread {

    String byte_to_hex, swapped_result, result,ipAddr;
    public Socket s;
    public InputStream i;
    public OutputStream o;
    int status;

    public ClientThread(String ip) {
        // TODO Auto-generated constructor stub
        this.ipAddr=ip;

    }

    public int readStatus() {
        try {
            s = new Socket(ipAddr, 502);
            i = s.getInputStream();
            o = s.getOutputStream();

            byte[] data1 = new byte[1024], packet1 = { (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x06, (byte) 0x01, (byte) 0x01, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x19 };

            o.write(packet1);
            i.read(data1, 0, 1024);//Comment this
            byte_to_hex = bytesToHex(data1).substring(18, 26);

            char[] arr = byte_to_hex.toCharArray();
            for (int i = 0; i < arr.length - 1; i += 2) {
                char temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }

            swapped_result = new String(arr);

            result = hexStringToNBitBinary(swapped_result, 32);

            int counter = 0;
            for (int i = 0; i < result.length(); i++) {
                if (result.charAt(i) == '1') {
                    counter++;
                }
            }
            status = counter;

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return status;
    }
}

結果

Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1
Staus :: 1

コメントアウトするi.read(data1, 0, 1024);と問題なく動作しますが、結果を得るにはこの行が必要です。

これには何が問題になる可能性がありますか?なぜ11回しか実行されないのですか?

更新-1

Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...
Status :: 1
Reading status...

更新 2

cThread.start();この行をPollThread.javaファイルに追加してみました が、結果は同じです。

4

1 に答える 1

1

他のコメンターが言及したように、定期的にテストする単一の接続とは対照的に、Timer は実際には複数のクライアント接続を2 秒ごとに作成しています。「サーバー シミュレーター」の最大接続数が 10 または 11 である可能性は十分にあります。毎回作成されないように、クライアント接続を TimerTask の外に移動してみてください。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("PollThread");
    myTimer = new Timer();
    final ClientThread cThread = new ClientThread(ip);
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            String status = cThread.readStatus();
            System.out.println("Staus :: "+status);
        }
    }, 0, 2000);
}

readStatus メソッド内で新しい Socket を作成していることに気付きましたが、これは基本的に同じ複数接続の問題です。次のように、それをコンストラクターに移動してみてください。

public ClientThread(String ip) {
    // TODO Auto-generated constructor stub
    this.ipAddr=ip;

    try {
        s = new Socket(ipAddr, 502);
        i = s.getInputStream();
        o = s.getOutputStream();
    // ...
}

public int readStatus() {
    try {
        //s = new Socket(ipAddr, 502);
        //i = s.getInputStream();
        //o = s.getOutputStream();
于 2013-03-21T11:12:46.037 に答える