我正在编写一个android应用程序,以接收正在网络的广播地址上广播的数据包(这已经过测试,并且该数据包确实得到广播,并且也在“ UDP Sender / Receiver”应用程序中得到接收。)我无法让我的应用程序将其拾起并告诉我它存在。 这些设备位于同一网络上,并且发送设备的代码有效且专有。 这是该应用程序的基本DatagramSocket代码。
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
final TextView txt = (TextView)findViewById(R.id.txt1);
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
}
if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
}}
任何帮助表示赞赏,谢谢!
更新的代码:
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
WifiManager wifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
final TextView txt = (TextView) findViewById(R.id.txt1);
lock.acquire();
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
//while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
//}
//if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
//}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
lock.release();
}
}