从 ANDROID 2.2 发送 UDP 包(HTC 希望)

发布于 2024-10-17 19:25:39 字数 2041 浏览 1 评论 0原文

我有一个局域网,我想从我的 Android (htcdesire) 向我的电脑发送更新消息。它们之间有一个 WLAN 路由器。问题是,UPD 消息永远不会到达 PC。

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();
    }

}

我选择展台两侧的高端口。

Android 上的权限是:

uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" 

服务器端是 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();
}

}

}

那么为什么没有可见来自 android 的 UDP 包?即使 Wireshark 也只显示一些 ARP 包。 请帮忙:)

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.

Code on the 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:

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:

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-10-24 19:25:39

我有一个非常相似的问题。我的解决方案是将: 添加

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"

Manifest.xml 文件中。然后我禁用了所有Windows 防火墙并且它起作用了。我能够将 String 从我的 Droid 发送到 PC。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文