如何使用wifi p2p将数据从组所有者发送到客户端
how to send data from Group owner to client with wifi p2p
试了很多方法,终于问出了这个问题。正如 wifi-direct 中提到的许多文章,所有客户端都知道群主的 IP 并且可以使用此 ip 发送消息,群主将保存客户端的 ip 地址。但我无法像第一次发送的客户端那样从群组所有者向客户端发送消息。我遇到了这个错误:
第一:
failed to connect to /192.168.49.24 (port 8988) after 5000ms: isConnected failed:
EHOSTUNREACH (No route to host).
更改代码后:
第一个错误 + bind failed: EADDRINUSE (Address already in use).
我要检索的 AsyncTask:
@Override
protected String doInBackground(Void... params) {
ServerSocket serverSocket = null;
Socket client = null;
DataInputStream inputstream = null;
try {
serverSocket = new ServerSocket(8988);
client = serverSocket.accept();
inputstream = new DataInputStream(client.getInputStream());
String str = inputstream.readUTF();
String IP = client.getInetAddress().toString();
serverSocket.close();
return IP+"+"+str;
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
return null;
}finally{
if(inputstream != null){
try{
inputstream.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(client != null){
try{
client.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(serverSocket != null){
try{
serverSocket.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
}
}
和我的 IntentService 发送消息:
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
if (intent.getAction().equals(ACTION_SEND_IP)) {
String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
Log.e("DAVUD","Host:"+ host);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
Log.e("DAVUD","Port:"+ port);
DataOutputStream stream = null;
try {
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
stream = new DataOutputStream(socket.getOutputStream());
String str = intent.getStringExtra("message");
stream.writeUTF(str);
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
和我测试的其他一些代码...还有另一个问题问同样但没有回答(android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients)这个项目基于 wifiDirectDemo 简单。请帮助我真的需要它。
时隔一年又把我的问题缝了一遍。问题不在于 wifi 或连接。这是关于字符串解析。 doInBackground
中的一行是:
return IP+"+"+str
在 onPostExecute
中,我解析并从返回的字符串中获取 ip;但解析代码不正确。所以 returns:
192.168.49.24
而不是:
192.168.49.241
其中两个是有效的 ips 我不认为解析逻辑有问题。我更改了代码并使用 String[]
而不是 String
。
试了很多方法,终于问出了这个问题。正如 wifi-direct 中提到的许多文章,所有客户端都知道群主的 IP 并且可以使用此 ip 发送消息,群主将保存客户端的 ip 地址。但我无法像第一次发送的客户端那样从群组所有者向客户端发送消息。我遇到了这个错误: 第一:
failed to connect to /192.168.49.24 (port 8988) after 5000ms: isConnected failed:
EHOSTUNREACH (No route to host).
更改代码后:
第一个错误 + bind failed: EADDRINUSE (Address already in use).
我要检索的 AsyncTask:
@Override
protected String doInBackground(Void... params) {
ServerSocket serverSocket = null;
Socket client = null;
DataInputStream inputstream = null;
try {
serverSocket = new ServerSocket(8988);
client = serverSocket.accept();
inputstream = new DataInputStream(client.getInputStream());
String str = inputstream.readUTF();
String IP = client.getInetAddress().toString();
serverSocket.close();
return IP+"+"+str;
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
return null;
}finally{
if(inputstream != null){
try{
inputstream.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(client != null){
try{
client.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(serverSocket != null){
try{
serverSocket.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
}
}
和我的 IntentService 发送消息:
@Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
if (intent.getAction().equals(ACTION_SEND_IP)) {
String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
Log.e("DAVUD","Host:"+ host);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
Log.e("DAVUD","Port:"+ port);
DataOutputStream stream = null;
try {
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
stream = new DataOutputStream(socket.getOutputStream());
String str = intent.getStringExtra("message");
stream.writeUTF(str);
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
和我测试的其他一些代码...还有另一个问题问同样但没有回答(android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients)这个项目基于 wifiDirectDemo 简单。请帮助我真的需要它。
时隔一年又把我的问题缝了一遍。问题不在于 wifi 或连接。这是关于字符串解析。 doInBackground
中的一行是:
return IP+"+"+str
在 onPostExecute
中,我解析并从返回的字符串中获取 ip;但解析代码不正确。所以 returns:
192.168.49.24
而不是:
192.168.49.241
其中两个是有效的 ips 我不认为解析逻辑有问题。我更改了代码并使用 String[]
而不是 String
。