1

when I run the code for the code below for the firs time it runs just fine then I try to add more code (like creating buffered input stream in server side and try to read back from the client ) and run it again it gives me this exception in client side

Exception in thread "main" java.net.SocketException: Connection reset
        at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
        at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
        at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
        at java.base/java.net.Socket$SocketInputStream.read(Socket.java:976)
        at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
        at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
        at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
        at java.base/java.io.InputStreamReader.read(InputStreamReader.java:178)
        at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
        at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
        at client.main(client.java:20)

when I remove the added code and run it with just the reader in client side it still wont work it wont work unless I removed the inputstream from client side this is the server side

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class server
 {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(8080);
        Socket client = server.accept();
                  PrintWriter out= new PrintWriter(client.getOutputStream(),true);
                  out.write("dfdawdadwawdw");
         
           }
                        
    }

and this is the client side

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

import jdk.internal.jshell.tool.resources.l10n;

public class client {
    public static void main(String[] args) throws Exception {
                Socket java = new Socket("localhost",8080);
             InputStreamReader in = new InputStreamReader(java.getInputStream());
             BufferedReader bufferedreader = new BufferedReader(in);
            System.out.println(bufferedreader.readLine());
               }
 
    }
Ahmed Mohamed
  • 11
  • 1
  • 3
  • 1
    what do you think happens to your server after `out.write("dfdawdadwawdw");` ? – Scary Wombat Jul 16 '21 at 00:54
  • the server just closes without any issue – Ahmed Mohamed Jul 16 '21 at 00:55
  • The server exits without 1) flushing the printwriter stream or 2) closing any stream or sockets. – President James K. Polk Jul 16 '21 at 02:09
  • [PrintWriter docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/PrintWriter.html) say *...Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked...*. Notice that `write()` is not one of the methods listed that gets automatically flushed. – President James K. Polk Jul 16 '21 at 02:11

2 Answers2

0

In the server code try out.println instead of out.write:

// Server side code

import java.io.*
import java.net.*

public class server () {

    public static void main(String[] args) {
        ServerSocket server = new ServerSocket(8080);
        Socket client = server.accept();
        PrintWriter out= new PrintWriter(client.getOutputStream(),true);
        out.println("dfdawdadwawdw");
    }

}

This worked for me.

Dada
  • 6,313
  • 7
  • 24
  • 43
Thiyan
  • 1
0

According to BufferedReader.readLine(), the readLine function expects a termination.

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), a carriage return followed immediately by a line feed, or by reaching the end-of-file (EOF).

So the problem is caused by your bufferedReader that tries to read a line breaker from a stream that has nothing in it anymore.

Also please refer to java.net.SocketException: Connection reset.