summaryrefslogtreecommitdiff
path: root/misc/client/Socket-demo
diff options
context:
space:
mode:
authorsjtufs <[email protected]>2013-08-25 15:45:55 +0800
committersjtufs <[email protected]>2013-08-25 15:45:55 +0800
commitac7633d8149a28af288ac0b850850cef9b13c151 (patch)
tree861fe3d57c114b5ec7f3141679428cc6302d21bc /misc/client/Socket-demo
parenta53f4d21238d2bccd58a22d7485e54f495dbd55d (diff)
This is alpha
Diffstat (limited to 'misc/client/Socket-demo')
-rw-r--r--misc/client/Socket-demo/Login.java6
-rw-r--r--misc/client/Socket-demo/MainActivity.java44
-rw-r--r--misc/client/Socket-demo/Myrequest.java10
-rw-r--r--misc/client/Socket-demo/Myrespond.java12
-rw-r--r--misc/client/Socket-demo/SocketClient.java107
-rw-r--r--misc/client/Socket-demo/Transam.java125
6 files changed, 304 insertions, 0 deletions
diff --git a/misc/client/Socket-demo/Login.java b/misc/client/Socket-demo/Login.java
new file mode 100644
index 0000000..2dd386c
--- /dev/null
+++ b/misc/client/Socket-demo/Login.java
@@ -0,0 +1,6 @@
+package com.example.test;
+
+public class Login{
+ void success(int t){}
+ void failed(){}
+} \ No newline at end of file
diff --git a/misc/client/Socket-demo/MainActivity.java b/misc/client/Socket-demo/MainActivity.java
new file mode 100644
index 0000000..ab2ef0c
--- /dev/null
+++ b/misc/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/misc/client/Socket-demo/Myrequest.java b/misc/client/Socket-demo/Myrequest.java
new file mode 100644
index 0000000..e5bfce2
--- /dev/null
+++ b/misc/client/Socket-demo/Myrequest.java
@@ -0,0 +1,10 @@
+package com.example.test;
+
+import java.util.Vector;
+
+public class Myrequest{
+ public Vector<Object> contain;
+ Myrequest(){
+ contain = new Vector<Object>();
+ }
+} \ No newline at end of file
diff --git a/misc/client/Socket-demo/Myrespond.java b/misc/client/Socket-demo/Myrespond.java
new file mode 100644
index 0000000..ea6ef3b
--- /dev/null
+++ b/misc/client/Socket-demo/Myrespond.java
@@ -0,0 +1,12 @@
+package com.example.test;
+
+import java.util.Vector;
+
+public class Myrespond{
+ public Vector<Object> contain;
+ public String wrong;
+ Myrespond(){
+ wrong = null;
+ contain = new Vector<Object>();
+ }
+} \ No newline at end of file
diff --git a/misc/client/Socket-demo/SocketClient.java b/misc/client/Socket-demo/SocketClient.java
new file mode 100644
index 0000000..df42484
--- /dev/null
+++ b/misc/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/misc/client/Socket-demo/Transam.java b/misc/client/Socket-demo/Transam.java
new file mode 100644
index 0000000..b5e2381
--- /dev/null
+++ b/misc/client/Socket-demo/Transam.java
@@ -0,0 +1,125 @@
+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 boolean reqlog = false;
+ public int cnt = 4;
+ public int port;
+ public String ip;
+ Thread thread;
+ Myrequest req;
+ Myrespond res;
+ Controller core;
+ Login log;
+
+
+ Transam(String i,int p,Myrequest r,Login l){
+ port = p;
+ ip = i;
+ req = r;
+ log = l;
+ reqlog = true;
+ }
+
+ 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);
+ if(!reqlog){
+ core.recieveInfo(res);
+ }
+ else{
+ if((Integer) res.contain.get(2)==0){
+ int t = (Integer) res.contain.get(1);
+ log.success(t);
+ }
+ else{
+ log.failed();
+ }
+ }
+ 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:
+ if(!reqlog){
+ res = new Myrespond();
+ res.wrong = msg.obj.toString();
+ core.recieveInfo(res);
+ }
+ else{
+ log.failed();
+ }
+ 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