0

I have a little C++/Java - Socket Server (UDP) running on my PC. Now, i want to connect with my Android App to the Server. But when i send a package my App crash.

public void Socketinit() {

    // 1. Socket erstellen!
    try {
        serverAddr = InetAddress.getByName("192.168.0.101");
        socket = new DatagramSocket();

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    createListeners();


}

and

entprivate void createListeners() {

    confirm.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            buf = input.getText().toString().getBytes();
DatagramPacket packet = new DatagramPacket(buf,buf.length, serverAddr, SERVERPORT);

            try {
                socket.send(packet);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

It crashs on "socket.send(packet);" I can connect to my Server via C++ so the Server is up and running. Where is the Clientproblem in my code ?

thanks

Pete
  • 13
  • 4

1 Answers1

0

You are propably getting the NetworkOnMainThreadException (see Logcat or check in the debugger).

You can use an AsyncTask to resolve this. Propably you are also missing the Internet permission in the manifest.

Here are further details: How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Matthias247
  • 9,836
  • 1
  • 20
  • 29
  • Yep! i getting the NetworkOnMainThreadException! I not missing the internet permission but AsyncTask solve the Poblem.! THANK YOU! – Pete Oct 20 '13 at 20:41