我有一个项目需要使用UDP协议来传输文件,但仍然保证TCP协议的功能。这意味着我们有UDP的速度和文件不会丢失。通过UDP传输文件,但guaranti文件没有丢失
我已经有:
服务器:
public class UDPServer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int port = 6788;
DatagramSocket sk = new DatagramSocket(port);
byte[] buf = new byte[1000];
while(true){
DatagramPacket request = new DatagramPacket(buf, buf.length);
sk.receive(request);
String msg = (new String(request.getData()));
DatagramPacket reply = new DatagramPacket(msg.getBytes(), msg.getBytes().length, request.getAddress(), request.getPort());
sk.send(reply);
}
}
}
客户:
public class UDPClient {
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
DatagramSocket sk = new DatagramSocket();
String msg = "message send";
InetAddress addr = InetAddress.getByName("localhost");
int port = 6788;
DatagramPacket request = new DatagramPacket(msg.getBytes(), msg.getBytes().length, addr, port);
sk.send(request);
byte[] buf = new byte[1000];
DatagramPacket reply = new DatagramPacket(buf, buf.length);
sk.receive(reply);
System.out.println("packet da nhan duoi client" + new String(reply.getData()));
sk.close();
}
}
,你能帮助我的任何建议保证文件不丢失? tks
你是指TCP协议而不是“TDP”? – Jared 2014-09-26 07:27:30
我的错误,我的意思是TCP编辑 – 2014-09-26 07:29:22
你不能那样开箱即可。你必须在你自己的udp和ack接收之上实现你自己的协议。介意你还必须自己跟踪订单。 – Fildor 2014-09-26 07:32:01