udp 发送数据java,Java-UDP通过套接字发送数据。所有数据

博客讨论了在使用UDP协议进行客户端-服务器应用数据传输时遇到的问题,即并非所有数据每次都得到处理。作者指出,UDP不保证数据包的可靠传递,丢失、乱序或重复接收是可能的。为了解决这个问题,需要实现一个检测和处理丢失、错序或重复数据包的机制,而不是单纯依赖TCP的保证服务。
摘要由CSDN通过智能技术生成

It would seem that the Client - Server application i wrote does work however it seems that not all data is processed every time.

I am testing it on a local machine in Eclipse env.

Server:

private void sendData() throws Exception

{

DatagramPacket data = new DatagramPacket(outgoingData, outgoingData.length, clientAddress, clientPort);

InputStream fis = new FileInputStream(responseData);

int a;

while((a = fis.read(outgoingData,0,512)) != -1)

{

serverSocket.send(data);

}

}

Client:

private void receiveData() throws Exception

{

DatagramPacket receiveData = new DatagramPacket(incomingData, incomingData.length);

OutputStream fos = new FileOutputStream(new File("1"+data));

while(true)

{

clientSocket.receive(receiveData);

fos.write(incomingData);

}

}

I used to have if else in the while(true) loop to check if packet length is less than 512 bytes so it knew when to break;

I was thinking there was a problem whit that but seems that was oke for now i just wait few minutes and then stop the Client.java app

The file does transfer but the original file is 852kb and so far i got 777, 800, 850,.. but never all of it.

解决方案

The fundamental problem with your approach is that UDP does not guarantee delivery. If you have to use UDP (rather than, say, TCP), you have to implement a scheme that would detect and deal with packets that got lost, arrive out of order, or are delivered multiple times.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值