package com.cfes;
import android.text.TextUtils;
import android.util.Log;
import com.cfes.demo.SocketServer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* SockectClient
* <p>
* PC端每隔2s给监听端口发送PC时间戳信息
*/
public class SockectClient {
private Socket mSocket = null;
private static final int PORT = 54321;
private ServerSocket mServerSocket = null;
private DataOutputStream dos;
private boolean mRunning = false;
private boolean adbCmd() {
String cmd = "adb forward tcp:54321 tcp:12345";
try {
Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086");
} catch (IOException e) {
e.printStackTrace();
}
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
return true;
}
private void test() {
new Thread(new Runnable() {
@Override
public void run() {
try {
mSocket = new Socket("127.0.0.1", PORT);
System.out.println("socket:" + mSocket.toString());
DataInputStream dis = new DataInputStream(mSocket.getInputStream());
DataOutputStream dos = new DataOutputStream(mSocket.getOutputStream());
//暂时不发心跳包
// new Thread(new Runnable() {
// @Override
// public void run() {
// while (true){
// String data = "心跳包";
// try {
// dos.writeUTF(data);
// dos.flush();
// Thread.sleep(2000);
// System.out.println("socket:心跳包");
// } catch (IOException | InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// }).start();
String data = "sendTime:" + System.currentTimeMillis();
dos.writeUTF(data);
dos.flush();
System.out.println("socket:writeUTF success");
while (true) {
String s = dis.readUTF();
System.out.println("receive:" + s);
Thread.sleep(2000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void serverStart(){
new Thread(new Runnable() {
@Override
public void run() {
mRunning = true;
// ServerSocket mServerSocket = null;
try {
mServerSocket = new ServerSocket(PORT);
System.out.println("666:等待连接");
while (mRunning) {
Socket socket = mServerSocket.accept();
// ThreadPoolManager.getInstance().startTaskThread(new ReceiveThread(socket), "receive-thread");
DataInputStream dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
String data = dis.readUTF();
System.out.println("666:receive:" + data);
// String s = "device:" + System.currentTimeMillis();
dos.writeUTF("PC Service is start");
dos.flush();
}
} catch (Exception e) {
e.printStackTrace();
mRunning = false;
} finally {
if (mServerSocket != null) {
try {
mServerSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}).start();
}
public static void main(String[] args) {
SockectClient client = new SockectClient();
// client.serverStart();
if (client.adbCmd()) {
client.test();
}
}
}
我是大咸鱼
- 粉丝: 2087
- 资源: 8
评论0