这是我在网上查资料再根据自己情况修改过的代码,主要内容是UDP客户端发送和接收数据,点一下按键就发送数据,然后马上接收数据,设置阻塞时间为3秒。等待3秒后没有接收到服务端数据就自动再次发送数据,循环5次,5次还是没有接收到数据就停止发送数据,宣布接收数据失败。代码如下:
新建UDPThread类:
public class UDPThread extends Thread{
/**
* UDP接收数据的长度为1KB(1024)
*/
private final int data_length = 1024;
private String m_IP;
private String m_Port;
/**
* UDP要发送的数据
*/
private String m_text;
//构造函数
public UDPThread(String strIP, String strPort, String strText)
{
m_IP = strIP;
m_Port = strPort;
m_text = strText;
}
@Override
public void run() {
final int TIMEOUT = 3000;//阻塞时间设置为3s
int tries = 0;//数据包可能丢失,这次尝试的次数
final int maxTries = 5;//最大失败接收数据次数
boolean receivedResponse = false;//是否接收数据成功的标志位
byte[] bytesToSend = m_text.getBytes();//要发送的字节
byte[] bytesReceived = new byte[data_length];//接收到的字节
int port = Integer.valueOf(m_Port);
try {
InetAddress serverAddress = InetAddress.getByName(m_IP);
DatagramSocket datagramSocket = new DatagramSocket();
//设置阻塞时间
datagramSocket.setSoTimeout(TIMEOUT);
//把要发送的信息打包
DatagramPacket sendPacket = new DatagramPacket
(bytesToSend, bytesToSend.length, serverAddress, port);
//建立一个空的接收包
DatagramPacket receivePacket = new DatagramPacket
(bytesReceived, bytesReceived.length);
do {
try {
datagramSocket.send(sendPacket);//发送数据
datagramSocket.receive(receivePacket);//接收数据
if (!receivePacket.getAddress().equals(serverAddress)) {//检验数据源
throw new IOException("Received packet from an unknown source");
}
receivedResponse = true;
} catch (IOException e) {//当接收不到信息或接收时间超过3秒时,就向服务器重发请求
e.printStackTrace();
tries += 1;
Log.e("UDP", "尝试接收数据次数:" + tries);
}
} while ((!receivedResponse) && (tries < maxTries));
} catch (IOException e) {
Log.e("UDP", "发送数据失败");
}
}
}
UDPThread类的应用,点击按键启动UDPClient线程:
String ip = "192.168.88.88"
String port = "1000"
//启动udp线程发送并接收数据
UDPThread udp = new UDPThread(ip, port, "message");
udp.start();