TCPClient .java
package com.example.testtcp;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class TCPClient {
private static final String TAG = "TCPClient";
private String ip;
private int port;
private Socket mSocket;
private SocketAddress mSocketAddress;
private OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private boolean _isConnected = false;
private boolean syncDataFlag = false;
private String mData;
public TCPClient(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void connect() {
try {
Log.d(TAG, "connect: start");
this.mSocket = new Socket();
this.mSocket.setKeepAlive(true);
this.mSocketAddress = new InetSocketAddress(ip, port);
this.mSocket.connect( mSocketAddress, 10000);
this.mOutputStream = mSocket.getOutputStream();
this.mInputStream = mSocket.getInputStream();
this.mReadThread = new TCPClient.ReadThread();
this.mReadThread.start();
this._isConnected = true;
Log.d(TAG, "connect: start");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "connect: " +e);
Log.d(TAG, "connect: fail");
}
}
public String getData() {
while (!syncDataFlag) {
Thread.sleep(500);
}
return mData;
}
public void close() {
if (this.mReadThread != null) {
this.mReadThread.interrupt();
}
if (this.mSocket != null) {
try {
this.mSocket.close();
this.mSocket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
this._isConnected = false;
}
public boolean isConnected() {
return this._isConnected;
}
public void send(String testData) {
try {
byte[] msg = testData.getBytes();
byte[] len = intToByteArray(msg.length);
byte[] messageBody = new byte[len.length +msg.length];
System.arraycopy(len, 0, messageBody, 0, len.length);
System.arraycopy(msg, 0, messageBody, len.length, msg.length);
this.mOutputStream.write(messageBody);
Log.d(TAG, "SyncScenarioEngine: write end");
} catch (Exception e) {
Log.e(TAG, "connect: " +e);
}
}
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while (!isInterrupted()) {
try {
if (TCPClient.this.mInputStream == null) {
return;
}
int available = TCPClient.this.mInputStream.available();
if (available > 0) {
byte[] buffer = new byte[1024];
int size = mInputStream.read(buffer);
if (size > 0) {
syncScenarioListFlag = true;
byte[] len = new byte[4];
System.arraycopy(buffer, 0, len, 0, len.length);
byte[] megBody = new byte[buffer.length - len.length];
System.arraycopy(buffer, len.length, megBody, 0, megBody.length);
JSONObject jsonObj = new JSONObject(new String(megBody));
mData = String.valueOf(jsonObj.getJSONArray("params"));
syncDataFlag = true;
}
} else {
Thread.sleep(10);
}
} catch (Throwable e) {
System.out.println(e.getMessage());
return;
}
}
}
}
private byte[] intToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
private int byteArrayToInt(byte[] bytes) {
int value=0;
for(int i = 0; i < 4; i++) {
int shift= (4-1-i) * 8;
value +=(bytes[i] & 0x000000FF) << shift;
}
return value;
}
}
MainActivity.java
package com.example.scenariotcp;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "TCPClient";
private TCPClient tcpClient = null;
private String ip = "192.168.100.108";
private int port = 35100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: statrt ");
tcpClient = new TCPClient(ip, port);
tcpClient.connect();
if (tcpClient.isConnected()) {
Log.d(TAG, "onCreate: isconnect ");
String outContent = "TCPClient say hello";
String data = tcpClient.send(outContent);
Log.d(TAG, "onCreate: data = " + tcpClient.getData);
}
Log.d(TAG, "onCreate: end ");
tcpClient.close();
}
}
|