0
我正在編程一個android應用程序以接收正在網絡的廣播地址上廣播的數據包(這已經過測試,數據包確實得到廣播並在「UDP發送者/接收者「應用程序。)我無法讓我的應用程序將其啓動並告訴我它存在。這些設備位於同一網絡上,並且發送設備的代碼正在工作並且是專有的。這是應用程序的基本DatagramSocket代碼。未能在Android應用上接收UDP字節
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();
}
}
我在多播端嘗試了一些東西,最後以「192.168.123.255不是多播組」爲例,我沒有在做「多播」我在192.168.123.255上讓其他設備「廣播」每個設備都可以看到它。 – JDSlimz 2015-03-03 13:22:50
廣播是一種多播。它只是意味着從一個到多個發送。正如我上面提到的,嘗試直接發送數據包到您的IP地址。轉到您的Wi-Fi設置,找到您的手機的IP地址,並查看您是否可以收到直接數據包。 – Willis 2015-03-03 15:46:33
我一定在做錯事。我將多路廣播代碼整合到我的應用程序中,並嘗試發送多播地址,甚至直接按照您的建議發送,也沒有骰子。添加更新的代碼來提問。 – JDSlimz 2015-03-04 03:32:43