UDP and TCP are the two main transport protocols used in today’s Internet. This post illustrates how to program UDP client and server in Android.

Android UDP communication uses the APIs provided in java.net package, which are also available in Java standard edition etc.

To program a UDP server, one can use the code below,

private void runUdpServer() {

    String lText;

    byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN];

    DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);

    DatagramSocket ds = null;

    try {

        ds = new DatagramSocket(UDP_SERVER_PORT);

        //disable timeout for testing



        lText = new String(lMsg, 0, dp.getLength());

        Log.i("UDP packet received", lText);


    } catch (SocketException e) {


    } catch (IOException e) {


    } finally {

        if (ds != null) {





The code opens a DatagramSocket and calls the receive method. Note that if setSoTimeout() is not called, the receive method is a block call, which means the execution will block until a udp message is received. If you don’t want this behavior, set the timeout to a value you want and catch for timeout exception.

For the client side, it’s also simple,

private void runUdpClient()  {

    String udpMsg = "hello world from UDP client " + UDP_SERVER_PORT;

    DatagramSocket ds = null;

    try {

        ds = new DatagramSocket();

        InetAddress serverAddr = InetAddress.getByName("");

        DatagramPacket dp;

        dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), serverAddr, UDP_SERVER_PORT);


    } catch (SocketException e) {


    }catch (UnknownHostException e) {


    } catch (IOException e) {


    } catch (Exception e) {


    } finally {

        if (ds != null) {





The code also opens a DatagramSocket, and create a DatagramPacket with destination address and port number. It calls send method to send the DatagramPacket out.

Note that to use the network socket (DatagramSocket, in our case), you’ll need to request for INTERNET permission. Adds the following line to AndroidManifest.xml will do,

<uses-permission android:name="android.permission.INTERNET"/>

Download and Screenshots

You can download the complete example here (client) and here (server) or from my github android tutorial repo here. If you run server and client on your android device (server first), you’ll get something like this,


Figure 1. Screenshot for UDP Communication Client and Server

The UDP server receives the UDP datagram from UDP client and outputs the message to a TextView.


14 Responses to Android UDP Client and Server Communication Programming–Illustrated with Example

  1. dickie says:

    This program crashes every time I try to load it

  2. dickie says:

    Do you have any solution to this crashing everytime?

  3. Anonymous says:

    Works with SGSII (2.3.4) and TF201 (4.0.3)

  4. Jorge says:

    Eu estou a tentar fazer uma comunicação com o programa cliente e um progrma feito em pascal. Mas não esta a funcionar. Sabe me dar uma razão?
    Gostei bastante do poste, bom trabalho mesmo.

    • hardy says:

      –. dont work for me on android 2.1 and 2.2. access to socket denied; works only in java-application on pc. downloadable version cant be imported properly.

  5. Hamza says:

    does this UDP server can uni cast live video to web browser from android camera. thanks in advance

  6. michael says:

    Works like a charm after editing it a bit.
    Thank you

    • jjkjh says:

      wa editing have u done?

      • Thanh says:

        When I changed: Get serverAddr from a real ip address instead of “”, it works.
        I got this real ip address after connecting 2 device by WiFi Direct.

        In device, it seems not have local address “” like in PC

  7. CLockwork says:

    This is really useful, but I can only receive the first message and it gets stuck in that…do you have any idea of how can i solve it? thank you very much!!!

  8. Jerry says:

    hi, i want send the imei, and other device data.

    i add this variable:
    final TelephonyManager imei = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    and dont works the client part.

    you know any solution?

Leave a Reply

Your email address will not be published.

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.