我正在努力掌握Android上的UDP,所以我正在编写一个小型测试程序,在同一台机器上设置2个套接字,发送数据包并接收它。我认为这是直截了当的,但是出了点问题。我的代码是:
DatagramSocket server;
DatagramSocket client;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize a client socket and a server socket using UDP
try {
server = new DatagramSocket();
client = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Server will now send some data
String data = "This is a test";
DatagramPacket sendPacket = new DatagramPacket(data.getBytes(), data.length(),
client.getInetAddress(), client.getLocalPort());
try {
server.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Client has to receive and read the data
byte[] buf = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
try {
client.receive(receivePacket);
byte[] receivedData = receivePacket.getData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server.disconnect();
client.disconnect();
}
当我调试我的代码时,它进入最后一次尝试,在“client.receive(receivePacket);
”但它永远不会从该函数调用返回。该数据包似乎从未收到,但我很难找到原因。
我希望有人可以帮我解决这个问题。
编辑:使用线程的新代码。在实际发送数据包之前调用“接收”。试图使用AsyncTask。
public class udpexample extends Activity {
DatagramSocket server;
DatagramSocket client;
String data = "This is a test";
private static final String TAG = "UDPExampleActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "Starting");
// Initialize a client socket and a server socket using UDP
try {
server = new DatagramSocket();
client = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new ReceivePacket().execute();
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Server will now send some data
DatagramPacket sendPacket = new DatagramPacket(data.getBytes(), data.length(),
client.getInetAddress(), client.getLocalPort());
try {
Log.i(TAG, "About to send");
server.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class ReceivePacket extends AsyncTask<Void, Void, Void> {
// Thread used to receive a packet
@Override
protected Void doInBackground(Void... params) {
// Client has to receive and read the data
byte[] buf = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
try {
Log.i(TAG, "About to receive");
client.receive(receivePacket);
byte[] receivedData = receivePacket.getData();
// Verify that the packet is identical to the one being sent
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
server.disconnect();
client.disconnect();
}
}
}
仍有同样的问题,它停止并等待接收,没有任何反应。
非常感谢,
Jary
答案 0 :(得分:1)
我认为你正在发送数据包而接收器还没有收听。
先尝试client.receive(..)
,然后server.send(..)
。那样client
将监听传入的数据包。当然,你必须在两个不同的线程中运行它。