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!
try/catch
doesn't fix anything. It changes the behaviour.