我是使客户端服务器线程并使用套接字,我想将消息从客户端发送到服务器,并将服务器发送到客户端 我能够从客户端到服务器获取消息,但无法从服务器到客户端获取消息,这给了我错误 我可以更改的内容,因此使用此方法可以将消息从服务器发送到客户端。 我可以配对,但是当我从服务器向客户端应用程序发送数据时已终止
MainActivity代码
package com.example.wifi_direct;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.wifi.WifiManager;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
String[] deviceNameArray;
WifiP2pDevice[] deviceArray;
public static String TAG="MainActivity";
private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private BroadcastReceiver receiver;
IntentFilter intentFilter;
ListView wifiList;
Button discoverWifi,send;
EditText sendMessage;
TextView textMessage;
static final int MESSAGE_READ=1;
ServerClass serverClass;
ClientClass clientClass;
SendReceive sendReceive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Permission();
wifiList=findViewById(R.id.wifilist);
discoverWifi=findViewById(R.id.discoverwifi);
textMessage=findViewById(R.id.textmessage);
sendMessage=findViewById(R.id.sendmessage);
send=findViewById(R.id.send);
intentFilter = new IntentFilter();
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
discoverWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
peers.clear();
deviceArray =(null);
deviceNameArray =(null);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(getApplicationContext(),"Discovery Started",Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reason) {
Toast.makeText(getApplicationContext(),"Discovery Stopped",Toast.LENGTH_SHORT).show();
}
});
}
});
wifiList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
connect(peers.get(position));
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg=sendMessage.getText().toString();
byte[] bytes=msg.getBytes();
sendReceive.write(bytes);
}
});
}
WifiP2pManager.ConnectionInfoListener connectionInfoListener= new WifiP2pManager.ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(WifiP2pInfo info) {
InetAddress groupownerAdderss= info.groupOwnerAddress;
if(info.groupFormed&&info.isGroupOwner){
Toast.makeText(getApplicationContext(),"Host",Toast.LENGTH_SHORT).show();
serverClass=new ServerClass();
serverClass.start();
Toast.makeText(getApplicationContext(),"Server class created",Toast.LENGTH_SHORT).show();
}else if(info.groupFormed) {
Toast.makeText(getApplicationContext(),"Client",Toast.LENGTH_SHORT).show();
clientClass=new ClientClass(groupownerAdderss);
clientClass.start();
Toast.makeText(getApplicationContext(),"Client class created",Toast.LENGTH_SHORT).show();
}
}
};
protected WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
//List<WifiP2pDevice> refreshedPeers = (List<WifiP2pDevice>) peerList.getDeviceList();
if (!peerList.getDeviceList().equals(peers)) {
Toast.makeText(getApplicationContext(),"device Found",Toast.LENGTH_SHORT).show();
peers.clear();
peers.addAll(peerList.getDeviceList());
deviceNameArray = new String[peerList.getDeviceList().size()];
deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];
int index = 0;
for (WifiP2pDevice device : peerList.getDeviceList()) {
deviceNameArray[index] = device.deviceName;
deviceArray[index] = device;
index++;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplication(), android.R.layout.simple_list_item_1, deviceNameArray){
//@androidx.annotation.NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=super.getView(position, convertView, parent);
TextView textView=view.findViewById(android.R.id.text1);
textView.setTextColor(Color.CYAN);
return view;
}
};
wifiList.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
// If an AdapterView is backed by this data, notify it
// of the change. For instance, if you have a ListView of
// available peers, trigger an update.
//((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
// Perform any other updates needed based on the new list of
// peers connected to the Wi-Fi P2P network.
}
if (peers.size() == 0) {
Toast.makeText(getApplicationContext(),"No device Found",Toast.LENGTH_SHORT).show();
Log.d(MainActivity.TAG, "No devices found");
return;
}
}
};
public void connect(final WifiP2pDevice device) {
// Picking the first device found on the network.
//final WifiP2pDevice device = device1;
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
// WiFiDirectBroadcastReceiver notifies us. Ignore for now.
Toast.makeText(MainActivity.this, "Connected to " + device.deviceName,
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reason) {
Toast.makeText(MainActivity.this, "Connect failed. Retry.",
Toast.LENGTH_SHORT).show();
}
});
}
/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver, intentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
WifiManager wifiManager= (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 123:
{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
// permission was granted
}
else
{
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(),"Please Give Permission To See avilable WI-FI",Toast.LENGTH_SHORT).show();
//Permission();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
protected void Permission(){
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
|| (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE)
!= PackageManager.PERMISSION_GRANTED))
{
Log.d("Tav", "Requesting permissions");
//Request permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},123);
}
}
public class ServerClass extends Thread{
Socket socket;
ServerSocket serverSocket;
@Override
public void run() {
try {
serverSocket=new ServerSocket(8888);
socket=serverSocket.accept();
sendReceive=new SendReceive(socket);
sendReceive.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class SendReceive extends Thread{
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public SendReceive(Socket skt)
{
socket=skt;
try {
inputStream= socket.getInputStream();
outputStream= socket.getOutputStream();
Log.e("Error 404 Found","Send Recive Created ");
//Toast.makeText(getApplicationContext(),"Send Recive Created",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
byte[] buffer=new byte[1024];
int bytes;
while (socket!=null)
{
try {
bytes=inputStream.read(buffer);
if(bytes>0)
{
handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write(byte[] bytes)
{
try {
//outputStream=socket.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ClientClass extends Thread{
Socket socket;
String hostAdd;
public ClientClass(InetAddress hostAddress)
{
hostAdd=hostAddress.getHostAddress();
socket=new Socket();
}
@Override
public void run() {
try {
socket.connect(new InetSocketAddress(hostAdd,8888),500);
sendReceive=new SendReceive(socket);
sendReceive.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what)
{
case MESSAGE_READ:
byte[] readBuff= (byte[]) msg.obj;
String tempMsg=new String(readBuff,0,msg.arg1);
textMessage.setText(tempMsg);
break;
}
return true;
}
});
}
WifiDirectBroadCastReciver
package com.example.wifi_direct;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.se.omapi.Channel;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.content.Context.WIFI_SERVICE;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver{
private WifiP2pManager manager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
private static final String TAG = "MyActivity";
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel mChannel, MainActivity mActivity) {
this.manager = manager;
this.mChannel = mChannel;
this.mActivity = mActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi P2P is enabled
Toast.makeText(context, "WiFiP2P is Enabled ... ", Toast.LENGTH_LONG).show();
} else {
// Wi-Fi P2P is not enabled
//manager=(WifiP2pManager)getApplicationContext().getSystemService(WIFI_P2P_SERVICE);
Toast.makeText(context, "WiFiP2P is disabled ... ", Toast.LENGTH_LONG).show();
WifiManager wifiManager= (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
// Toast.makeText(context, "WiFi is disabled ... We need to enable it", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
if (manager!= null){
manager.requestPeers(mChannel,mActivity.peerListListener);
Toast.makeText(context, "Requested to Peer ", Toast.LENGTH_LONG).show();
}
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
if(manager==null)return;
NetworkInfo networkInfo=intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if(networkInfo.isConnected()){
manager.requestConnectionInfo(mChannel,mActivity.connectionInfoListener);
}
else {
Toast.makeText(context, "Device Disconnected", Toast.LENGTH_LONG).show();
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
}
XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:fontFamily="sans-serif-condensed-light"
android:textSize="30dp"
android:textColor="#000"
android:text="Available WI-FI"
android:id="@+id/ec"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/ec"
android:id="@+id/discoverwifi"
android:text="discover"
android:textColor="#000"
android:background="#fff"
android:textSize="30dp"
/>
<ListView
android:layout_below="@id/discoverwifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textDirection="anyRtl"
android:layout_above="@+id/textmessage"
android:id="@+id/wifilist"
/>
<TextView
android:id="@+id/textmessage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:maxLines="2"
android:textColor="#000"
android:text="hello"
android:textSize="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_above="@+id/sendmessage"
/>
<EditText
android:layout_width="280dp"
android:layout_height="wrap_content"
android:id="@+id/sendmessage"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="right"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:text="send"
android:background="@color/colorAccent"
/>
</RelativeLayout>
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wifi_direct, PID: 3574
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108)
at java.net.SocketOutputStream.write(SocketOutputStream.java:141)
at com.example.wifi_direct.MainActivity$SendReceive.write(MainActivity.java:328)
at com.example.wifi_direct.MainActivity$3.onClick(MainActivity.java:116)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)