0

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

Server.java

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 > 0 && (inlen = bis.read(buffer, 0, (int)Math.min(left, buffer.length))) >= 0) {
            dos.write(buffer, 0, inlen);
            left -= inlen;
        }
        dos.flush();
        if (left > 0) {
            throw new IllegalStateException("We expected " + expect + " bytes but came up short by " + left);
        }
        if (bis.read() >= 0) {
            throw new IllegalStateException("We expected only " + expect + " bytes, but additional data has been added to the file");
        }
}
}

Client2.java

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 > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, bytesRemaining))) >= 0) {
        output.write(buffer, 0, bytesRead);
        bytesRemaining -= bytesRead;
    }
    output.flush();
    if (bytesRemaining > 0) {
        throw new IllegalStateException("Unable to read entire file, missing " + bytesRemaining + " bytes");
    }
    if (clientData.read() >= 0) {
        throw new IllegalStateException("Unexpected bytes still on the input from the client");
    }
}
}

Please help me to solve this. The Exception is

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
Roshin
  • 1
  • 4
  • What line throws the exception? – Hovercraft Full Of Eels Oct 12 '16 at 11:41
  • `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` this is the exception i got – Roshin Oct 12 '16 at 11:43
  • Again, what line is that? there's no way that I can look at your code and guess which line is Server.java:54 -- only you can tell us. If it's in your IDE, you should be able to look it up in seconds. – Hovercraft Full Of Eels Oct 12 '16 at 11:47
  • ooh sorry, the exception comes from `left -= inlen;` which is in the while loop – Roshin Oct 12 '16 at 11:51
  • That can't be a line that would throw that exception, as that's a simple int operation. Please re-run your code, and click on the offending line in the exception stacktrace and have the IDE go right to the offending line. – Hovercraft Full Of Eels Oct 12 '16 at 11:57
  • there is no error while sending large files... but if we send small files less than 10mb, then the error occur – Roshin Oct 12 '16 at 12:32
  • But again, which line throws it? Again the line that you indicated is simple numeric operation, one that can't possibly throw the exception that you have posted. – Hovercraft Full Of Eels Oct 12 '16 at 12:33
  • Also [the answer to this similar question](http://stackoverflow.com/questions/25611741/java-net-socketexception-connection-reset-by-peer-socket-write-error-when-serv) might help you. – Hovercraft Full Of Eels Oct 12 '16 at 12:37
  • there is one more error. The error is, while sending large files , while loop in client.java stucks. But no error throws. The program continues its execution with no output. If we manually exit the program then the output file stores. Why this happens – Roshin Oct 12 '16 at 12:47

0 Answers0