想在Thread A中,利用PipedOutputStream写入数据
在Thread B中,循环读取数据
只要 Thread A写数据了,线程B就读Thread A
负责接收user 输入
并用 管道 ,将 数据发送给 Thread B
Thread B
读取Thread A传来的数据
while(true)
{
  byte[] bytes = new byte[100];
  int bytesLength=input.read(bytes); //这里第2次会抛出异常..
  String srcStr=new String(bytes,0,bytesLength);
}可在 Thread B中,
循环第1次 能够正确读取信息
循环第2次 抛出了 IOException并且提示
write end dead不知道什么原因?

解决方案 »

  1.   


    if (writeSide != null && !writeSide.isAlive()&& !closedByWriter && (in < 0)) {   
                throw new IOException("Write end dead");   
            }   你那个Write end dead来自这里
      

  2.   


    好的!Thread A
    主线程,用于向 ThreadB 发送消息
    public class ClientLaunch { private static InetAddress SERVERADDRESS = null;
    private static int SERVERPORT = 8008; public static void main(String[] args) throws IOException {
    System.out.println("Welcome to client!"); //log SERVERADDRESS = InetAddress.getLocalHost(); //init
    Socket server = new Socket(SERVERADDRESS, SERVERPORT); //描述服务器 // UI线程 与 通信线程 的 桥梁
    PipedInputStream input = new PipedInputStream();
    PipedOutputStream output = new PipedOutputStream();
    input.connect(output);
    SendClient sc = new SendClient(server, input); //发送线程
    Thread thSend = new Thread(sc);
    thSend.start(); RecvClient rc = new RecvClient(server); //接收线程
    Thread thRecv = new Thread(rc);
    thRecv.start();

    boolean isFresh = true; //是否是刚上线 Message msg = null; //第一次上线 须通知Server 使Server更新在线用户列表
    if (isFresh) { msg = Function.notifyServerReach(); output.write((msg.isTalkFlag() + "#" + msg.getDesAdd() + "#" + msg
    .getContent()).getBytes());

    output.flush();


    isFresh = false;

    msg=null;
    } }
    }Thread B 接收消息
    在第一次接收的时候,能够收到数据
    循环第2次的时候,就 IOException了public class SendClient implements Runnable { private Socket server = null; //目的 服务器

    private PipedInputStream  input=null; //等待UI发来消息

    private boolean isConn=true; public SendClient(Socket server,PipedInputStream  input) {
    this.server = server;
    this.input=input;
    } public void run() {
    try {// // out 用于 向外界发送
    ObjectOutputStream out=new ObjectOutputStream(server.getOutputStream());

    while(isConn)
    {
    byte[] bytes = new byte[100];
    int bytesLength=input.read(bytes); //这里会线程等待吗? 这里第2此会抛出异常..
    String srcStr=new String(bytes,0,bytesLength);

    System.out.println("User will send: "+srcStr); //test
    String[] msgStr=srcStr.split("#");

    //第1字段 flag 第2字段 destination 第3字段content
    Message sendMsg=new Message();
    sendMsg.setTalkFlag(Boolean.parseBoolean(msgStr[0]));
    sendMsg.setDesAdd(msgStr[1]);
    sendMsg.setContent(msgStr[2]);


    out.writeObject(sendMsg);
    out.flush();

    //为什么这里不设置会抛出异常?
    //isConn=false;
    }
    server.close();
    server=null; } catch (Exception ex) {
    ex.printStackTrace(); }
    }
    }
      

  3.   

    有许多冗余,主要是看
    PipedOutputStream和
    PipedInputStream通信的部分
      

  4.   

    非常感谢你的提示!http://jinyan798.javaeye.com/category/50277?show_full=true我看了这篇文章,明白了 一点
    如果写线程结束了,写线程的 PipedOutputStream 对象也就没有了
    好像这样就会引起 PipedInputStream读取出现问题。
      

  5.   

    B里while中应该判断发送方是否有效,无效应该sleep等待一秒,
    直到等待的时间累计到一定程度,比如5秒,然后判定发送端确实是发送结束了
    isConn=false;
    否则你这样两个线程,接收方还要读,发送方还没发,就挂了
      

  6.   

    你没有把PipeWriter关闭掉,导致!closedByWriter为true.从而引发Write end dead.