我有一個項目需要使用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