![](/img/trans.png)
[英]Connection reset by peer: socket write error in java for file transfer program
[英]java file transfer socket write error
我正在做一個客戶端-服務器文件傳輸項目,我幾乎在localhost上完成了測試,但是今天下面出現錯誤,這是客戶端和服務器的源代碼:
客戶端
public class Client {
public static void main(String args[]) {
String receiverIP = null;
int serverPort = 0;
receiverIP = args[0];
serverPort = Integer.parseInt(args[1]);
String fileToSend = args[2];
byte[] aByte = new byte[1];
int bytesR;
Socket clientSocket = null;
BufferedOutputStream bos = null;
InputStream is = null;
try {
clientSocket = new Socket(receiverIP, serverPort);
bos = new BufferedOutputStream(clientSocket.getOutputStream());
is = clientSocket.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
if (is != null) {
FileOutputStream fos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
File myFile = new File( fileToSend );
System.out.println("The file chosen is being sent...");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream( myFile );
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
try {
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, mybytearray.length);
bis.close();
return;
}catch (IOException ex) {
ex.printStackTrace();
}
File file = new File("C:\\copy.jpg");
fos = new FileOutputStream( file );
bos = new BufferedOutputStream(fos);
bytesR = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesR = is.read(aByte);
} while (bytesR != -1);
System.out.println("File transfer successful");
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
服務器端
public class Server {
public static void main(String args[]) {
while (true) {
ServerSocket welcomeSocket = null;
BufferedOutputStream ToClient = null;
try {
welcomeSocket = new ServerSocket(3249);
System.out.println("The port " + welcomeSocket.getLocalPort() + " is opened and ready for use.");
Socket connectionSocket = welcomeSocket.accept();
ToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
這是我得到的錯誤
The file chosen is being sent...
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at Client.main(Client.java:44)
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at Client.main(Client.java:55)
我幾乎可以肯定,該錯誤不是與所有數據都被傳輸之前關閉服務器套接字有關,以及與字節數組上的讀寫過程有關,但是我所有的修復嘗試都是徒勞的,也許我放錯了流,所以它們沒有按預期工作(創建copy.jpg文件但未獲得任何流)任何幫助將不勝感激
編輯:我忘了提及,目前我正在使用無線互聯網連接,並且我已經閱讀了一些有關套接字格式的文章,其中提到無線網絡不可靠地進行測試
您的服務器端不等待接收數據。 它接受連接,然后立即繼續下一個循環。 這會導致您的初始服務器套接字和客戶端套接字被垃圾收集,從而被關閉。
您可以通過使用telnet來驗證此行為,這在通常檢查服務器時非常方便。 在服務器計算機(cmd或控制台)上打開命令提示符,然后運行以下命令以連接到服務器:
telnet localhost 3249
您將看到它連接,然后幾乎立即斷開連接,就像您自己的客戶端應用程序一樣。
解決方案是,除了用於在服務器端接受連接的代碼外,您還需要在此處編寫用於接收數據的代碼。
此外,您應該將服務器套接字的創建放在循環的前面而不是循環的內部。 您只需要創建一次服務器套接字,然后就可以通過它接受任意多個連接。 多次打開服務器套接字將失敗,因為該端口仍被占用(在某些操作系統中,即使關閉了先前的服務器套接字,釋放端口通常也要花費幾秒鍾)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.