Richard Stokes
Richard Stokes

Reputation: 3552

SocketException: Connection Reset when trying to read from socket in Java?

Having some irritating trouble with Java sockets, my application seems to be failing at a very basic level. Part of my application requires writing filenames across a TCP connection. The receiver code is as follows:

ServerSocket serverSocket = new ServerSocket(4445); 
Socket socket = serverSocket.accept();      
BufferedReader reader = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
String filename = reader.readLine();

While my sender code is as follows:

    Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
    PrintWriter writer = new PrintWriter(socket.getOutputStream());
    writer.write("Test.jpg");

Very, very basic stuff here, but for some reason, I'm getting a SocketException: Connection Reset when I run this? This is the full stack trace:

Exception in thread "main" java.net.SocketException: Connection reset
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 FileReceiver.main(FileReceiver.java:11)

with the FileReceiver.java:11 line being the one where the reader.readLine() call is made. I can't for the life of me figure out what is going wrong, similarly basic use of TCP sockets has always worked for me in the past, why is this happening now?

Upvotes: 3

Views: 29038

Answers (6)

Andrew
Andrew

Reputation: 1

Chris is right. When you're writhing usint PrintWriter or even for serialization like ObjectOutputStream (ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());) you need to call the .flush() function and after that you must close the Writer and the socket. Your Solution is bellow:

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();
 writer.close();
 socket.close();

Below you have an example for serialization : (send an Object through the network or save it to a file in original state for future replication)

    ...
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(record);
oos.flush();
oos.close();
socket.close();

Upvotes: 0

Chris
Chris

Reputation: 23171

You need to combine what both Nick and Nikolai said: You need to write with println (since readLine is expecting an end-of-line) and you need to flush your writer before closing it.

 Socket socket = new Socket(InetAddress.getLocalHost(), 4445);
 PrintWriter writer = new PrintWriter(socket.getOutputStream());
 writer.println("Test.jpg");
 writer.flush();

Upvotes: 2

Krishan
Krishan

Reputation: 641

This is because your server exits. Use a while loop to send. See this. Here what happens is before the data is retrieved at the client side, server program exits so that connection is reset and the exception you see is thrown at the client side.

Upvotes: 0

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

Your code is working perfectly fine for me... have you forgotten to add

    writer.close();
    socket.close();

to your writer?

Upvotes: 3

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

The receiver is waiting for end-of-line, which your sender never sends. Try something like println() on the sending side.

Upvotes: 3

Nick Garvey
Nick Garvey

Reputation: 3000

I would suggest flushing your writer before you try and read, there is a chance the data is never being sent before you try to read it.

Upvotes: 1

Related Questions