summaryrefslogtreecommitdiff
path: root/client/activity-demo/src/com/example/piztor/SocketClient.java
blob: e66b5bc498eed9e5280e09c3e4213ed7b69884fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.example.piztor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {
	static PrintStream cout = System.out;
	static Socket client;

	public SocketClient(String site, int port) throws UnknownHostException,
			IOException {
		try {
			cout.println(site + "   " + port);
			client = new Socket(site, port);
			cout.println("connected successfully!!!");
		} catch (UnknownHostException e) {
			cout.println("unknownhostexception!!");
			throw e;
		} catch (IOException e) {
			cout.println("IOException!!");
			throw e;
		}
	}

	public Myrespond sendMsg(Myrequest req) throws IOException {
		try {
			DataOutputStream out = new DataOutputStream(
					client.getOutputStream());
			int tmp = (Integer) req.contain.get(0);
			out.writeByte(tmp);
			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();
		}
	}

}