如何将Android数据包发送到套接字服务器?我当前的代码导致应用程序退出。三星Galaxy GT-P5210上的Android 4.2.2。我可以使用WiFiP2pManager成功连接到远程设备。
String wr_data = ">04" + (char) 0x0D + (char) 0x0A; // GAA 04Jan2018
Socket client = null;
Log.d(WiFiDirectActivity.TAG, "Start socket connection");
toast = Toast.makeText(context, "Start socket connection", duration);
toast.show();
try {
InetAddress cc3200Address = InetAddress.getByName("192.168.49.146");
client = new Socket(cc3200Address, PORT);
OutputStream os = client.getOutputStream();
// InputStream is = client.getInputStream(); // GAA 04Jan2018
len = wr_data.length();
os.write(wr_data.getBytes(), 0, len);
os.close();
// inputStream.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
} finally {
if (client != null) {
if (client.isConnected()) {
try {
client.close();
} catch (IOException e) {
// Give up
e.printStackTrace();
}
}
}
}