本文介绍了从Android 2.2的(HTC渴望)发送UDP数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个局域网,我想从我的Andr​​oid(HTC愿望)发送UPD消息,我的电脑。那里有一个无线路由器之间。现在的问题是,该UPD消息永远不会到PC。

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对Android:

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.

在Android的权限是:

The Permissions on the Android are:

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

服务器端是PC只是一个简单的PROGRAMM用于接收的:

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

}

}

那么为什么没有UDP包可见的未来的机器人?即使是Wireshark的只显示部分的ARP包。plz帮助:)

So why is there no UDP Package visible coming from android? Even Wireshark only shows some ARP packages.Plz help :)

推荐答案

我有一个非常类似的问题。我的解决办法是添加:

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"

的Manifest.xml 文件。然后我禁用了所有 Windows防火墙和它的工作。我能送字符串从我的的Droid 到PC。

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.

这篇关于从Android 2.2的(HTC渴望)发送UDP数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 01:20