从套接字获取数据时,BufferedReader readLine()冻结

时间:2014-10-06 17:28:32

标签: java sockets bufferedreader printwriter bufferedwriter

我在服务器端有这个代码

    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            LinkedList <String[]> temp = new LinkedList<String[]>();
            String cognome = "";
            String nome = "";
            while (!(cognome=reader.readLine())!=null){
                System.out.println(cognome);
                nome = reader.readLine();
                System.out.println(nome);
                temp.addLast(new String[]{cognome,nome});
                System.out.println("ricevuto il docente "+cognome+" "+nome);
            }
            mutex.acquire();
            for(;0<temp.size();)
                docenti.add(temp.removeFirst());
            mutex.release();
            if (isRunning.tryAcquire()){
                aggiungiDocenti();
                isRunning.release();
     }

现在,服务器挂起“while((cognome = reader.readLine())!= null),我尝试使用PrintWriter(使用println(mex)和使用print(mex +”\ r \ n) n“))并使用BufferedWriter(使用write(mex)和newLine())但是服务器挂在while行上......它有什么问题?HEre是我的实际客户端代码:

        Socket s = new Socket(ip,Integer.parseInt(port));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        while (model.effectivilyCount()>0){
            writer.write(model.getValueAt(0, 0)+"\r\n");
            writer.write(model.getValueAt(0, 1)+"\r\n");
            model.removeRow(0);
        }
        writer.close();
        s.close();

0 个答案:

没有答案