关闭

网络代理服务Agent,服务端.client 连接server 进行通信

标签: Androidsocketserverclient
210人阅读 评论(0) 收藏 举报
分类:

package com.example.agentserv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


public class AgentServ extends Service {


private static final String TAG = "AgentServ";
private static final int PORTNUMER = 60000; // port
Context context = AgentServ.this;
private acceptThread macceptThread; // thread
private receiveThread mreceiveThread;
private boolean stop = true;
Socket client = null;


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.e(TAG, "oncreate server");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e(TAG, "AgentServ onStartCommand");


macceptThread = new acceptThread();
macceptThread.setPriority(Thread.MAX_PRIORITY);
macceptThread.start();


Log.e(TAG, "intent = " + intent + "startid-->" + startId);
return Service.START_NOT_STICKY; 


}


// 接收客户端
class acceptThread extends Thread {
public acceptThread() {
// TODO Auto-generated constructor stub
Log.e(TAG, "acceptThread");
}


@Override
public void run() {
// TODO Auto-generated method stub
try {
@SuppressWarnings("resource")
ServerSocket serversocket = new ServerSocket(PORTNUMER);
Log.e(TAG, "wait client--- ");
// while (true) {
client = serversocket.accept();
Log.e(TAG,
"client-->" + client.getPort() + "     client-->"
+ client.getInetAddress() + "   是否连接 成功-->"
+ client.isConnected());


Log.v(TAG, "client-->" + client.getInetAddress());
// send message to client
// String msg = Transla(client);


mreceiveThread = new receiveThread(client);
stop = false;
mreceiveThread.start();
// }


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}


// 接收数据
class receiveThread extends Thread {
private InputStream mInputStream = null;
private byte[] buf;
private String str = null;
private Socket socket = null;
BufferedReader bReader = null;


public receiveThread(Socket socket) {
// TODO Auto-generated constructor stub
this.socket = socket;
try {
Log.e(TAG, "receiveThread start ");
// 获得输入流
this.mInputStream = client.getInputStream();
this.bReader = new BufferedReader(new InputStreamReader(
mInputStream));
Log.e(TAG, "go  here ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@Override
public void run() {
// TODO Auto-generated method stub
int n = 100;


while (!stop) {
this.buf = new byte[512];
// 读取输入的数据(阻塞读)
try {
this.mInputStream.read(buf);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 字符编码转换
try {
this.str = new String(this.buf, "GB2312").trim();
Log.v(TAG, "receive  data " + str);
n = Integer.parseInt(str);
switch (n) {
case 1:
getCpuInfo();
break;
case 2:
Ack("get data is  2");
Log.e(TAG, "please input  right number ");
break ;
default:
Log.e(TAG, "please input  right number ");
break;
}


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}
}


public void getCpuInfo() {
// TODO Auto-generated method stub
Log.v(TAG, "send msg--> " + new CpuSum().getVersion()[0]);
// displayToast(new CpuSum().getVersion()[0]);
Ack(new CpuSum().getVersion()[0]);
}


public void Ack(String string) {
// TODO Auto-generated method stub
String outString = string;
try {
OutputStream output = client.getOutputStream();
output.write(outString.getBytes("gbk"));
Log.v(TAG, string);
output.flush();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}






}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场