UDP-Networking - within the Emulator

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

UDP-Networking - within the Emulator

Postby plusminus » Fri Dec 14, 2007 9:26 pm

UDP-Networking - within the Emulator


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

:?: Problems/Questions: Write it right below...

Difficulty: 2 of 5 :)

What it will look like:
"S:" stands for server, "C:" stands for Client:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/UDP(1515): S: Connecting...
  2. D/UDP(1515): S: Receiving...
  3. D/UDP(1515): C: Connecting...
  4. D/UDP(1515): C: Sending: 'Hello from Client'
  5. D/UDP(1515): S: Received: 'Hello from Client'
  6. D/UDP(1515): S: Done.
  7. D/UDP(1515): C: Sent.
  8. D/UDP(1515): C: Done.
Parsed in 0.030 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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.udpconnection;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class UDPConnection extends Activity {
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     public void onCreate(Bundle icicle) {
  10.         super.onCreate(icicle);
  11.         setContentView(R.layout.main);
  12.        
  13.         /* Kickoff the Server, it will
  14.          * be 'listening' for one client packet */
  15.         new Thread(new Server()).start();
  16.         /* GIve the Server some time for startup */
  17.         try {
  18.                         Thread.sleep(500);
  19.                 } catch (InterruptedException e) { }
  20.                
  21.         // Kickoff the Client
  22.         new Thread(new Client()).start();
  23.     }
  24. }
  25.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

1.) The Server waiting for one packet.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.udpconnection;
  2.  
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6.  
  7. import android.util.Log;
  8.  
  9. public class Server implements Runnable {
  10.  
  11.         public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
  12.         public static final int SERVERPORT = 4444;
  13.  
  14.         @Override
  15.         public void run() {
  16.                 try {
  17.                         /* Retrieve the ServerName */
  18.                         InetAddress serverAddr = InetAddress.getByName(SERVERIP);
  19.  
  20.                         Log.d("UDP", "S: Connecting...");
  21.                         /* Create new UDP-Socket */
  22.                         DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
  23.  
  24.                         /* By magic we know, how much data will be waiting for us */
  25.                         byte[] buf = new byte[17];
  26.                         /* Prepare a UDP-Packet that can
  27.                          * contain the data we want to receive */
  28.                         DatagramPacket packet = new DatagramPacket(buf, buf.length);
  29.                         Log.d("UDP", "S: Receiving...");
  30.  
  31.                         /* Receive the UDP-Packet */
  32.                         socket.receive(packet);
  33.                         Log.d("UDP", "S: Received: '" + new String(packet.getData()) + "'");
  34.                         Log.d("UDP", "S: Done.");
  35.                 } catch (Exception e) {
  36.                         Log.e("UDP", "S: Error", e);
  37.                 }
  38.         }
  39. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


2.) The Client that sends just one packet to the Server.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.udpconnection;
  2.  
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6.  
  7. import android.util.Log;
  8.  
  9. public class Client implements Runnable {
  10.         @Override
  11.         public void run() {
  12.                 try {
  13.                         // Retrieve the ServerName
  14.                         InetAddress serverAddr = InetAddress.getByName(Server.SERVERIP);
  15.                        
  16.                         Log.d("UDP", "C: Connecting...");
  17.                         /* Create new UDP-Socket */
  18.                         DatagramSocket socket = new DatagramSocket();
  19.                        
  20.                         /* Prepare some data to be sent. */
  21.                         byte[] buf = ("Hello from Client").getBytes();
  22.                        
  23.                         /* Create UDP-packet with
  24.                          * data & destination(url+port) */
  25.                         DatagramPacket packet = new DatagramPacket(buf, buf.length,     serverAddr, Server.SERVERPORT);
  26.                         Log.d("UDP", "C: Sending: '" + new String(buf) + "'");
  27.                        
  28.                         /* Send out the packet */
  29.                         socket.send(packet);
  30.                         Log.d("UDP", "C: Sent.");
  31.                         Log.d("UDP", "C: Done.");
  32.                 } catch (Exception e) {
  33.                         Log.e("UDP", "C: Error", e);
  34.                 }
  35.         }
  36. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Sagar Ganeshe » Wed Jan 02, 2008 10:56 am

Hi Plusminus,

I have a doubt.
Where I can see these logs..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/UDP(1515): S: Connecting...
  2.  
  3. D/UDP(1515): S: Receiving...
  4.  
  5. D/UDP(1515): C: Connecting...
  6.  
  7. D/UDP(1515): C: Sending: 'Hello from Client'
  8.  
  9. D/UDP(1515): S: Received: 'Hello from Client'
  10.  
  11. D/UDP(1515): S: Done.
  12.  
  13. D/UDP(1515): C: Sent.
  14.  
  15. D/UDP(1515): C: Done.
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Sagar Ganeshe
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 10, 2007 6:43 am

Postby plusminus » Thu Jan 03, 2008 2:26 pm

Hi Sagar,

Open the DDMS-View like in the picture. (If you cannot see the Button A points to, do it the B-Way).
Image


You need Eclipse and the ADT-Plugin to do so.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Using a seperate server?

Postby nehagp2 » Tue Jan 22, 2008 12:19 pm

Hi,

Has anyone tried using different machines for the client and the server?
My Android server stalls while waiting for receiving packet, while if write a standalone java app with the exact same code, it runs fine and receives the packet. The sender is an android client in both cases.

Any idea why this might be happening?

Thanks!
nehagp2
Freshman
Freshman
 
Posts: 2
Joined: Tue Jan 22, 2008 11:09 am

Re: Using a seperate server?

Postby GeorgieYuan » Wed Mar 05, 2008 7:18 pm

nehagp2 wrote:Hi,

Has anyone tried using different machines for the client and the server?
My Android server stalls while waiting for receiving packet, while if write a standalone java app with the exact same code, it runs fine and receives the packet. The sender is an android client in both cases.

Any idea why this might be happening?

Thanks!


Hi, all

I also have meet the problem with Socket(TCP or UDP). When I using Android as Server and a java app as Client, the Client can not connect to Server correctly.

I also use the technology that redir localhost's port to emulator's port, but that's no effect.

Any suggestion is welcome!
GeorgieYuan
Freshman
Freshman
 
Posts: 3
Joined: Mon Mar 03, 2008 6:36 pm

Postby ish » Wed Mar 12, 2008 2:50 am

Thank you for this tutorial! I got it working in no time.

The next step for me is getting this to work with a remote application on the web. I will post when I get it working and hopefully help someone else out as this has helped me!
ish
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 12, 2008 2:35 am

Top

Postby com2usprav » Fri May 02, 2008 9:55 am

It was a Nice Tutorial.

Regards
Pravin Kumar
User avatar
com2usprav
Developer
Developer
 
Posts: 26
Joined: Sat Mar 29, 2008 3:28 pm
Location: Bangalore

Postby com2usprav » Fri May 02, 2008 9:56 am

It was a Nice Tutorial.

Regards
Pravin Kumar
User avatar
com2usprav
Developer
Developer
 
Posts: 26
Joined: Sat Mar 29, 2008 3:28 pm
Location: Bangalore

Postby hadja » Tue Jul 22, 2008 12:24 am

Hi!

I see nothing in th DDMS. why?

If I don't remove @Override on the server and client I have error and cannot execute application.

Help me please to work UDP-Networking - within the Emulator
hadja
Freshman
Freshman
 
Posts: 7
Joined: Sun Jul 20, 2008 2:37 pm

Postby cslinux » Tue Oct 21, 2008 11:07 pm

Me too , i didn't get an output on the LogCat , any comment on what could be the reason ?????
cslinux
Freshman
Freshman
 
Posts: 5
Joined: Tue Oct 21, 2008 11:04 pm

Postby haden » Wed Oct 22, 2008 6:44 am

cslinux wrote:Me too , i didn't get an output on the LogCat , any comment on what could be the reason ?????


I think there is a bug with the eclipse plugin. Try restarting the application. It works for me.
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby cslinux » Wed Oct 22, 2008 7:05 am

Really i tried to restart eclipse and application more than 3 times till now , always now way. even the hello android didn't work as expected , what's wrong ....?
cslinux
Freshman
Freshman
 
Posts: 5
Joined: Tue Oct 21, 2008 11:04 pm

Postby haden » Wed Oct 22, 2008 7:08 am

cslinux wrote:Really i tried to restart eclipse and application more than 3 times till now , always now way. even the hello android didn't work as expected , what's wrong ....?


After restarting eclipse did you click on DDMS button?
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Postby cslinux » Wed Oct 22, 2008 7:11 am

Yes and always the logCat appearing blank, even i tried to change the port number always no way.
cslinux
Freshman
Freshman
 
Posts: 5
Joined: Tue Oct 21, 2008 11:04 pm

Postby MrSnowflake » Wed Oct 22, 2008 10:48 am

If your logcat is blank, select the emulator on the left hand side of the ddms perspective.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests