刚刚接触java,一个模拟多线程的聊天程序,不知道注释掉的代码为什么删掉程序正常运行,保留程序出错.

   阅读
刚接触java,一个模拟多线程的聊天程序,不知道注释掉的代码为什么删掉程序正常运行,保留程序出错...
Server端:
package chatAgain;
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatServer {
boolean bConnected = false;
boolean serverAlive = false;
List<Socket> al = new ArrayList<Socket> ();//java.lang.NullPointerException解决

public static void main(String[] args) {
new ChatServer().begin();
}

public void begin() {
ServerSocket ss;
try {
ss = new ServerSocket(7698);
serverAlive = true;
while (serverAlive) {
Socket s = ss.accept();
System.out.println("A client connected!");
al.add(s);
bConnected = true;
if(bConnected) {
Thread t = new Thread(new Client(s, true));
t.start();
}
}
} catch (BindException b) {
System.out.println("端口已经被使用,打开失败。");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}

public class Client implements Runnable {
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
boolean sAlive = false;

Client(Socket s, boolean sAlive) {
this.s = s;
this.sAlive = sAlive;
}

public void run() {
String str = null;
while (sAlive) {
try {
dis = new DataInputStream(s.getInputStream());
str = dis.readUTF();
System.out.println(str);
Iterator<Socket> i = al.iterator();
while (i.hasNext()) {
Socket sTemp = i.next();
if(!(sTemp.equals(this.s))) {
dos = new DataOutputStream(sTemp.getOutputStream());
try {
dos.writeUTF(str);
} catch (IOException e) {

}
}
}
} catch (IOException e) {
System.out.println("客户端退出。");
this.sAlive = false;
al.remove(this.s);
} finally {
if (this.s != null) {
try {
this.s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//??????????????????????
// if (this.dos != null) {
// try {
// this.dos.close();
// } catch (IOException e2) {
// e2.printStackTrace();
// }
// }
//??????????????????????
if (this.dis != null)
try {
this.dis.close();
} catch (IOException e3) {
e3.printStackTrace();
}

}
}
}

}

Client端:
package chatAgain;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
TextArea ta = null;
TextField tf = null;
boolean bConnected = false;
DataOutputStream dos;
DataInputStream dis;

public static void main(String[] args) {
new ChatClient().launchFrame();
}

public void launchFrame() {
this.setLocation(600, 50);
this.addWindowListener(new CloseMonitor());
setResizable(true);
Panel p = new Panel();
p.setLayout(new BorderLayout());
ta = new TextArea(20, 50);
tf = new TextField(20);
tf.addActionListener(new MyMonitor());
p.add(ta, BorderLayout.NORTH);
p.add(tf, BorderLayout.SOUTH);
add(p);
pack();
setVisible(true);

connect();
Thread t = new Thread(new TextReceive());
t.start();
}

public void connect() {
try {
s = new Socket("192.168.1.115", 7698);
bConnected = true;
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

class TextReceive implements Runnable {
public void run() {
while(bConnected) {
try {
ta.append(dis.readUTF());
ta.append("\n");
} catch (IOException e) {
}
}
}
}

class MyMonitor implements ActionListener {
public void actionPerformed (ActionEvent e) {
String str = ((TextField) e.getSource()).getText();
try {
ta.append(str);
ta.append("\n");
tf.setText("");
dos.writeUTF(str);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

class CloseMonitor extends WindowAdapter {
public void windowClosing (WindowEvent e) {
setVisible(false);
System.exit(0);
bConnected = false;
try {
dis.close();
dos.close();
s.close();
} catch (IOException i) {
i.printStackTrace();
}
}
}

}

问题如题目,新手不太懂希望高人指点。
------解决思路----------------------
 System.out.println("客户端退出。");前面打印一下异常信息
------解决思路----------------------
没错啊?我怎么试了下没报错...就是发一次之后断开了然后才会报connect的错误啊
------解决思路----------------------
dos 是从socket -> OutputStream 

你关闭了dos,间接的会影响 socket 

http://stackoverflow.com/questions/8890303/behavior-of-java-sockets-when-closing-output-stream
from the description above, we know closing the returned OutputStream will close the associated socket.
阅读