我有一个适用于 UDP 数据包的应用程序。所以我想在同一个应用程序中同时发送和接收 UDP 数据包。这是真的吗?现在它完美地工作,但分开。代码片段:
UDP Server:
public class UDPServer {
DatagramPacket packet;
DatagramSocket socket;
static int port = 11111;
private boolean isRun = false;
private String message = "";
private int broadcastInterval;
private Context context;
public boolean IsRunUDPServer(){
return isRun;
}
public void StopBroadcasting(){
isRun = false;
}
public void StartBroadcasting(String message, int broadcastInterval){
isRun = true;
this.message = message;
this.broadcastInterval = broadcastInterval;
new Thread(runner).start();
}
Runnable runner = new Runnable() {
public void run() {
while(isRun){
try {
//Sending(message);
SendBroadcastMessageOverWiFi(message);
Thread.sleep(broadcastInterval);
Log.e("SendBroadcastMessageOverWiFi", message);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
}
}
if(socket!=null){
socket.disconnect();
socket.close();
}
}
};
public UDPServer(Context context) {
this.context=context;
}
private InetAddress getBroadcastAddress() throws IOException {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
// handle null somehow
if(dhcp==null){ return null; }
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
return InetAddress.getByAddress(quads);
}
private void SendBroadcastMessageOverWiFi(String message) throws IOException{
InetAddress addr = getBroadcastAddress();
if(addr!=null){
if(socket==null){
socket = new DatagramSocket(port);
socket.setBroadcast(true);
}
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length,
addr, port);
socket.send(packet);
}
}}
UDP Client:
public class UDPClient {
MulticastSocket socket;
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
private static final int UDP_SERVER_PORT = 11111;
private static final int MAX_UDP_DATAGRAM_LEN = 32768;
public interface OnReceiveDataListener{
public abstract void onReceiveData(String data);
}
private OnReceiveDataListener ReceiveDataListener = null;
public void setReceiveDataListener(OnReceiveDataListener ReceiveDataListener) {
this.ReceiveDataListener = ReceiveDataListener;
}
public OnReceiveDataListener getReceiveDataListener() {
return ReceiveDataListener;
}
private boolean isRun = false;
private Thread broadcastReceiver;
public void StopReceiving(){
isRun = false;
}
public void StartReceiving(){
isRun = true;
buffer = new byte[4096];
broadcastReceiver = new Thread(runner);
broadcastReceiver.start();
}
Runnable runner = new Runnable() {
@Override
public void run() {
while(isRun){
String lText;
packet = new DatagramPacket(buffer, buffer.length);
DatagramSocket ds = null;
try {
ds = new DatagramSocket(UDP_SERVER_PORT);
ds.receive(packet);
lText = new String(packet.getData(), 0, packet.getLength());
Log.i("UDP packet received", lText+" "+packet.getLength()+" "+packet.getData().length);
if(getReceiveDataListener()!=null)
getReceiveDataListener().onReceiveData(lText);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
}
};}
如果我启动服务器,客户端将无法接收 udp 数据包。请帮我。谢谢你。