0

我使用以下代码连接到设备。

使用这个套接字代码我可以执行所有任务,但是现在我需要在服务器关闭时执行一些功能。我找不到合适的方法,请帮忙。

编辑

我想检测服务器何时与该客户端断开连接,这意味着在进行交易后服务器将断开连接,以便我可以禁用按钮,

void sendRequest(){

  try {
      this.clientSocket=new Socket("192.168.1.11",2000);
        this.os=new DataOutputStream(this.clientSocket.getOutputStream());
        this.in=new DataInputStream(this.clientSocket.getInputStream());

           sendFirtCommand();   
           Client t=new Client();
        t.start();

        }catch(Exception e){
            e.printStackTrace();

        }       
          }// end of the sendRequest

我的线程代码

private class Client extends Thread{
  int time;
  public void run(){
    try{
        while(true){


            //if(in.read()==-1) break;

            int size =in.available();
            if(size>0){

            byte data[]=new byte[size];
            in.readFully(data);
            String str=new String(data);

            // System.out.println(data);
            //char c[]=str.toCharArray();
            str=toHex(data);
            System.out.println(str);
        /*
            if(str.equalsIgnoreCase("050D00E7F0E1000101581D4A1D01FF")){
            System.out.println("Start Left 3"); 

            }

            */

             if(str.equalsIgnoreCase("050d00e7f0e1000101601d4a1d01ff")){
                stopAll();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        enableAll();
                    }
                });
            }
         }
4

1 回答 1

0

如果有帮助,试试这个

try{
        while(true){
             if(str.equalsIgnoreCase("050d00e7f0e1000101601d4a1d01ff")){
                stopAll();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        enableAll();
                    }
                });
            }
         }
   }catch(IOException e)
   {
     handler.post(new Runnable() {
                    @Override
                    public void run() {
                        enableAll();
                    }
                });
   }

您发布的代码中似乎没有异常处理,如果我遗漏了什么,请告诉我......

于 2012-09-17T14:57:38.100 回答