Logiciel a provoqué l'abandon de la connexion: prise d'erreur d'écriture

J'ai un cas où le client postérieurement à l'établissement d'une connexion avec le serveur reçoit un fichier et lorsque la connexion a été utilisée ( persistant ) je finis par obtenir cette erreur mentionnée ci-dessus.
Ci-dessous est la mise en œuvre du code:

Scanner in = new Scanner(file);

this.clientSocket = new Socket(this.host,this.port);

this.os = new DataOutputStream(this.clientSocket.getOutputStream());

this.is = this.clientSocket.getInputStream();

while(in.hasNextLine()){
  newFile = in.nextLine();
  System.out.println(newFile);

  this.os.writeBytes(newFile + '\n');
  this.os.flush();
  scanFileList();
  writeFile();

 }

et le côté serveur de la mise en œuvre est:

 final class HttpRequest implements Runnable {
final static String CRLF = "\r\n";
Socket socket;
static String dir;
BufferedOutputStream outToClient = null;
//Constructor
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
dir = "C:\\Users\\";
}
//Implement the run() method of the Runnable interface.
public void run() {
try {
//Get a reference to the socket's input and output streams.
InputStream is = socket.getInputStream(); 
outToClient = new BufferedOutputStream(socket.getOutputStream());
processRequest(is,outToClient);
} catch (Exception e) {
System.out.println(e);
}
}
private void processRequest(InputStream is,BufferedOutputStream os) throws Exception {
//Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//Get the request line of the HTTP request message.
String fileName = br.readLine();
//Prepend a "." so that file request is within the current directory.
System.out.println(fileName);
//Open the requested file.
File myFile = null ;
boolean fileExists = true ;
myFile = new File(dir + fileName);
FileInputStream fis = null ;
try {
fis = new FileInputStream(dir + fileName);
System.out.println(fis);
} catch (FileNotFoundException e) {
fileExists = false ;
}
//Debug info for private use
System.out.println("Incoming!!!");
//Send the entity body.
if (fileExists) {
sendBytes(myFile, os);
//fis.close();
} 
//Close streams and socket.
is.close();
os.close();
br.close();
socket.close();
}
private static void sendBytes(File myFile, 
BufferedOutputStream os) throws Exception {
//Construct a 1K buffer to hold bytes on their way to the socket.
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
//Copy requested file into the socket's output stream.
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
//Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
os.write(mybytearray, 0, mybytearray.length);
os.flush();
//File sent, exit the main method
return;
} catch (IOException ex) {
//Do exception handling
}
}
}

L'erreur se produit lorsque le programme sur le côté client tente d'écrire sur le serveur: cela.os.writebytes(newFile + /n);

  Testfile01.bmp
writing
saved
Testfile02.bmp
Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at TCPClientPersistentNp.openSocket(TCPClientPersistentNp.java:53)
at TCPClient.main(TCPClient.java:66)
  • Quelle est votre question?
  • Pourquoi ne puis-je pas envoyer la requête au serveur pour le deuxième passage dans la boucle je reçois un seul fichier. Et puis la fin obtenir cette erreur.
  • double possible de Logiciel a provoqué l'abandon de la connexion: prise d'erreur d'écriture
  • Si votre code a été correctement mis en forme / en retrait, je pourrais prendre le temps d'essayer de le lire. Mais comme il est ... désolé.