java 从 ANDROID 2.2 发送 UDP 包(HTC 的愿望)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5042548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending UDP packages from ANDROID 2.2 (HTC desire)
提问by user623273
i have a lan and i want to send a upd message from my android (htc desire) to my PC. Theres a Wlan Router between them. The Problem is, that the UPD message never gets to the PC.
我有一个局域网,我想从我的 android(htc 愿望)向我的 PC 发送一条更新消息。它们之间有一个 Wlan 路由器。问题是,UPD 消息永远不会到达 PC。
Code on the Android.:
Android上的代码。:
package org.example.androidapp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClientAnd {
public void sendUDPMessage(int port) throws java.io.IOException {
DatagramSocket socket = new DatagramSocket();
InetAddress serverIP = InetAddress.getByName("192.168.1.110");
byte[] outData = ("Ping").getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP,50005);
socket.send(out);
socket.close();
}
}
I choose a high port on booth sides.
我选择展位两侧的高端口。
The Permissions on the Android are:
Android上的权限是:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
The Server-Side is the PC just a simple programm for receiving:
服务器端是 PC 只是一个简单的接收程序:
package org.example.androidapp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UPDServerAnd implements Runnable{
public void run(){
byte[] inData = new byte[48];
byte[] outData = new byte[48];
String message;
DatagramSocket socket;
try {
socket = new DatagramSocket(50005);
while (true) {
DatagramPacket in = new DatagramPacket(inData,inData.length);
socket.receive(in);
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
message=new String(in.getData(),0,in.getLength());
System.out.println("Got "+message+" from "+senderIP+","+senderPort);
outData = "Pong".getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, senderIP,senderPort);
socket.send(out);
}
} catch (SocketException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
So why is there no UDP Package visible coming from android? Even Wireshark only shows some ARP packages. Plz help :)
那么为什么没有来自 android 的 UDP 包可见呢?甚至 Wireshark 也只显示一些 ARP 包。请帮忙:)
回答by Alex
I was having a very similar problem. My solution was to add:
我遇到了非常相似的问题。我的解决方案是添加:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
to the Manifest.xml
file. Then I disabled all Windows firewall
and it worked. I was able to send a String
from my Droid
to a PC.
到Manifest.xml
文件。然后我禁用了所有Windows firewall
并且它起作用了。我能够String
从我Droid
的电脑发送一个。