Simple udp server for android and get multi messages
<p>I write simple method for udp server:</p>

<pre><code> private void runUdpServer() {
        String message;
        byte[] lmessage = new byte[MAX_UDP_DATAGRAM_LEN];
        DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(UDP_SERVER_PORT);
            socket.receive(packet);
            message = new String(lmessage, 0, packet.getLength());
            textMessage.setText(message);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }

    }
</code></pre>

<p>How can I make to this server run and listen for messages. I use this method in onCreate, but when I send message from client I saw this message, but when I send second message I don;t see this second. Only that first message. How can I make to get new messages?</p>

<p>Edit:
This is my code now:</p>

<pre><code>@SuppressLint("NewApi")
public class Server extends Activity {

    private static final int UDP_SERVER_PORT = 2004;
    private static final int MAX_UDP_DATAGRAM_LEN = 1500;
    private TextView textMessage;
    private RunServerInThread runServer = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);

        if (android.os.Build.VERSION.SDK_INT &gt; 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
          }

        textMessage = (TextView) findViewById(R.id.messageText);
        runServer = new RunServerInThread();
        runServer.start();
    }
    private Runnable updateTextMessage = new Runnable() {
        public void run() {
            if (runServer == null) return;
            textMessage.setText(runServer.getLastMessage());
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.server, menu);
        return true;
    }


    private void runUdpServer() {
        String message;
        byte[] lmessage = new byte[MAX_UDP_DATAGRAM_LEN];
        DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(UDP_SERVER_PORT);
            socket.receive(packet);
            message = new String(lmessage, 0, packet.getLength());
            textMessage.setText(message);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }

    }

    private class RunServerInThread extends Thread{
        private boolean keepRunning = true;
        private String lastmessage = "";

        @Override
        public void run() {
            String message;
            byte[] lmessage = new byte[MAX_UDP_DATAGRAM_LEN];
            DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
            DatagramSocket socket = null;
            while(keepRunning){
            try {
                socket = new DatagramSocket(UDP_SERVER_PORT);
                socket.receive(packet);
                message = new String(lmessage, 0, packet.getLength());
                textMessage.setText(message);
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    socket.close();
                }
            }
        }
        }

       public String getLastMessage() {
            return lastmessage;
        }
    }
}
</code></pre>

<p>and I get error:</p>

<pre><code>05-30 12:18:48.562: E/AndroidRuntime(17987): FATAL EXCEPTION: Thread-1233
05-30 12:18:48.562: E/AndroidRuntime(17987): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.View.requestLayout(View.java:15469)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.View.requestLayout(View.java:15469)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.View.requestLayout(View.java:15469)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.View.requestLayout(View.java:15469)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.view.View.requestLayout(View.java:15469)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.widget.TextView.checkForRelayout(TextView.java:6368)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.widget.TextView.setText(TextView.java:3622)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.widget.TextView.setText(TextView.java:3480)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at android.widget.TextView.setText(TextView.java:3455)
05-30 12:18:48.562: E/AndroidRuntime(17987):    at pl.udpconnection.server.Server$RunServerInThread.run(Server.java:89)
</code></pre>

<p>How I can solve this problem?</p>
