udp收不到,为什么?

jokesh 2011-08-09 07:19:24
您好,大家们

我从远程电脑发udp数据,但我收不到,为什么? 我给远程 机子我的ip地址了。。。。 本地运行没有问题。。。就从远程电脑收不到udp数据。。。。怎么解决?
...全文
139 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jokesh 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 directray 的回复:]

你中间有路由设备?NAT和NAPT和Java无关,这是一种网络技术。LZ去搜一下吧。如果你真的需要P2P通信的话,建议还是TCP。UDP还是有些麻烦的,是通信协议自身的问题,你解决以后会发现,你实现的就是TCP。
[/Quote]

我的程序是p2p通信工具 我的程序每个1分钟内能得到连接服务器的客户最新ip地址,并且这些最新的ip地址能下载到各个客户的电脑里,方便客户读取对方客户的ip地址 ,。。。
jokesh 2011-08-23
  • 打赏
  • 举报
回复
遇到防火墙 问题 有时候 我能接受udp数据 但对方收不到。。。 有时候 我们都收不到 怎么回事?
jokesh 2011-08-23
  • 打赏
  • 举报
回复
就是用路由器。。。p2p就像QQ一样聊天工具。。。但遇到udp打洞问题 太麻烦了。。。
DirectRay 2011-08-19
  • 打赏
  • 举报
回复
你中间有路由设备?NAT和NAPT和Java无关,这是一种网络技术。LZ去搜一下吧。如果你真的需要P2P通信的话,建议还是TCP。UDP还是有些麻烦的,是通信协议自身的问题,你解决以后会发现,你实现的就是TCP。
jokesh 2011-08-12
  • 打赏
  • 举报
回复
通过研究。。。。。这个不是程序的错误 而是需要udp打洞技术。。。 谁用过java udp打洞技术? 介绍一下。。谢谢。。
evangelionxb 2011-08-12
  • 打赏
  • 举报
回复
String longtex=new String(buffer, 0, packet.getLength());
改成
String longtex=new String(packet.getData(), 0, packet.getLength());

例子


public class SearchLocalComputer {

private int port = 10001;

public void send() throws UnknownHostException{

DatagramPacket dp = null;
DatagramSocket ds = null;

String str = "is there anyone";

byte[] buf = str.getBytes();

dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("localhost"),port);

try {
ds = new DatagramSocket();
System.out.println("send: ");
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
ds.send(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void receive(){

DatagramSocket ds = null;
DatagramPacket dp = null;

try {
ds = new DatagramSocket(port);
System.out.println("receive: ");
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int size = 1024;

byte[] buf = new byte[size];
dp = new DatagramPacket(buf, buf.length);


try {
ds.receive(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int length = dp.getLength();

String msg = new String(dp.getData(),0,length);
System.out.println(msg);

}

public static void main(String[] args) {

SearchLocalComputer slc = new SearchLocalComputer();

if(args[0].equals("send")){
try {
slc.send();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(args[0].equals("receive")){
slc.receive();
}


}
}

jokesh 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 evangelionxb 的回复:]

你程序发出来才能看啊,没程序,一切都是魂淡啊
[/Quote]

我的发udp代码:

try {
int port = 7777;
byte[] msg = chattext.getText().getBytes();

// Get the internet address of the specified host
InetAddress address = InetAddress.getByName("124.126.176.80");//my pc`s ip adress

// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(msg, msg.length,
address, port);

packet.getPort();

// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
} catch (Exception e) {
e.printStackTrace();
}


udp接受代码:


try {  
int port = 7777;

InetAddress address = InetAddress.getLocalHost();

DatagramSocket dsocket = new DatagramSocket(port, address);

byte[] buffer = new byte[65000];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

// Now loop forever, waiting to receive packets and printing them.
while (true) {


dsocket.receive(packet);
String longtex=new String(buffer, 0, packet.getLength());
..............





请大家帮我解决这个问题 谢谢大家。。。
TheSadLove 2011-08-10
  • 打赏
  • 举报
回复
在远程电脑上ping你自己电脑的IP,看是否连通,如果连通,再调试程序,看是否有问题
UDP协议传输数据稳定性比较差,丢数据是很正常的事情。
我之前做过车载GPS定位系统,车载GPS就是采用的UDP协议传输经纬度数据的,经过很多次测试数据丢失率在5% - 20%之间。
UDP:单向发送数据,数据发送方只管发数据,不会考虑接收方是否收到。
evangelionxb 2011-08-10
  • 打赏
  • 举报
回复
你程序发出来才能看啊,没程序,一切都是魂淡啊
想喝咖啡的貓 2011-08-09
  • 打赏
  • 举报
回复
ping不ping的通。
防火墙。
jokesh 2011-08-09
  • 打赏
  • 举报
回复
谢谢 您的回复 具体是什么 ? 我没有懂你的意思?

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧