我正在使用TFTP服務器應用程序。我設法處理從服務器到客戶端的成功的文件傳輸,但是另一方面卻被竊聽。讀取文件時發生IndexOutOfBoundException
客戶端而不是傳輸整個文件只是終止惠普編譯器返回沒有錯誤。調試器在引用該數組超出範圍的標記代碼上顯示IOBE異常。
整個轉移過程是這樣這樣:
- 客戶端發送一個文件名和請求的操作WRQ - 寫請求
- 服務器接收到的數據包,並確定該操作是否WRQ是給出了新的文件合適的名稱。
- 服務器現在開始執行receiveData(),直到它獲得一個數據包< 512 indicationg EOT
- 客戶端不斷傳輸從文件中讀取的數據。
關鍵代碼:
客戶:
private void sendWRQ() throws Exception
{
String rrq = "WRQ-" + data;
outgoingData = rrq.getBytes();
DatagramPacket output = new DatagramPacket(outgoingData, outgoingData.length, serverAddress, serverPort);
clientSocket.send(output);
//Thread.sleep(50);
sendData();
}
byte outgoingData = new byte[512];
private void sendData() throws Exception
{
DatagramPacket dataTransfer = new DatagramPacket(outgoingData, outgoingData.length, serverAddress, serverPort);
InputStream fis = new FileInputStream(new File(data));
int x;
while((x = fis.read(outgoingData,0,512)) != -1) // << Debugged gives IOBE
{
dataTransfer.setLength(x);
clientSocket.send(dataTransfer);
Thread.sleep(5);
}
fis.close();
}
服務器:
private void listen() throws Exception
{
DatagramPacket incTransfer = new DatagramPacket(incomingData, incomingData.length);
serverSocket.receive(incTransfer);
clientAddress = incTransfer.getAddress();
clientPort = incTransfer.getPort();
String output = new String(incTransfer.getData());
if(output.substring(0, 3).equals("RRQ"))
{
File test = new File(output.substring(4));
responseData = output.substring(4);
if(test.exists())
{
sendResponse("Y");
} else {
sendResponse("N");
}
} else if (output.substring(0, 3).equals("WRQ"))
{
File test = new File(output.substring(4));
if(test.exists())
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat prefix = new SimpleDateFormat(date_format);
String date = prefix.format(cal.getTime()).toString();
responseData = date + output.substring(4);
receiveData();
} else {
responseData = output.substring(4);
receiveData();
}
}
}
private void receiveData() throws Exception
{
DatagramPacket receiveData = new DatagramPacket(incomingData, incomingData.length);
OutputStream fos = new FileOutputStream(new File(responseData));
while(true)
{
serverSocket.receive(receiveData);
if(receiveData.getLength() == 512)
{
fos.write(receiveData.getData());
} else {
fos.write(receiveData.getData(), receiveData.getOffset(), receiveData.getLength());
break;
}
}
fos.close();
}
print'rrq.getBytes()'並查看它是否正確。不確定'String'是否正確處理特殊字符。 – vulkanino 2012-03-07 16:01:35
這是正確的。問題不在這裏這部分是發送和接收,因爲它應該是有問題的部分是我在代碼中標記的地方。 – 2012-03-07 16:03:46
你不提供'outgoingData'定義,它是否有足夠的空間容納512字節? – vulkanino 2012-03-07 16:07:30