Thursday, December 9, 2010

[android-developers] No receiving packets from Emulator

Hi Friends,

I have written Java coding to develop a simple application using
Android emulator in Eclipse. ( Scenario - I need to send UDP packets
to particular IP address also to the corresponding port numer which I
mentioned ).
My code is working properly when I try to rum my code it is executing
without error when I try to send packets from the emulator it is not
working, for your reference I have mentioned my Java code below.

Help me friends in this issue. I would really appreciate.

Thanks in Advance.

Source Code :

package com.getipandport;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Random;

import android.app.Activity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class getipandports extends Activity {
interface getipandportsReceiver
{
void addAnnouncedServers(InetAddress[] host, int port[]);
}

getipandports(WifiManager wifi) {
}
/** Called when the activity is first created. */
@Override


public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button send=(Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

EditText temp=(EditText)findViewById(R.id.ip);// Text box
reference for IP
String Dest_addr=String.valueOf(temp.getText());// Text box
reference for Destination IP
temp=(EditText)findViewById(R.id.port);// Text box reference
for port number
String port_number=String.valueOf(temp.getText());
temp=(EditText)findViewById(R.id.packet);// Text box
reference for no of packets

try
{


int
no_packet=Integer.parseInt(String.valueOf(temp.getText())); //Changing
the Value to int
int port = Integer.parseInt(port_number);//Changing the
Value to int
DatagramSocket socket = new DatagramSocket( );//Scoket
port datagram
byte[] buffer ;//Scoket input buffer
Random rand=new Random();//To generate randon value
DatagramPacket packet;
// To send no_packet
for(int i = 0; i < no_packet; i++) {
buffer=new byte[rand.nextInt()];

packet=new DatagramPacket(buffer,
buffer.length,InetAddress.getByName(Dest_addr),port);


socket.setSoTimeout(rand.nextInt());
socket.send(packet);

packet.setLength(buffer.length);
}
socket.close( );
}
catch(Exception Ex){
//temp.setText(Ex.getMessage());
}
}
});
}
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment