2

Server.java

public class Server { public static BufferedReader inFromClient = null; public static DataOutputStream outToClient = null; public static Socket connectionSocket = null; static ServerSocket welcomeSocket = null; static String path = null; public static void main(String[] args) throws IOException { welcomeSocket = new ServerSocket(1001); FileInputStream fin = null; FileOutputStream fout = null; while (true) { connectionSocket = welcomeSocket.accept(); inFromClient = new BufferedReader(new InputStreamReader( connectionSocket.getInputStream())); outToClient = new DataOutputStream( connectionSocket.getOutputStream()); path = inFromClient.readLine(); if (path != null) { String fileName = new File(path).getName(); File file = new File(path); File file2 = new File(fileName); fin = new FileInputStream(file); fout = new FileOutputStream(file2); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fin.read(buffer)) > 0) { fout.write(buffer, 0, bytesRead); } fin.close(); fout.close(); } } } } 

I'm getting 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 sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at Server.main(Server.java:37) 

And here is what I was trying to do.

After I run the Server.java and then run Client.java. I tried by press the Choose button, but didn't choose any file and then close the File Chooser after that.

In my understanding is that this line

 path = inFromClient.readLine(); 

When it tries to readLine, but didn't find anything which is why it says unknown source, but how do I fix this?

In my Client.java I'm trying to close() the socket but its not close at all....

UPDATE I have fixed the error by putting try&catch on the server.java where the error occur, and that fixed the problem!

1
  • try/catch doesn't fix anything. It changes the behaviour.CommentedAug 4, 2017 at 10:20

1 Answer 1

3

When it tries to readLine, but didn't find anything which is why it says unknown source

No it isn't. That refers to the fact that the source code concerned wasn't compiled with debug information so the source line number and file weren't available when the stack trace was printed.

The most common explanation of this condition is that you have written to a connection that has already been closed by the peer: in other words, an application protocol error.

Why are you writing a server whose function is to copy a file from one location to another based on an input from the client?

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.