0

i just want to run a sample code for file transfer using sockets , in eclipse! and the client gives me this error:

Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at Client.main(Client.java:13)

i don't think that it is caused by the other side closing the socket! here's the code of server and client:

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket servsock = new ServerSocket(1000);
        File myFile = new File("s.pdf");

        while (true) {
            Socket sock = servsock.accept();
            byte[] mybytearray = new byte[(int) myFile.length()];
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
            bis.read(mybytearray, 0, mybytearray.length);
            OutputStream os = sock.getOutputStream();
            os.write(mybytearray, 0, mybytearray.length);
            os.flush();
            sock.close();
        }
    }
}


public class Client {
    public static void main(String[] argv) throws Exception {
        Socket sock = new Socket("127.0.0.1", 1000);
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("s.pdf");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();
        sock.close();
    }
}
4

0 回答 0