Sto cercando di implementare un server HTTP tramite Sockets. Se il client (ad esempio un browser) richiede una directory, il server visualizza un elenco di file disponibili. Il problema sorge quando il client richiede un file. Ottengo il seguente errore:java.net.SocketException: Connessione ripristinata dal peer: errore di scrittura socket Quando si serve un file
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at cf.charly1811.java.web.RequestHandler.writeFile(RequestHandler.java:152)
at cf.charly1811.java.web.RequestHandler.processRequest(RequestHandler.java:139)
at cf.charly1811.java.web.RequestHandler.handleRequest(RequestHandler.java:110)
at cf.charly1811.java.web.RequestHandler.run(RequestHandler.java:86)
at java.lang.Thread.run(Thread.java:745)
La StackTrace dimostra che il problema è in arrivo dalle writeFile()
metodi:
private void writeFile(File request) throws IOException
{
InputStream byteReader = new BufferedInputStream(new FileInputStream(request));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = byteReader.read(buffer)) != -1)
{
outputStream.write(buffer, 0, bytesRead);
}
byteReader.close();
}
Ma io non riesco a capire cosa c'è che non va. Potete aiutarmi?
EDIT
Grazie a tutti per le vostre risposte. Dopo aver letto le tue risposte ho capito che il problema era il socket quando si verificava un errore. Qui di stato il mio codice errato:
// Method to process a single request
handleRequest() throw IOException
{
// process here
// if the client request a file
writeFile();
// close socket when the request is processed
}
// The method is called
public run()
{
try{
// If an error occurs the try/catch won't be called because it is implemented outside the loop. So when an IOException occurs, the loop just stop and exit the program
while(true)
{
handleRequest();
}
}
catch(IOException e) {
// Handle exception here
}
}
e il mio nuovo codice era simile a questo:
// Method to process a single request
handleRequest()
{
try {
// process here
// if the client request a file
writeFile();
// close socket when the request is processed
}
// If this exception occurs the catch() method will be called
catch(IOException e)
{
// handle exception here
}
}
// The method is called
public run()
{
while(true)
{
handleRequest();
}
}
}
Probabilmente è necessario esaminare cosa sta accadendo sull'host remoto (il "clent"). SUGGERIMENTO: provare [Wireshark] (http://www.wireshark.org). – FoggyDay
Il ciclo 'while (true)' deve uscire normalmente quando si verifica una IOException. Il nuovo codice nella modifica ha un nuovo problema in cui il ciclo 'while' sta per girare per sempre se il socket si chiude in modo anomalo. – bond
@baconoverlord cosa consiglia per questo? –