class ReadWorkThread extends Thread {
        public void run() {
            try {
                while (!exitFlag) {
                    String str = in.readUTF();
                    if (str != null) {
                        ui.receiveTF.setString(str);
                    }
                }
            } catch (IOException e) {
                if (!exitFlag)
                    ui.state.setText("读取数据失败");
            }
        }
    }
class WriteWorkThread extends Thread {
        DataOutputStream out = null;
        public WriteWorkThread(DataOutputStream out){
            this.out = out;
        }
        public void run() {
            try {
                while (!exitFlag) {
                    synchronized (this) {
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (exitFlag)// 可能因为关闭操作被打断
                            break;
                        if (sendText != null)
                            out.writeUTF(sendText);
                            out.flush();
                    }
                }
                out.close();
            } catch (IOException e) {
                if (!exitFlag)
                    ui.state.setText("写数据失败");
            }
        }
    }
这段程序读线程在第一次读取的时候没有任何问题,但是第二次就会发生IOException
"读取数据失败",不知道为什么?

解决方案 »

  1.   

    synchronized (this) {
                            try {
                                wait();
                            } catch (InterruptedException e) {
    你的代码不全吧,这里在等待,没有看到你notify();或notifyAll()方法
      

  2.   

    是不全 太长了  别的地方有notify  但是写线程第二次writeUTF的时候  读线程却抛IOException“读取数据失败”  为什么读线程只能读一次呢?
      

  3.   

    1个聊天的程序,一方发一方收
    java.io.IOException:存储空间不足,无法处理此命令 
    这是什么错误?
      

  4.   

    用两个TCP连接试试,,既双方都实现ServerSocket监听一个端口,比如:9001.9002
    然后,各自写连对方端口的客户端,监听端口的方法只接收输入流,
    连接端口的客户端只发送输出流,把逻辑搞清楚,
    思路就这些,参考下