Android UDP Client Code Snippets

Java UDP Server
Listens on port 50003 and responds with "Hello from UDP Server"
import java.net.*;
import java.util.Date;

class UDPServer
{
    private static final int PORT = 50004;

    public static void main(String args[])
        throws Exception
    {
  System.out.println("UDP Server Listening on port# " + PORT);
   
        byte[] buffer  = new byte[65536];
        DatagramSocket serverSocket = new DatagramSocket(PORT);

        DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
        for (;;)
        {
          
            serverSocket.receive(receivePacket);

            System.out.println((new Date().toString()) + ": " +"Received " + receivePacket.getLength() +
                " bytes from " + receivePacket.getAddress() + ":" + receivePacket.getPort() + "...");
   
            String value = new String(buffer, 0, receivePacket.getLength());
            System.out.println(value);
            
            receivePacket.setLength(buffer.length);
   
            byte[] b = "Hello from UDP Server".getBytes();
   
   DatagramPacket sendPacket = new DatagramPacket(b, b.length, receivePacket.getAddress(), receivePacket.getPort()); 
   serverSocket.send(sendPacket);
   
   System.out.println((new Date().toString()) + ": " +"Sent Response of " + b.length + "b to " +receivePacket.getAddress() + ":" + receivePacket.getPort());
   
         }
    }
}
Android UDP Client
Sends "Hello from Android" to server ip "192.168.202.107" on port 50003 and outputs response from server
String message = "Hello From Android!";

final int server_port = 50004;
final DatagramSocket s = new DatagramSocket();
final InetAddress local = InetAddress.getByName("192.168.202.107");

int msg_length = message.length();
byte[] messageBytes = message.getBytes();

DatagramPacket p = new DatagramPacket(messageBytes, msg_length, local, server_port);

s.send(p);
Log.d(TAG, "message sent");

messageBytes = new byte[1500];
p = new DatagramPacket(messageBytes, messageBytes.length);
s.receive(p);

msg_length = p.getLength();
message = new String(messageBytes, 0, msg_length);

final Toast toast = Toast.makeText(getApplicationContext(), message 
                + " (" + msg_length + ")", Toast.LENGTH_LONG);
toast.show();

Log.d(TAG, "message received (" + msg_length + ") " + message);
//txtMessage.setText(message);

s.close();

UDP UDP Client
Just incase you need it..
Sends "Hello from UDP Client" to server ip "192.168.202.210" on port 50004 and outputs response from server
import java.net.*;
import java.util.Date;

class UDPClient
{
    private static final int PORT = 50004;
    private static final String HOST = "192.168.202.210";
    
    public static void main(String args[])
        throws Exception
    {
  System.out.println("UDP Client Sending to HOST:" + HOST + " on Port#:" + PORT);
  
        DatagramSocket serverSocket = new DatagramSocket(50000);

        byte[] b = "Hello from UDP Client".getBytes();

        DatagramPacket sendPacket = new DatagramPacket(b, b.length, InetAddress.getByName(HOST), PORT); 
        serverSocket.send(sendPacket);
                
        System.out.println((new Date().toString()) + ": " +"Sent Response... to " + HOST + ":" + PORT);    

        serverSocket.send(sendPacket);
           
    }
}

3 comments:

sg57 said...

Wonderful example code. Much appreciated.

gwyneth said...

Glad you found it useful!

Anonymous said...

will this work on an android emulator?