blob: ed75e52fffbf68a076c9dd42607f6b2dc0ab9324 (
plain) (
tree)
|
|
package com.macaroon.piztor;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Vector;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
public class SocketClient {
static Socket client;
static final int ByteLength = 1;
static final int IntLength = 4;
static final int DoubleLength = 8;
static final int TokenLength = 32;
public SocketClient(String site, int port, int retime) throws UnknownHostException,
IOException {
try {
client = new Socket();
client.connect(new InetSocketAddress(site,port), 5000);
client.setSoTimeout(retime);
} catch (UnknownHostException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
public int sendMsg(Req req,Handler recall) throws IOException,SocketTimeoutException {
try {
DataOutputStream out = new DataOutputStream(
client.getOutputStream());
int tmp = req.type;
int len;
switch (tmp) {
case 0:
ReqLogin rau = (ReqLogin) req;
String id = rau.user;
String pa = rau.pass;
len = IntLength+ByteLength+id.length()+ByteLength+pa.length()+ByteLength;
out.writeInt(len);
out.writeByte(tmp);
out.writeBytes(id);
out.writeByte(0);
out.writeBytes(pa);
out.writeByte(0);
break;
case 1:
ReqUpdate rup = (ReqUpdate) req;
String tk1 = rup.token;
String name1 = rup.uname;
len = IntLength+ByteLength+TokenLength+name1.length()+ByteLength+DoubleLength+DoubleLength;
out.writeInt(len);
out.writeByte(tmp);
double slat = rup.lat;
double slot = rup.lot;
byte[] b = hexStringToBytes(
|