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

追逐永恒 2014-11-05 04:51:36
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();
}
}
}

}

问题如题目,新手不太懂希望高人指点。
...全文
292 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
追逐永恒 2014-11-05
  • 打赏
  • 举报
回复
引用 5 楼 shnulaa 的回复:
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.
不好意思啊还有地方不理解,如果Server端和Client端各自有指向对方Socket的dis和dos,那么Server的dos和Client的dis是一条线路吗,删除会互相影响Socket吗?
追逐永恒 2014-11-05
  • 打赏
  • 举报
回复
引用 4 楼 cumtwyc 的回复:
[quote=引用 2 楼 scmod 的回复:] 没错啊?我怎么试了下没报错...就是发一次之后断开了然后才会报connect的错误啊
我运行的结果: [/quote] 谢谢帮助,问题已解决
追逐永恒 2014-11-05
  • 打赏
  • 举报
回复
引用 5 楼 shnulaa 的回复:
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.
嗯嗯,链接已经看了,正好解决了我的疑惑,非常感谢!
晓风吹雾 2014-11-05
  • 打赏
  • 举报
回复
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.
wyc_ 2014-11-05
  • 打赏
  • 举报
回复
引用 2 楼 scmod 的回复:
没错啊?我怎么试了下没报错...就是发一次之后断开了然后才会报connect的错误啊

我运行的结果:

追逐永恒 2014-11-05
  • 打赏
  • 举报
回复
引用 2 楼 scmod 的回复:
没错啊?我怎么试了下没报错...就是发一次之后断开了然后才会报connect的错误啊
有错误,你试一下启3,4个以上的Client端,关闭其中的一个其它的就会出错。但是保留注释行就能正常运行,就是不明白为什么dos不能关闭...
scmod 2014-11-05
  • 打赏
  • 举报
回复
没错啊?我怎么试了下没报错...就是发一次之后断开了然后才会报connect的错误啊
wyc_ 2014-11-05
  • 打赏
  • 举报
回复
System.out.println("客户端退出。");前面打印一下异常信息

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧