Monday, 5 October 2020

message skip in local wifi chat without internet

I want to chat on two or more device using wifi without internet. I am sending message using socket from first android mobile to second mobile. I have faced issue that some massage is received and some massage skip and not received on second mobile. I have tried Multicastsoket and Datagramsoket but text message skipped. following is my send side and receiver side code.

**- Sender side**
    public static void sender(final String ipAddress, final String message) {
            // Creates the thread for capturing and transmitting audio
          /*  AsyncTaskExample asyncTask = new AsyncTaskExample();
            asyncTask.execute(message);*/
            Thread replyThread = new Thread(new Runnable() {
    
                @Override
                public void run() {
    
                    try {
                        InetAddress address = InetAddress.getByName(ipAddress);
                        byte[] data = message.getBytes();
                        DatagramSocket socket = new DatagramSocket();
                        DatagramPacket packet = new DatagramPacket(data, data.length, address, BROADCAST_PORT);
                        socket.setReuseAddress(true);
                        socket.send(packet);
                        Log.i(LOG_TAG, "Sent message( " + message + " ) to " + ipAddress);
                        socket.disconnect();
                        socket.close();
                    } catch (UnknownHostException e) {
    
                        Log.e(LOG_TAG, "Failure. UnknownHostException in sendMessage: " + ipAddress);
                    } catch (SocketException e) {
    
                        Log.e(LOG_TAG, "Failure. SocketException in sendMessage: " + e);
                    } catch (IOException e) {
    
                        Log.e(LOG_TAG, "Failure. IOException in sendMessage: " + e);
                    }
                }
            });
            replyThread.start();
        }
    
**- Receiver side**   
    
    public void receiver() {
            // Creates the thread for receiving massage
            
                Thread receiveThread = new Thread(new Runnable() {
    
                    @Override
                    public void run() {
    
                        try {
                            DatagramSocket socket  = new DatagramSocket(BROADCAST_PORT);
                         //   InetAddress group = InetAddress.getByName(myIPAddress);
                            //socket.setSoTimeout(1000);
                            byte[] buffer = new byte[BUF_SIZE];
                            DatagramPacket packet = new DatagramPacket(buffer, BUF_SIZE);
    
                            while (LISTEN) {
                                // Listen for incoming call requests
                                try {
                                    Log.i(LOG_TAG, "Listening for incoming message");
                                    /* Arrays.fill(buffer,(byte)0);*/
                                    socket.receive(packet);
                                    String data = new String(buffer, 0, packet.getLength());
                                    Log.i("SocketMSG", "Packet received from " + packet.getAddress() + " with contents: " + data);
                                    String action = data.substring(0, 4);
                                    //Toast.makeText(context,"Packet received from",Toast.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                }
                            }
                            
                            socket.disconnect();
                            socket.close();
                        } catch (SocketException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                });
                receiveThread.start();
        }
     


from message skip in local wifi chat without internet

No comments:

Post a Comment