diff options
author | Teddy <[email protected]> | 2013-08-23 11:11:23 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2013-08-23 11:11:23 +0800 |
commit | 8d5489095e4b0526e4152f7e3a1e9bc3a758e9bd (patch) | |
tree | b6b2ebca38aad5c63fa92846d6793a9e7ba817ff /client/Socket-demo/Transam.java | |
parent | a44601d57a7b96d69848d5bdd2fa6f3e442d0c85 (diff) | |
parent | 0b0d350bfe97c0101f31d767d54892f7cef05b96 (diff) |
Merge branch 'master' of github.com:Determinant/piztor
Diffstat (limited to 'client/Socket-demo/Transam.java')
-rw-r--r-- | client/Socket-demo/Transam.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/client/Socket-demo/Transam.java b/client/Socket-demo/Transam.java new file mode 100644 index 0000000..6f0fb44 --- /dev/null +++ b/client/Socket-demo/Transam.java @@ -0,0 +1,98 @@ +package com.example.test; + +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.Timer; +import java.util.TimerTask; + +import android.annotation.SuppressLint; +import android.os.Handler; +import android.os.Message; + +public class Transam implements Runnable { + public Timer timer; + public boolean flag = true; + public int cnt = 4; + public int port; + public String ip; + Thread thread; + Myrequest req; + Myrespond res; + Controller core; + + Transam(String i,int p,Myrequest r,Controller c){ + port = p; + ip = i; + req = r; + core = c; + } + + public void run() { + final thd t = new thd(); + flag = false; + thread = new Thread(t); + cnt = 4; + thread.start(); + timer = new Timer(); + TimerTask task = new Timertk(); + timer.schedule(task,2000,2000); + } + + class thd implements Runnable { + @Override + public void run() { + try{ + SocketClient client = new SocketClient(ip,port); + res = client.sendMsg(req); + core.recieveInfo(res); + Message msg = new Message(); + msg.what = 1; + handler.sendMessage(msg); + client.closeSocket(); + }catch (UnknownHostException e){ + }catch (IOException e){ + } + + } + } + + @SuppressLint("HandlerLeak") + Handler handler = new Handler(){ + public void handleMessage(Message msg) { + switch (msg.what) { + case 1: + flag = true; + break; + case 2: + res = new Myrespond(); + res.wrong = msg.obj.toString(); + core.recieveInfo(res); + break; + case 3: + final thd t = new thd(); + thread = new Thread(t); + thread.start(); + break; + } + super.handleMessage(msg); + } + }; + + class Timertk extends TimerTask { + public void run() { + if(flag==false&&cnt>0){ + cnt--; + } + else if(cnt==0) { + Message msg = new Message(); + msg.obj = "connecting failed"; + msg.what = 2; + handler.sendMessage(msg); + timer.cancel(); + } + else if(flag==true){ + timer.cancel(); + } + } + }; +}
\ No newline at end of file |