如何接收来自服务器的UDP数据包?适用于android java
我已经为UDP接收数据包使用了一个线程。当我将数据包发送到UDP接收程序运行的特定IP时。很遗憾,应用程序将被停止。然后,如果我删除名为如何接收来自服务器的UDP数据包?适用于android java,java,android,sockets,Java,Android,Sockets,我已经为UDP接收数据包使用了一个线程。当我将数据包发送到UDP接收程序运行的特定IP时。很遗憾,应用程序将被停止。然后,如果我删除名为new thread(new Runnable())和public void run的线程,应用程序将正常运行,但只收到一个数据。我的目的是在数据到达时,在接收器端连续接收数据。请承认我 udpserver.java: import java.io.IOException; import java.net.DatagramPacket; import java.
new thread(new Runnable())
和public void run
的线程,应用程序将正常运行,但只收到一个数据。我的目的是在数据到达时,在接收器端连续接收数据。请承认我
udpserver.java:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class UdpServer extends Activity {
/** Called when the activity is first created. */
private TextView data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = (TextView) findViewById(R.id.textView);
runUdpServer();
}
private static final int UDP_SERVER_PORT = 11111;
private static final int MAX_UDP_DATAGRAM_LEN = 1500;
private void runUdpServer() {
new Thread(new Runnable() {
public void run() {
String lText;
byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN];
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
DatagramSocket ds=null;
try {
ds = new DatagramSocket(UDP_SERVER_PORT);
//disable timeout for testing
//ds.setSoTimeout(100000);
ds.receive(dp);
lText = new String(dp.getData());
Log.i("UDP packet received", lText);
data.setText(lText);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (ds != null) {
ds.close();
}
}
}
}).start();
}
可以设置循环以从udp套接字读取数据
try {
ds = new DatagramSocket(UDP_SERVER_PORT);
//disable timeout for testing
//ds.setSoTimeout(100000);
while (!ds.isClosed()) {
ds.receive(dp);
lText += new String(dp.getData());
Log.i("UDP packet received", new String(dp.getData());
runOnUiThread(new Runnable() {
public void run() {
data.setText(lText);
}
});
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
更新:因为包数据是在非UI线程中接收的。直接访问工作线程中的数据。setText(lText)无效 这是我用来接收和解析UDP数据包的工作代码段
try {
DatagramSocket clientsocket=new DatagramSocket(9876);
byte[] receivedata = new byte[1024];
while(true)
{
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());
tv.setText(rec_str);
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));
}
} catch (Exception e) {
Log.e("UDP", "S: Error", e);
}
try {
int port = 11000;
DatagramSocket dsocket = new DatagramSocket(port);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
lText = new String(buffer, 0, packet.getLength());
Log.i("UDP packet received", lText);
data.setText(lText);
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
如果你想让它一直听下去,你可以使用一个循环<代码>while(true){ds.receive(dp);//更多内容}好的,谢谢。。我应该使用线程吗?这取决于你的程序,但是保留额外的线程应该不会有什么坏处thread@dly我没有得到输出…如果我使用while(true)。。我已将我的代码附加到下面的链接中,现在将我的作为答案发布。您忽略了数据报中的长度。@EJP是的,应该考虑有关缓冲区大小甚至超时问题的更多详细信息,但此答案仅提供了修复连续读取问题的工作示例。请确认我接收了连续数据包。如果一旦套接字关闭,然后它将不会被重新分配。在关闭套接字后,只有我才能看到传入的数据。@DineshAntonRaja您的意思是要重新创建套接字并在异常抛出时继续接收数据包?@jobcrazy每当数据包出现时,我都需要读取数据包。但现在只有一次数据包已经收到。我认为没有例外抛出…我已附加在以下网站的代码。(www.pdaraja.weebly.com)。。。。我已经厌倦了while循环(真的)。我的意图是程序应该随时准备好接收数据包。请确认我您忽略了数据报中的长度。上述解决方案是不够的。只有一次数据包已收到。但我需要的解决方案是,每当数据包来时,它必须接收数据包。这是我使用的代码。。