bufferedreader挂起readline

时间:2013-11-09 09:12:33

标签: bufferedreader readline hang

我的客户端 - 服务器应用程序和BufferedReader有问题。我的客户端挂起readline()。我已经在这里阅读了很多帖子,但它并没有真正帮助。我知道readline()读到文件结尾但我不知道如何解决它...

这是我的服务器代码:

    public class Server {
private List<PrintWriter> clients;

public Server() {
    clients = new LinkedList<PrintWriter>();
}

// start Server and accept Clients
public void start() {
    try {
        System.out
                .println("Binding to port " + PORT + ", please wait  ...");
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Server started: " + s);
        System.out.println("Waiting for Clients...");
        // accept clients
        for (;;) {
            Socket incoming = s.accept();
            System.out.println("Client accepted : "+incoming);
            new ClientHandler(this, incoming).start();
        }

    } catch (IOException ioe) {
        System.out.println(ioe);
    } catch (Exception e) {
        System.out.println(e);
    }
}

public void removeClient(PrintWriter out) {
    synchronized(clients) {
        System.out.println("Client removed");
    clients.remove(out);
    }
}

public void addClient(PrintWriter out) {
    synchronized(clients) {
        System.out.println("Client added");
    clients.add(out);
    }
}

public void broadcast(String msg) {
    for (PrintWriter writer : clients) {
        System.out.println("Broadcast MSG");
        writer.println(msg);
        writer.flush();
    }

}

public static void main(String[] args) {
    if (args.length > 0) {
        System.out.println(USAGE);
        System.exit(-1);
    }

    Server server = new Server();
    server.start();
}
    }

    public class ClientHandler extends Thread {
/** Socket to read client messages. */
private Socket incoming;
private Server server; 
String msg = null;

/** Creates a hander to serve the client on the given socket. */
public ClientHandler(Server _server, Socket _incoming) {
    this.server = _server;
    this.incoming = _incoming;
}

public void run (){
    PrintWriter out = null; 
    try{

        out = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));

        server.addClient(out);

        BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));

        for (;;){

            msg = in.readLine();
            if(msg == null){
                break; 
            }
            else {
                if(msg.trim().equals("BYE"))
                    break;

                System.out.println("Recieved Message: "+msg);
                server.broadcast(msg);
            }

        }

        out.println("Hello"+"\n");
        out.flush();

        incoming.close();
        server.removeClient(out);
    }catch (Exception e){
        if (out != null)
            server.removeClient(out);
        System.out.println("Exception "+e.getMessage());
        //e.printStackTrace();
    }
}

    }

这是我的客户代码: ...

    public void connectToServer() {
    new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                socket = new Socket(id, port);
                try {
                    in = new BufferedReader(new InputStreamReader(
                            socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                 out.println("Hello\n");
                 out.flush();

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

}

public void recieveMessage2() {
    String msg = null;

    if(in != null)
        try {
            msg = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

    if (msg != null)
        readMessages(msg);
    else 
        Log.d(TAG, "msg = null");

感谢您的所有答案:)

0 个答案:

没有答案