Java多线程客户端服务器

时间:2013-01-29 20:03:35

标签: java sockets stream serversocket

我遇到了一些问题,尝试在java中创建客户端/服务器系统,下一个代码属于客户端...

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

...这个属于服务器,并为每个客户端创建了线程:

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

线程创建类......

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

很抱歉代码的数量,但是由于这个问题,我很疯狂,而且我认为错误的是大胆的那个,我认为这会产生一些阻止我的问题/ O,但我不知道为什么,以及如何解决它,提前谢谢

2 个答案:

答案 0 :(得分:1)

客户端正在等待服务器发送一些文本行。你的服务器没有发送任何东西,所以它会永远等待。

我建议你让服务器向客户端发送一些东西供它阅读。

答案 1 :(得分:0)

问题在于ClientThreads上的这一行:

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

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

我只需要添加到循环结束,休息一下,否则,总会要求一些代码,换句话说,它永远不会阻止I / O