I'm sending some packets by UDP protocol but app crashes by NetworkOnMainThreadException error at this part of UDP class(in below):
client_socket.send(send_packet);
by searching the error i find out that should use AsyncTask in this case(I'm not sure about this,any idea). So how can i implement AsynkTask to this code in order to solve the error? My UDP class below:
public class CheckIP {
    private String CheckState;
    private String IP;
    //UDP IP Seraching
    private byte[] send_data = new byte[1024];
    private byte[] receiveData = new byte[1024];
    public CheckIP(final String BN, final Context context) throws IOException {
        DatagramSocket client_socket = new DatagramSocket(2222);
        InetAddress IPAddress = InetAddress.getByName("255.255.255.255");
        String str = String.valueOf(BN.charAt(7) + BN.charAt(5) + BN.charAt(8) + BN.charAt(3));
        send_data = str.getBytes();
        DatagramPacket send_packet = new DatagramPacket(send_data, str.length(), IPAddress, 2222);
        client_socket.send(send_packet);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        client_socket.receive(receivePacket);
        CheckState = "true";
        IP = receivePacket.getAddress().toString();
     }
    public String getIP() {
        return IP;
    }
    public String getCheckState() {
        return CheckState;
    }
}
and the related code in my home.class :
additembtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                  try {
                        Buildnumber = "123456789";
                        CheckIP checkIP = new CheckIP(Buildnumber,home.this);
                        if (checkIP.getCheckState().equals("true")){
                            //show Toast
                        }else{
                            //show Toast
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
 
     
    