package com.android.figo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.net.*;
public class ReceiveUDP extends Activity {
/** Called when the activity is first created. */
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte receiver[]=new byte[100];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
pack=new DatagramPacket(receiver,receiver.length);
mail_data=new DatagramSocket(8888);
mail_data.receive(pack);
}
catch (Exception e) {}
String s=new String(receiver);
Log.d("figo",s);
}
}
程序很短,我用自己电脑上的java程序给自己电脑上模拟器发送udp包,发端经测试好用(用自己电脑上另一个java程序接收到了包),这个完全接收不到,uses permission我也设置过Internet了,还是不好用~~~
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.net.*;
public class ReceiveUDP extends Activity {
/** Called when the activity is first created. */
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte receiver[]=new byte[100];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
pack=new DatagramPacket(receiver,receiver.length);
mail_data=new DatagramSocket(8888);
mail_data.receive(pack);
}
catch (Exception e) {}
String s=new String(receiver);
Log.d("figo",s);
}
}
程序很短,我用自己电脑上的java程序给自己电脑上模拟器发送udp包,发端经测试好用(用自己电脑上另一个java程序接收到了包),这个完全接收不到,uses permission我也设置过Internet了,还是不好用~~~
解决方案 »
- android推送有什么简单点的方案?
- 哪位哥哥写一个自定义ActionBar的demo给看看
- 求教游戏开发时声音的处理问题
- 在Android平台中,如何获取本地的资源
- 哪里有android的2.6.32内核 下载??
- 求高手帮忙,如何获得正在播放的音乐文件名
- 有 人用过在cocos2d-android 的布景层 中播放 flash么 ...
- GridView布局出错,纠结好长时间
- android访问webservice返回结果SoapFault问题
- android.support.v4.app.FragmentTabHost是什么意思啊
- android sdk升级问题,在线等结果
- 有谁知道 android 4.* vpn的!!!, 调用了 IConnectivityManager.aidl 时,无法import几个隐藏类,报错
我之前研究用UDP有段时间,最后放弃用TCP来做。
可以收到UDP包,没有理由只能收TCP不能收UDP
我也遇到同样的问题 pc做server,手机做client,现在是手机接收不到数据啊
我用软件监听手机流量 手机还是收到了 能达到5MB 用的wlan
有什么好的解决方案嘛?
我用的真机测试的
用检测软件看到系统确实收到了PC发的udp包大概在30Mb 但是自己程序的reveice()只能偶尔收到包(既有几Kb)这丢包率也不能这么大啊
android模拟器作为服务器端接收来自PC的UDP数据包,
需要在模拟器上做端口映射,用的命令跟TCP的时候有些不同
redir add udp:8000:9000
这样做了之后就可以收到UDP的包了!
String localip=null;
String netip=null;
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded=false;
while(netInterfaces.hasMoreElements() && !finded){
NetworkInterface ni=netInterfaces.nextElement();
Enumeration<InetAddress> address=ni.getInetAddresses();
while(address.hasMoreElements()){
ip=address.nextElement();
if( !ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":")==-1){
netip=ip.getHostAddress();
finded=true;
break;
}else if(ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":")==-1){
localip=ip.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
if(netip!=null && !"".equals(netip)){
return netip;
}else{
return localip;
}
}
把电脑的防火墙关了就可以了