UDP-Networking - within the Emulator
What you will learn: You will learn how to setup a UDP-Connection within the Emulator.

Difficulty: 2 of 5

What it will look like:
"S:" stands for server, "C:" stands for Client:
Using java Syntax Highlighting
- D/UDP(1515): S: Connecting...
- D/UDP(1515): S: Receiving...
- D/UDP(1515): C: Connecting...
- D/UDP(1515): C: Sending: 'Hello from Client'
- D/UDP(1515): S: Received: 'Hello from Client'
- D/UDP(1515): S: Done.
- D/UDP(1515): C: Sent.
- D/UDP(1515): C: Done.
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
Description:
0.) The MainActivity will do nothing, except to start 2 Threads. The first will be the Server waiting for a packet, that will be sent by the second Thread, the Client, 500ms later.
This is the simple MainActivity:
Using java Syntax Highlighting
- package org.anddev.android.udpconnection;
- import android.app.Activity;
- import android.os.Bundle;
- public class UDPConnection extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- /* Kickoff the Server, it will
- * be 'listening' for one client packet */
- new Thread(new Server()).start();
- /* GIve the Server some time for startup */
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) { }
- // Kickoff the Client
- new Thread(new Client()).start();
- }
- }
Parsed in 0.011 seconds, using GeSHi 1.0.8.4
1.) The Server waiting for one packet.
Using java Syntax Highlighting
- package org.anddev.android.udpconnection;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import android.util.Log;
- public class Server implements Runnable {
- public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
- public static final int SERVERPORT = 4444;
- @Override
- public void run() {
- try {
- /* Retrieve the ServerName */
- InetAddress serverAddr = InetAddress.getByName(SERVERIP);
- Log.d("UDP", "S: Connecting...");
- /* Create new UDP-Socket */
- DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
- /* By magic we know, how much data will be waiting for us */
- byte[] buf = new byte[17];
- /* Prepare a UDP-Packet that can
- * contain the data we want to receive */
- DatagramPacket packet = new DatagramPacket(buf, buf.length);
- Log.d("UDP", "S: Receiving...");
- /* Receive the UDP-Packet */
- socket.receive(packet);
- Log.d("UDP", "S: Received: '" + new String(packet.getData()) + "'");
- Log.d("UDP", "S: Done.");
- } catch (Exception e) {
- Log.e("UDP", "S: Error", e);
- }
- }
- }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
2.) The Client that sends just one packet to the Server.
Using java Syntax Highlighting
- package org.anddev.android.udpconnection;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import android.util.Log;
- public class Client implements Runnable {
- @Override
- public void run() {
- try {
- // Retrieve the ServerName
- InetAddress serverAddr = InetAddress.getByName(Server.SERVERIP);
- Log.d("UDP", "C: Connecting...");
- /* Create new UDP-Socket */
- DatagramSocket socket = new DatagramSocket();
- /* Prepare some data to be sent. */
- byte[] buf = ("Hello from Client").getBytes();
- /* Create UDP-packet with
- * data & destination(url+port) */
- DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, Server.SERVERPORT);
- Log.d("UDP", "C: Sending: '" + new String(buf) + "'");
- /* Send out the packet */
- socket.send(packet);
- Log.d("UDP", "C: Sent.");
- Log.d("UDP", "C: Done.");
- } catch (Exception e) {
- Log.e("UDP", "C: Error", e);
- }
- }
- }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
Regards,
plusminus