From c4d4831297d97ff2d0330858951579ad49884a96 Mon Sep 17 00:00:00 2001 From: Myth Yang Date: Fri, 23 Aug 2013 09:12:50 +0800 Subject: new --- client/Socket-demo/MainActivity.java | 44 ++++++++++++++ client/Socket-demo/Myrequest.java | 10 ++++ client/Socket-demo/Myrespond.java | 12 ++++ client/Socket-demo/SocketClient.java | 107 +++++++++++++++++++++++++++++++++++ client/Socket-demo/Transam.java | 98 ++++++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+) create mode 100644 client/Socket-demo/MainActivity.java create mode 100644 client/Socket-demo/Myrequest.java create mode 100644 client/Socket-demo/Myrespond.java create mode 100644 client/Socket-demo/SocketClient.java create mode 100644 client/Socket-demo/Transam.java (limited to 'client/Socket-demo') diff --git a/client/Socket-demo/MainActivity.java b/client/Socket-demo/MainActivity.java new file mode 100644 index 0000000..ab2ef0c --- /dev/null +++ b/client/Socket-demo/MainActivity.java @@ -0,0 +1,44 @@ +package com.example.test; + +import android.app.Activity; +import android.os.Bundle; +import android.view.Menu; +import android.view.View; +import android.widget.TextView; + +public class MainActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + TextView textview = (TextView)findViewById(R.id.textView1); + textview.setText("Server Created!"); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.main, menu); + return true; + } + + + public void sendMessage(View view) { + + } + + + + + + + + +} + + + + + + diff --git a/client/Socket-demo/Myrequest.java b/client/Socket-demo/Myrequest.java new file mode 100644 index 0000000..fd2ceb1 --- /dev/null +++ b/client/Socket-demo/Myrequest.java @@ -0,0 +1,10 @@ +package com.example.test; + +import java.util.Vector; + +public class Myrequest{ + public Vector contain; + Myrequest(){ + contain.clear(); + } +} \ No newline at end of file diff --git a/client/Socket-demo/Myrespond.java b/client/Socket-demo/Myrespond.java new file mode 100644 index 0000000..6fe4396 --- /dev/null +++ b/client/Socket-demo/Myrespond.java @@ -0,0 +1,12 @@ +package com.example.test; + +import java.util.Vector; + +public class Myrespond{ + public Vector contain; + public String wrong; + Myrespond(){ + wrong = null; + contain.clear(); + } +} \ No newline at end of file diff --git a/client/Socket-demo/SocketClient.java b/client/Socket-demo/SocketClient.java new file mode 100644 index 0000000..df42484 --- /dev/null +++ b/client/Socket-demo/SocketClient.java @@ -0,0 +1,107 @@ +package com.example.test; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; + +public class SocketClient { + + static Socket client; + + public SocketClient(String site, int port)throws UnknownHostException,IOException{ + try{ + client = new Socket(site,port); + }catch (UnknownHostException e){ + throw e; + }catch (IOException e){ + throw e; + } + } + + public Myrespond sendMsg(Myrequest req)throws IOException{ + try{ + DataOutputStream out = new DataOutputStream(client.getOutputStream()); + int tmp = (Integer) req.contain.get(0); + switch(tmp){ + case 0: + String id = (String) req.contain.get(1); + String pass = (String) req.contain.get(2); + out.writeUTF(id); + out.writeUTF(pass); + break; + case 1: + int tk1 = (Integer) req.contain.get(1); + int acc = (Integer) req.contain.get(2); + String mess = (String) req.contain.get(3); + out.writeInt(tk1); + out.writeInt(acc); + out.writeUTF(mess); + break; + case 2: + int tk2 = (Integer) req.contain.get(1); + double slot = (Double) req.contain.get(2); + double slat = (Double) req.contain.get(3); + out.writeInt(tk2); + out.writeDouble(slot); + out.writeDouble(slat); + break; + case 3: + int tk3 = (Integer) req.contain.get(1); + int gid = (Integer) req.contain.get(2); + out.writeInt(tk3); + out.writeInt(gid); + break; + } + out.flush(); + client.shutdownOutput(); + DataInputStream in = new DataInputStream(client.getInputStream()); + int type = in.readUnsignedByte(); + Myrespond r = new Myrespond(); + switch(type){ + case 0: + int id = in.readInt(); + int status = in.readUnsignedByte(); + r.contain.add(0); + r.contain.add(id); + r.contain.add(status); + break; + case 1: + r.contain.add(1); + //reserved + break; + case 2: + r.contain.add(2); + //reserved + break; + case 3: + int n = in.readInt(); + r.contain.add(3); + r.contain.add(n); + for(int i=1;i<=n;i++) + { + int tid = in.readInt(); + double lot = in.readDouble(); + double lat = in.readDouble(); + r.contain.add(tid); + r.contain.add(lot); + r.contain.add(lat); + } + break; + } + return r; + }catch(IOException e){ + throw e; + } + } + public void closeSocket(){ + try{ + client.close(); + }catch(IOException e){ + e.printStackTrace(); + } + } + +} diff --git a/client/Socket-demo/Transam.java b/client/Socket-demo/Transam.java new file mode 100644 index 0000000..d809be1 --- /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 static String outmsg = ""; + 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); + Myrespond 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.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 -- cgit v1.2.3-70-g09d2