我正在尝试建立 UDP 发送/接收应用程序。我发送一条消息,服务器回复,然后服务器可能随时间发送更多信息。我创建了一个线程来发送消息,另一个异步线程在后台不断检查新消息。但是,我没有正确收到消息。如果我有一个恒定的接收数据包馈送,它可以正常工作,但是当接收是随机的时它就不起作用。
例如: 端口 1:不断向我的客户端发送数据,所以我每秒都会收到新的数据包。结果很好,我的应用程序显示了每个新数据包。我的“正在接收更新”甚至会像“正在接收”一样滴答作响。 “正在接收……” “正在接收……”
端口 2:我随机接收数据包。只有当我一直点击我的发送按钮时它才会显示它们。我的接收进度点仅在我按下发送按钮时重复。有时数据包会闪烁并消失。
我正在尝试让端口 2 正常工作。我尝试在发布进度后让我的 inbackground() 函数 hibernate 2 秒,但这没有帮助。我真的很困惑如何制作它以便我可以不断接收 UDP 数据包。这是我的代码。
编辑:现在端口 2 显示正确,消息不只是闪烁和消失。然而,我的接收...进度点更新真的很慢。我的问题是,在我的“doInBackground”函数中,我有一个无限的 while 循环,这个循环不是一直执行得非常快吗?因此,即使没有新消息,我仍然会在循环结束时发布,以便我的“正在接收..”进度点应该向右移动?
再次编辑:我实际上没有对源代码进行任何更改,只是一些布局内容,现在端口 2 不再工作。它不显示任何新接收或接收更新栏。
如果我通过持续的馈送进入端口 1,那么一切都会完美更新......
我试着查看其他问题,但无法理解该怎么做。感谢您的帮助!
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.Settings.Global;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView txt1, txtH, txtE, txtER, txtUpdate;
String msg;
CharSequence oldMsg="a";
Integer updateCount=0;
Activity mActivity;
DatagramSocket socket;
boolean msgSent = false;
boolean errorSend = false;
boolean errorReceive = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.textView2);
txtH = (TextView) findViewById(R.id.textView1);
txtE = (TextView) findViewById(R.id.textView6);
txtER = (TextView) findViewById(R.id.textView8);
txtUpdate = (TextView) findViewById(R.id.textView9);
//I start my async class here
new receiveUDP().execute();
button = (Button) findViewById(R.id.button1);
//When I click this, I send a message
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread sendThread = new Thread(){
public void run(){
try{
byte[] data ="Some MSG".getBytes();
InetAddress address = InetAddress.getByName("Some address");
DatagramPacket pack = null;
pack = new DatagramPacket(data,data.length, address, somePort);
socket.send(pack);
msgSent=true;
} catch (Exception e){
boolean errorSend = true;
}
}
};
sendThread.start();
try {
sendThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (msgSent){
txtH.setText("SENT!");
}
}
});
if (errorSend){
txtE.setText("Error Sending Socket");
}
}
public class receiveUDP extends AsyncTask <Void, CharSequence, Void>{
@Override
protected Void doInBackground(Void... params) {
//Constantly check to see if we received a new packet.
while (true){
try{ //if no socket, create a new socket
if (socket == null){
socket = new DatagramSocket(somePort, InetAddress.getByName("address"));
socket.setBroadcast(true);
}
byte[] buf = new byte[2500];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
//Get the data of the packet to be a string
msg = new String(packet.getData(),0,packet.getLength());
} catch (Exception e){
errorReceive=true;
e.printStackTrace();
}
publishProgress(msg);
}
}
protected void onProgressUpdate(CharSequence...progress) {
updateCount++;
// If no errors, and if new message is different than old message
// Then change the text field to show new message.
if(!(errorReceive)){
if(!(oldMsg.equals(progress[0]))){
txt1.setText(progress[0]);
oldMsg = progress[0];
}
}else
{
txtER.setText("Error Receiving");
}
//Progress dots...
if(updateCount==1){
txtUpdate.setText("Receiving.");
}
else if(updateCount==2){
txtUpdate.setText("Receiving..");
}
else {
txtUpdate.setText("Receiving...");
updateCount=0;
}
}
}
}
最佳答案
我这样做成功了(假设我收到的消息是一个字符串,使用下面的符号来划分参数|
因此你看到我在收到消息后使用拆分命令
p.s.: 不要用模拟器,在真机上试试
p.p.s.:当您创建 Activity 时使用 startReceiveUdp 开始监听,然后在您的 gui 线程中使用 stopReceiveUdp 停止监听,就在关闭您的 Activity 之前(我在 onDismiss 子中使用它,覆盖它)。
ReceiveSocket receiveSocket;
void startReceiveUdp()
{
if (receiveSocket==null) {
receiveSocket=new ReceiveSocket();
receiveSocket.execute("");
}
}
void stopReceiveUdp()
{
if (receiveSocket!=null) receiveSocket.cancel(true);
}
private class ReceiveSocket extends AsyncTask<String, String, String> {
DatagramSocket clientsocket;
@Override
protected String doInBackground(String... params) {
while (true) {
try {
publishProgress(receiveMessage());
if(isCancelled()) break;
} catch (Exception e) {
//
}
}
return "";
}
String[] receiveMessage(){
String[] rec_arr = null;
try {
int port = 8081;
if (clientsocket == null) clientsocket=new DatagramSocket(port);
byte[] receivedata = new byte[30];
DatagramPacket recv_packet = new DatagramPacket(receivedata, receivedata.length);
//Log.d("UDP", "S: Receiving...");
clientsocket.receive(recv_packet);
String rec_str = new String(recv_packet.getData()); //stringa con mesasggio ricevuto
rec_str= rec_str.replace(Character.toString ((char) 0), "");
//Log.d(" Received String ",rec_str);
//InetAddress ipaddress = recv_packet.getAddress();
//int port = recv_packet.getPort();
//Log.d("IPAddress : ",ipaddress.toString());
//Log.d(" Port : ",Integer.toString(port));
rec_arr=rec_str.split("\\|");
return rec_arr;
} catch (Exception e) {
Log.e("UDP", "S: Error", e);
}
return rec_arr;
}
@Override
protected void onPostExecute(String result) {
//
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(String... rec_arr) {
//ricevi la stringa,
//splittala
//esegui l'azione richiesta sulla GUI
if (rec_arr.length>1){
String clientType=rec_arr[0];
String command=rec_arr[1];
if(command.contentEquals("go")){
//press button go
startAction(null);
}
}
}
}
关于java - 如何在 android 的异步函数中连续接收 UDP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439745/