gpt4 book ai didi

java - Android 通过 WiFi 向 Java 客户端发送 UDP 数据包

转载 作者:行者123 更新时间:2023-11-30 04:26:26 26 4
gpt4 key购买 nike

我正在创建 2 个程序,其中一个从 Adnroid 设备发送 UDP 数据包,第二个(Java)接收它们。所以如果我使用 WiFi 路由器,效果很好。但是如果我使用直接连接,Java 应用程序不会收到这些数据包。在直接连接下,我的意思是在计算机上创建 HOTSPOT 并通过 Android 设备连接到它。我使用了下面的代码 fragment :

服务器代码:

public class UDPServer {
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
DatagramSocket socket;
static String ip = "227.0.25.57";
static int port = 6789;

private boolean isRun = false;
private String message = "";
private int broadcastInterval;
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() {
@Override
public void run() {
while(isRun){
try {
SendBroadcastMessage(message);
System.out.println("msg sended...");
Thread.sleep(broadcastInterval);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Stopping UDPServer...");
}
};

public UDPServer() {
buffer = new byte[4096];
try {
groupAddress = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
socket = new DatagramSocket();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void SendBroadcastMessage(String msg) throws IOException{
if(msg==null) return;
buffer = msg.getBytes();
packet = new //DatagramPacket(buffer, buffer.length);
DatagramPacket(buffer, buffer.length, groupAddress, port);
socket.send(packet);
}

public static void Send(String msg){
try {
InetAddress group = InetAddress.getByName(ip);
MulticastSocket s = new MulticastSocket(port);
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
group, port);
s.send(hi);
System.out.println("send...");

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

客户代码:

public class UDPClient {}
MulticastSocket socket;
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
static String ip = "227.0.25.57";
static int port = 6789;


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 StopBroadcasting(){
isRun = false;
if(broadcastReceiver!=null)
broadcastReceiver.interrupt();
}
public void StartBroadcasting(){
isRun = true;
broadcastReceiver = new Thread(runner);
broadcastReceiver.start();
}
Runnable runner = new Runnable() {
@Override
public void run() {
while(isRun){
try {
String msg = ReceiveBroadcastMessage();
if(ReceiveDataListener!=null)
ReceiveDataListener.onReceiveData(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
};

public UDPClient(){
buffer = new byte[4096];
try {
groupAddress = InetAddress.getByName(ip);
socket = new MulticastSocket(port);
socket.joinGroup(groupAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public String ReceiveBroadcastMessage() throws IOException{
packet = new DatagramPacket(buffer, buffer.length);
System.out.println("before receive");
socket.receive(packet);
System.out.println(packet.getData());
return new String(packet.getData());
}

public void DeInit(){
try {
socket.leaveGroup(groupAddress);
} catch (IOException e) {

e.printStackTrace();
}
socket.close();
}

MulticastSocket msocket;
public static void Receive(){
MulticastSocket msocket;

try {
msocket = new MulticastSocket(port);
InetAddress group = InetAddress.getByName(ip);
msocket.joinGroup(group);

byte[] inbuf = new byte[1024];
DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

System.out.println("before receive");
msocket.receive(packet);
System.out.println("after receive");
int numBytesReceived = packet.getLength();

System.out.println(new String(packet.getData()));
msocket.leaveGroup(group);
msocket.close();
System.out.println(numBytesReceived);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

如果你能看到我有两种发送和接收数据包的方法。两者都不行!我做错了什么?请帮助我。

最佳答案

关于java - Android 通过 WiFi 向 Java 客户端发送 UDP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8415885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com