App crash while sending data over a socket using WifiP2p connection

北慕城南 提交于 2020-01-06 06:54:30

问题


App crashes while sending data over a socket using wifi p2p connection

I am testing a simple chat application between HTC_Desire_10 pro (running Android 6.0) and OPPO A83 (running Android 7.1.1). I have the same app running on the two devices. The app first turns wifi on, then starts discovering peers on a users click event. The user can choose the device they want to connect to from a dynamically generated peer list. The two devices are able to connect successfully. I am able to send a text message from the HTC to OPPO phone but when I try to send a message from the OPPO phone, the app crashes.

This is the code that assigns which device will be the host or client

WifiP2pManager.ConnectionInfoListener connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
    @Override
    public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
        final InetAddress groupOwnerAddress = wifiP2pInfo.groupOwnerAddress;
        if(wifiP2pInfo.groupFormed&&wifiP2pInfo.isGroupOwner) {
            ConnectionStatus.setText("Host");
            serverClass = new ServerClass();
            serverClass.start();
        } else if(wifiP2pInfo.groupFormed) {
            ConnectionStatus.setText("Client");
            clientClass = new ClientClass(groupOwnerAddress);
            clientClass.start();
        }
    }
};

And this is the code for the ServerThread, ClientThread and SendRecieveThread

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();
        } 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.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),1000);
            sendReceive = new SendReceive(socket);
            sendReceive.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The SendRecieve functionality gets invoked when a user clicks a send message button.

btnSend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String msg = writeMsg.getText().toString();
        sendReceive.write(msg.getBytes());
    }
});

I've run the app in USB-debugging mode using android studio and obtained the crash report from the adb logcat command:

--------- beginning of crash
03-27 22:16:54.115  6904  6904 E AndroidRuntime: FATAL EXCEPTION: main
03-27 22:16:54.115  6904  6904 E AndroidRuntime: Process: june.androidapps.clatt, PID: 6904
03-27 22:16:54.115  6904  6904 E AndroidRuntime: android.os.NetworkOnMainThreadException
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1318)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at java.net.SocketOutputStream.write(SocketOutputStream.java:145)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at june.androidapps.clatt.MainActivity$SendReceive.write(MainActivity.java:264)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at june.androidapps.clatt.MainActivity$5.onClick(MainActivity.java:135)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.view.View.performClick(View.java:5773)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.view.View$PerformClick.run(View.java:23035)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:836)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:103)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:232)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6802)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
03-27 22:16:54.115  6904  6904 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
03-27 22:17:06.089  7393  7393 E AndroidRuntime: FATAL EXCEPTION: main

...

Can anyone please explain why the app behaves this way? I have gone through the entire logs for about an hour and I cant seem to find what's exactly going wrong in my code. Any help will be appreciated.

with regards.


回答1:


Turns out the write() method was trying to write to the sockets' outputStream before the socket connection was set up. I disabled the send message button by default, then used AsyncTask to setup the socket connection in a background thread. Once the doInBackground() method finishes executing it makes a call to onPostExecute() which in turn uses socket.getInputStream() and socket.getOutputStream() to setup the sockets' inputStream and outputStream respectively and also enables the send message button. The send message button has a OnClickListener which makes a call to the write() method on a click event.

Disable send message button by default

btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String msg = writeMsg.getText().toString();
            byte[] bytes =msg.getBytes();
            btnSend.setVisibility(View.INVISIBLE);
            if(userType.equals("server")) {
                serverClass.writeData(bytes);
            } else {
                clientClass.writeData(bytes);
            }
        }
    });

Server Thread

public class Server extends AsyncTask<String, Integer, Boolean> {
    Socket socket;
    ServerSocket serverSocket;
    InputStream inputStream;
    OutputStream outputStream;
    @Override
    protected Boolean doInBackground(String... strings) {
        boolean result = true;
        try {
            serverSocket = new ServerSocket(8888);
            socket = serverSocket.accept();
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    public void writeData(final byte[] bytes) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        btnSend.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result) {
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //listener
            new Thread(new Runnable(){
                public void run() {
                    byte[] buffer = new byte[1024];
                    int x;
                    while (socket!=null) {
                        try {
                            x = inputStream.read(buffer);
                            if(x>0) {
                                handler.obtainMessage(MESSAGE_READ,x,-1,buffer).sendToTarget();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            btnSend.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getApplicationContext(),"could not create sockets",Toast.LENGTH_SHORT).show();
            //restart socket assignment process
        }
    }
}

Client Thread

    public class Client extends AsyncTask<String, Integer, Boolean> {
    Socket socket;
    String hostAdd;
    InputStream inputStream;
    OutputStream outputStream;

    public Client(InetAddress hostAddress) {
        hostAdd = hostAddress.getHostAddress();
        socket = new Socket();
    }

    @Override
    protected Boolean doInBackground(String... strings) {
        boolean result = false;
        try {
            socket.connect(new InetSocketAddress(hostAdd, 8888), 5000);
            result = true;
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result = false;
            return result;
        }
    }

    public void writeData(final byte[] bytes) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        btnSend.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result) {
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            new Thread(new Runnable(){
                public void run() {
                    byte[] buffer = new byte[1024];
                    int x;
                    while (socket!=null) {
                        try {
                            x = inputStream.read(buffer);
                            if(x>0) {
                                handler.obtainMessage(MESSAGE_READ,x,-1,buffer).sendToTarget();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            btnSend.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getApplicationContext(),"could not create sockets",Toast.LENGTH_SHORT).show();
            //restart socket assignment process
        }
    }
}



回答2:


my is showing error on ServerClass.start() and ClientClass.start() method, what did you do with this?



来源:https://stackoverflow.com/questions/55387403/app-crash-while-sending-data-over-a-socket-using-wifip2p-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!