[英]UDP Client Server File Transfer
我試圖通過UDP從客戶端發送數據包到服務器。 我面臨的問題是,如果最后一個數據包大小小於我們正在讀取的字節數組的大小,那么來自前一個數據包的冗余數據將被附加到它。 我嘗試只將最后一個數據包的正確部分復制到一個新的字節數組然后發送它,但客戶端以某種方式僅發送錯誤的數據包。 任何人都可以指出我做錯了什么。 提前致謝。
Client.java:
class client
{
static int serverPort;
static String filename;
public static void main(String args[]) throws SocketException, IOException
{
int count=0;
int MAX_SIZE = 1048;
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IpAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[MAX_SIZE];
String filePath = "C:\\in.txt";
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
int totLength = 0;
while((count = fis.read(sendData)) != -1) //calculate total length of file
{
totLength += count;
}
System.out.println("Total Length :" + totLength);
int noOfPackets = totLength/MAX_SIZE;
System.out.println("No of packets : " + noOfPackets);
int off = noOfPackets * MAX_SIZE; //calculate offset. it total length of file is 1048 and array size is 1000 den starting position of last packet is 1001. this value is stored in off.
int lastPackLen = totLength - off;
System.out.println("\nLast packet Length : " + lastPackLen);
byte[] lastPack = new byte[lastPackLen-1]; //create new array without redundant information
fis.close();
FileInputStream fis1 = new FileInputStream(file);
//while((count = fis1.read(sendData)) != -1 && (noOfPackets!=0))
while((count = fis1.read(sendData)) != -1 )
{
if(noOfPackets<=0)
break;
System.out.println(new String(sendData));
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IpAddress, 9876);
clientSocket.send(sendPacket);
System.out.println("========");
System.out.println("last pack sent" + sendPacket);
noOfPackets--;
}
//check
System.out.println("\nlast packet\n");
System.out.println(new String(sendData));
lastPack = Arrays.copyOf(sendData, lastPackLen);
System.out.println("\nActual last packet\n");
System.out.println(new String(lastPack));
//send the correct packet now. but this packet is not being send.
DatagramPacket sendPacket1 = new DatagramPacket(lastPack, lastPack.length, IpAddress, 9876);
clientSocket.send(sendPacket1);
System.out.println("last pack sent" + sendPacket1);
}
}
Server.java:
import java.io.*;
import java.net.*;
class server
{
public static void main(String args[]) throws IOException
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] recData = new byte[1024];
int i =0;
FileWriter file = new FileWriter("C:\\Users\\ayushi\\Documents\\Semester 2\\Misc\\setups\\eclipse\\ip_1\\ip_second\\src\\out.txt");
PrintWriter out = new PrintWriter(file);
//BufferedOutputStream bos = new BufferedOutputStream(fos);
while(true)
{
//PrintWriter out = new PrintWriter(file);
DatagramPacket recPacket = new DatagramPacket(recData, recData.length);
serverSocket.receive(recPacket);
String line = new String(recPacket.getData());
System.out.println("\n Data: " + line);
out.println(line);
System.out.println("\nPacket" + ++i + " written to file\n");
out.flush();
}
}
}
如果最后一個數據包大小小於我們正在讀取的字節數組的大小,那么來自前一個數據包的冗余數據將被附加到它。
不會。問題是來自第一個數據包的字節仍然包含在recData
字節數組中。 隨后的讀取隨后用第二個數據包的內容覆蓋字節數組的開頭,但是數組的其余部分仍然填充有來自第一個數據包的數據。
根本問題是您忽略了實際接收的字節數。 您還應該使用FileOutputStream
,而不是Writer
。 嘗試這個:
class Server
{
public static void main(String args[]) throws IOException
{
...
while(true)
{
DatagramPacket recPacket = new DatagramPacket(recData, recData.length);
serverSocket.receive(recPacket);
System.out.println("\n Packet length: " + recPacket.getLength());
out.write((recPacket.getData(), 0, recPacket.getLength());
System.out.println("\nPacket" + ++i + " written to file\n");
out.flush();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.