0
    public class MainActivity extends ActionBarActivity {
    LinearLayout vpad;

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

    vpad.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    String vtmp="Testing";
                    send_udp(vtmp);
                    return true;
                }
            });

public void send_udp(String msg){
        DatagramSocket cs=null;
        InetAddress ipx=null;
        byte[] ip_byte=new byte[]{(byte)192,(byte)168,(byte)4,(byte)1};
        try {
            ipx = InetAddress.getByAddress(ip_byte);
        } catch (Exception E){

        }
        byte[] xdata=msg.getBytes();
        DatagramPacket sp=new DatagramPacket(xdata,xdata.length,ipx,87);
        try{
            cs=new DatagramSocket();
            cs.send(sp);
        } catch (Exception E) {

        } finally {
            if(cs!=null){
                cs.close();
            }
        }
    }

i have ESP8266 set as UDP server.. i've tried with Delphi 7 as UDP client, it work.. but with this android code, it didnt receive anything.. please help me..

  • You aren't catching the exception which is definitely being thrown. You can not run any networking operation on the main thread. – WalterM Nov 04 '15 at 03:22
  • At the very least, you should do something with the exception that you are catching. (Log it) You are probably getting a `NetworkOnMainThreadException`, because you are trying to do network operations on the main thread. Try putting it in an async task, like this [answer](http://stackoverflow.com/a/6343299/2278598) explains. – Andrew Brooke Nov 04 '15 at 03:28
  • @AndrewBrooke ok im confusing.. please tell me how to use those async task.. T_T – Tan Ory Jaka Perdana Nov 04 '15 at 03:37

0 Answers0