接下来是客户端的代码,主要是这个类!
帮忙分析下,多个客户端连接后,莫个客户端断开连接后,登录的user信息list有时总为空呢?为什么啊?
Client.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class Client extends JFrame {
private JButton topButton = null;
private JList userList = null;
private JTextArea msgShowArea = null;
private JTextField msgInputField = null;
private JButton sendButton = null;
private JPanel topPanel = null;
private JPanel bottomPanel = null;
private String ipAddr = "localhost";
private int serverPort = 8989;
private boolean isConnected = false;
MsgRecvThread socketThread = null;
Socket client = null;
BufferedReader socketIn = null;
PrintWriter socketOut = null;
DataModel dm;
Vector users;
public Client(String title) {
super(title);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
topButton = new JButton("连接服务器");
//users = new Vector();
dm = new DataModel();
userList = new JList(dm);
userList.setSize(40, 400);
msgShowArea = new JTextArea(160, 80);
msgInputField = new JTextField(40);
sendButton = new JButton("发送");
topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
topPanel.add(topButton);
c.add(topPanel, BorderLayout.NORTH);
c.add(new JScrollPane(userList), BorderLayout.WEST);
c.add(new JScrollPane(msgShowArea), BorderLayout.CENTER);
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(msgInputField);
bottomPanel.add(sendButton);
c.add(bottomPanel, BorderLayout.SOUTH);
this.setBounds(200, 200, 600, 400);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stopConnect();
System.exit(0);
}
});
this.setVisible(true);
topButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isConnected)
startConnect();
else
stopConnect();
}
});
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isConnected)
sendMsg();
}
});
}
public static void main(String[] args) {
new Client("简易多人聊天室");
}
public void startConnect() {
try {
topButton.setEnabled(false);
client = new Socket(ipAddr, serverPort);
socketIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketOut = new PrintWriter(client.getOutputStream(), true);
msgShowArea.append("--- 连接服务器成功 --- \n");
topButton.setText("断开连接");
topButton.setEnabled(true);
isConnected = true;
//(new Thread(new MsgRecvThread())).start();
socketThread = new MsgRecvThread();
socketThread.start();
} catch (IOException e) {
msgShowArea.append("--- 连接服务器失败! --- \n");
isConnected = false;
topButton.setText("连接服务器");
topButton.setEnabled(true);
}
}
public void stopConnect() {
try {
//socketThread.stop=true;
if (socketOut != null) {
socketOut.println("QUIT");
socketOut.close();
isConnected = false;
topButton.setText("连接服务器");
topButton.setEnabled(true);
//users = new Vector();
//
for(int i=0;i<dm.getSize();i++){
//
dm.removeIndex(i);
//
}
dm.clearAll();
userList.updateUI();
msgShowArea.append(" --- 与服务器的连接已断开 --- \n");
}
socketThread.stop = true;
//
if (socketIn != null)
//
socketIn.close();
//
if (client != null)
//
client.close();
} catch (Exception e) {
msgShowArea.append(" ---操作失败! --- \n");
}
}
public void sendMsg() {
String msg = msgInputField.getText();
try {
socketOut.println("MESG: " + msg);
msgShowArea.append(" --- 信息已发出! --- \n");
} catch (Exception e) {
msgShowArea.append(" --- 发送失败! ---\n");
}
}
class MsgRecvThread extends Thread {
boolean stop = false;
public void run() {
try {
String rcvStr = "";
while (true) {//!(rcvStr = socketIn.readLine()).equals("")
if (stop) {
if (socketIn != null)
socketIn.close();
if (client != null)
client.close();
return;
}
rcvStr = socketIn.readLine();
if (rcvStr.startsWith("USER: ")) {
System.out.println("好友上线" + rcvStr.substring("USER: ".length()));
dm.addData(rcvStr.substring("USER: ".length()));
userList.updateUI();
//Client.super.repaint();
} else if (rcvStr.startsWith("MESG: ")) {
msgShowArea.append(rcvStr.substring("MESG: ".length()) + "\n");
} else if (rcvStr.startsWith("QUIT: ")) {
System.out.println("好友下线" + rcvStr.substring("QUIT: ".length()));
dm.removeData(rcvStr.substring("QUIT: ".length()));
userList.updateUI();
msgShowArea.append(rcvStr.substring("QUIT: ".length()) + "退出聊天\n");
}
//rcvStr = "";
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class DataModel extends AbstractListModel {
private Vector data = new Vector();
//public DataModel(Object[] listData) {
public DataModel() {
//data = new Vector();
//for (int i = 0; i < listData.length; i++) {
//
data.add(listData);
//}
}
public void removeData(Object o) {
data.remove(o);
}
public void clearAll() {
data.clear();
}
public void addData(Object o) {
data.add(o);
}
public void removeIndex(int index) {
data.remove(index);
}
public int getSize() {
return data.size();
}
public Object getElementAt(int index) {
return data.get(index);
}
}
[ 本帖最后由 hanjs 于 2008-1-24 19:33 编辑 ]
|