Connection reset by peer: socket write error (Socket Programing)
<p>I am trying to implement a Server Client application using Socket. The client request to the server for a specified file, the server will accept the client request and send the file which client requested. But while i am running my code i got the error "Connection reset by peer: socket write error" at  the server side. Why this happening ? My code is below</p>

<blockquote>
  <p>Server.java</p>
</blockquote>

<pre><code>package Client_Server;

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server extends Thread {

static Server s;
static ServerSocket ss;
static Socket clientSocket;

public static void main(String[] args) throws IOException {
    s = new Server();
    ss = new ServerSocket(1122);
    while(true) {
        System.out.println("waiting for a connection....");
        clientSocket = ss.accept();
        Thread thread = new Thread(s);
        thread.start();
    }
}
public void run() {
    try {
        server();
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void server() throws IOException {
        System.out.println("accepted connection :"+clientSocket);

        BufferedReader bfr = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String fileName = bfr.readLine();
        System.out.println("server side got the file name : "+fileName);

        File myFile = new File(fileName);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
        OutputStream os = clientSocket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
        byte[] buffer = new byte[clientSocket.getSendBufferSize()];

        long expect = myFile.length();
        long left = expect;
        int inlen = 0;
        while (left &gt; 0 &amp;&amp; (inlen = bis.read(buffer, 0, (int)Math.min(left, buffer.length))) &gt;= 0) {
            dos.write(buffer, 0, inlen);
            left -= inlen;
        }
        dos.flush();
        if (left &gt; 0) {
            throw new IllegalStateException("We expected " + expect + " bytes but came up short by " + left);
        }
        if (bis.read() &gt;= 0) {
            throw new IllegalStateException("We expected only " + expect + " bytes, but additional data has been added to the file");
        }
}
}
</code></pre>

<blockquote>
  <p>Client2.java</p>
</blockquote>

<pre><code>package Client_Server;

import static Client_Server.MainServer.sock;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Client2 {
public static void main(String[] args) throws IOException {

    File outdir = new File("copiedfiles");
    if (!outdir.isDirectory()) {
        outdir.mkdirs();
    }
    String fileName = "som.jpg";
    Socket sock = new Socket("localhost", 1122);
    System.out.println("connecting.....");
    OutputStream os = sock.getOutputStream();
    PrintWriter pw = new PrintWriter(os, true);
    pw.println(fileName);
    DataInputStream clientData = new DataInputStream(new       BufferedInputStream(sock.getInputStream()));
    OutputStream output = new BufferedOutputStream(new FileOutputStream(new File(outdir, "received_from_client_" + fileName)));

    long size = clientData.readLong();
    long bytesRemaining = size;
    byte[] buffer = new byte[sock.getReceiveBufferSize()];
    int bytesRead = 0;
    while (bytesRemaining &gt; 0 &amp;&amp; (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, bytesRemaining))) &gt;= 0) {
        output.write(buffer, 0, bytesRead);
        bytesRemaining -= bytesRead;
    }
    output.flush();
    if (bytesRemaining &gt; 0) {
        throw new IllegalStateException("Unable to read entire file, missing " + bytesRemaining + " bytes");
    }
    if (clientData.read() &gt;= 0) {
        throw new IllegalStateException("Unexpected bytes still on the input from the client");
    }
}
}
</code></pre>

<p>Please help me to solve this.
The Exception is </p>

<pre><code>Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122)
at java.io.DataOutputStream.write(DataOutputStream.java:107)
at Client_Server.Server.server(Server.java:54)
at Client_Server.Server.main(Server.java:27)
Java Result: 1
</code></pre>