Question

I want to made a Server Which makes a client and start conversation with him but IOException is occur in Handler's code I couldn't underStand why Br.readLine method throws Exception Here is code of mine Server project's package's classess and two clients abc, def classes are also

This is code of Server projects classeess...............

        package server;
        import java.io.IOException;
        import java.net.ServerSocket;
        public class Server {
            private void operate() {
                try {
                    ServerSocket serverSocket = new ServerSocket(2222);
                    while(true) new Thread(new Handler(serverSocket.accept())).start();
                } catch(IOException e){
                    System.out.println("IOException in operate method of Server");
                }
            }    
            public static void main(String[] args) {
                new Server().operate();
            }
        }

package server;
import java.io.*;
import java.net.Socket;
public class Handler implements Runnable {
    Handler(Socket s) {
        socket = s;
        counter++;
    }
    public void run() {
        try {
            while(true) System.out.println(new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine());                 //This throw the IOExceptionnnnnnnnnnnnnnnnnnnnn...............
        } catch(IOException e) {
            System.out.println("IOException in "+counter+"'s run method");
        }
    }
    private final Socket socket; 
    private static int counter =0;
}

Code of First Client ABC...........................

package abc;
import java.net.Socket;
import java.io.*;
public class Abc {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",2222);
            while(true) new PrintWriter(socket.getOutputStream()).println("HI from Abc");
        } catch(IOException e) {
            System.out.println("IOException in main ");
        }
    }
}

Code of Another Client DEf.........................

package def;
import java.io.*;
import java.net.Socket;
public class DEf {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",2222);
            while(true) new PrintWriter(socket.getOutputStream()).println("HI from Abc");
        } catch(IOException e) {
            System.out.println("IOException in main ");
        }
    }
}
Was it helpful?

Solution

Your clients request the output stream repeatedly from the socket using socket.getOutputStream(). Instead, you should invoke this method and create a corresponding writer only once, for example:

Socket socket = new Socket("localhost",2222);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
while(true) {
  writer.println("HI from Abc");
  ...
}

Same with the Handler class - create your buffered reader once.

OTHER TIPS

I have already posted answers on Server-Client Socket Communication. Please have a look.


Try this code. It might solve you problem.

Handler.java:

  • Check BufferedReader.ready() before BufferedReader.readLine()

  • Use single BufferedReader

    class Handler implements Runnable {
        private BufferedReader reader;
    
        Handler(Socket s) {
            socket = s;
            counter++;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
            try {
                while (socket.isConnected() && !socket.isClosed()) {
                    if(!reader.ready()){
                        continue;
                    }
                    //System.out.println("ready");
                    System.out.println(reader.readLine()); // This throw
                                                           // the
                } // IOExceptionnnnnnnnnnnnnnnnnnnnn...............
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("IOException in " + counter + "'s run method");
            }
        }
    
        private final Socket socket;
        private static int counter = 0;
    }
    

Abc.java:

  • Use single PrintWriter

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        while (true)
            writer.println("HI from Abc");
    

DEf.java:

  • Use single PrintWriter

        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        while (true)
            writer.println("HI from Abc");
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top