Search code examples
javasocketsstreamserversocket

Java multi-threading client server


I'm having some problems, trying to make a client/server system in java, the next code belongs to the client...

public class ClientTCP {

    final static int PORT = 65001;

    Socket _socket;

    private void fireUp(){

        String message = "";

        BufferedReader bfi = null;
        BufferedReader bfr = null;
        PrintWriter writer = null; 

        try{

            InetAddress address = InetAddress.getByName("localhost");

            _socket = new Socket(address,PUERTO);

            bfr = new BufferedReader(
                            new InputStreamReader(System.in));

            bfi = new BufferedReader(
                            new InputStreamReader(_socket.getInputStream()));

            System.out.println("Enter some data");

            String inputstr = bfr.readLine();

            writer = new PrintWriter(_socket.getOutputStream(),true);

            writer.println(inputstr);

            if((message = bfi.readLine()) != null){

                Logger.getLogger(ClientTCP.class.getName()).log(Level.INFO,message);
            }
        }catch(IOException ioex){

            ioex.printStackTrace();
        }finally{

            if(_socket != null){

                try {

                    _socket.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        }
    }

...and this one belongs to the server, and the threads created for every one client:

public class ServerrTCP {

    static int SEC = 0;

    final static int PORT = 65001;

    Socket _socket;
    ServerSocket _ssocket;

    private void fireUp(){

        try{

            _socket = new Socket();
            _ssocket = new ServerSocket(PORT);

            while(true){

                _socket = _ssocket.accept();

                new ClientThreads(_socket,SEC++);
            }

        }catch(IOException ioex){

            Logger.getLogger(ServerTCP.class.getName()).log(Level.SEVERE,null,ioex);
        }

Thread creation class...

public class ClientThreads extends Thread{

    int _id;
    Socket _socket;


    public ClientThreads(Socket socket, int id){

        super();

        _socket = socket;
        _id = id;

        this.start();
    }

    @Override
    public void run(){

        String strinput;

        BufferedReader bfr = null;
        PrintWriter writer = null;

        try{

            bfr = new BufferedReader(
                    new InputStreamReader(_socket.getInputStream()));

            writer = new PrintWriter(
                    _socket.getOutputStream(),true);

            while((strinput = bfr.readLine()) != null){

                System.out.println("The user "+ _id +" wrote "+ strinput);
            }

            writer.println("Thanks, user " + _id);
        }catch(IOException ioex){

            ioex.printStackTrace();
        }
    }
}

Sorry for the amount of code, but I'm going nuts because of this issue, and I thing the point where I'm getting wrong is the one in bold, I think it's generating some kind of problem on blocking the I/O, but I don't know why, and how to solve it, thanks in advance


Solution

  • The problem was in this line on the ClientThreads:

    while((strinput = bfr.readLine()) != null){
    
              System.out.println("The user "+ _id +" wrote "+ strinput);
    }
    

    I just had to add to the end of the loop, a break, otherwise, will always asking for some code, in other words, it never ends blocking the I/O