Skip to content Skip to sidebar Skip to footer

App Crash While Sending Data Over A Socket Using Wifip2p Connection

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

Solution 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(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            Stringmsg= writeMsg.getText().toString();
            byte[] bytes =msg.getBytes();
            btnSend.setVisibility(View.INVISIBLE);
            if(userType.equals("server")) {
                serverClass.writeData(bytes);
            } else {
                clientClass.writeData(bytes);
            }
        }
    });

Server Thread

publicclassServerextendsAsyncTask<String, Integer, Boolean> {
    Socket socket;
    ServerSocket serverSocket;
    InputStream inputStream;
    OutputStream outputStream;
    @OverrideprotectedBooleandoInBackground(String... strings) {
        boolean result = true;
        try {
            serverSocket = newServerSocket(8888);
            socket = serverSocket.accept();
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    publicvoidwriteData(final byte[] bytes) {
        newThread(newRunnable() {
            @Overridepublicvoidrun() {
                try {
                    outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        btnSend.setVisibility(View.VISIBLE);
    }

    @OverrideprotectedvoidonPostExecute(Boolean result) {
        if(result) {
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //listenernewThread(newRunnable(){
                publicvoidrun() {
                    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

publicclassClientextendsAsyncTask<String, Integer, Boolean> {
    Socket socket;
    String hostAdd;
    InputStream inputStream;
    OutputStream outputStream;

    publicClient(InetAddress hostAddress) {
        hostAdd = hostAddress.getHostAddress();
        socket = newSocket();
    }

    @OverrideprotectedBooleandoInBackground(String... strings) {
        boolean result = false;
        try {
            socket.connect(newInetSocketAddress(hostAdd, 8888), 5000);
            result = true;
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result = false;
            return result;
        }
    }

    publicvoidwriteData(final byte[] bytes) {
        newThread(newRunnable() {
            @Overridepublicvoidrun() {
                try {
                    outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        btnSend.setVisibility(View.VISIBLE);
    }

    @OverrideprotectedvoidonPostExecute(Boolean result) {
        if(result) {
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            newThread(newRunnable(){
                publicvoidrun() {
                    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
        }
    }
}

Solution 2:

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

Post a Comment for "App Crash While Sending Data Over A Socket Using Wifip2p Connection"