3

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?

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

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

to your writer?

Comments

2

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();

3 Comments

Doesn't writer.close(); flush the stream?
It does, i was assuming it was being left open for additional writing.
I use the flush() but nothing happened please help
1

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.

Comments

0

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.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.