一个简单的QQ聊天程序,基于服务器很客户端的,但是我写好以后调试了两天都没调试出来。。。不知道错在哪里(希望大家帮帮忙,我没多少分数能给了,只剩20分)
要实现四个功能:服务器给每个客户端显示上线信息;单发;群发;退出
package org.jndx.client;
public class ChatClient extends JFrame implements ActionListener{
private static JPanel JPanel_north=new JPanel();
private static JPanel JPanel_center=new JPanel();
private static JPanel JPanel_south=new JPanel();
private static JLabel JLabel_north=new JLabel("简易QQ系统");
private static JTextField JTextField_loginName=new JTextField(10);
static JTextArea JTextArea_reciever=new JTextArea(10,10);
static JTextArea JTextArea_deliver=new JTextArea(10,10);
private static JScrollPane JScrollPane_reciever=new JScrollPane(JTextArea_reciever);
private static JScrollPane JScrollPane_deliver=new JScrollPane(JTextArea_deliver);
static JComboBox JComboBox_loginName=new JComboBox();
private static JButton JButton_login=new JButton("login");
private static JButton JButton_send=new JButton("send");
private static JButton JButton_quit=new JButton("quit");
Socket socket;
InetAddress address;
public ChatClient(){
this.getContentPane().add(JPanel_north,BorderLayout.NORTH);
JPanel_north.add(JLabel_north);
this.getContentPane().add(JPanel_center,BorderLayout.CENTER);
JPanel_center.setLayout(new GridLayout(2,2));
JPanel_center.add(JTextField_loginName);
JPanel_center.add(JScrollPane_reciever);
JPanel_center.add(JScrollPane_deliver);
JPanel_center.add(JComboBox_loginName);
this.getContentPane().add(JPanel_south,BorderLayout.SOUTH);
JPanel_south.add(JButton_login);
JPanel_south.add(JButton_send);
JPanel_south.add(JButton_quit);
//JComboBox_loginName.addActionListener(this);
JButton_login.addActionListener(this);
JButton_send.addActionListener(this);
JButton_quit.addActionListener(this);
}
public void actionPerformed(ActionEvent ee){
if(ee.getActionCommand().equals("login")){
try {
address=InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(address,7000);
String ip=InetAddress.getLocalHost().getHostName();
String id=JTextField_loginName.getText().trim();
StringBuffer buffer=new StringBuffer("People:");
buffer.append(ip).append(":"+id);
System.out.println(buffer);
PrintWriter pw=new PrintWriter(socket.getOutputStream());
pw.println(buffer.toString());
pw.flush();
ClientThread ct=new ClientThread(this,socket);
ct.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ee.getActionCommand().equals("send")){
String user=(String)JComboBox_loginName.getSelectedItem();
String message=JTextArea_deliver.getText().trim();
System.out.println(message);
StringBuffer buffer=new StringBuffer("Msg:");
buffer.append(user).append(":"+message);
System.out.println(buffer);
try {
PrintWriter pw=new PrintWriter(socket.getOutputStream());
pw.println(buffer.toString());
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ee.getActionCommand().equals("quit")){
String id=JTextField_loginName.getText().trim();
StringBuffer buffer=new StringBuffer("quit:");
buffer.append(id);
this.dispose();
try {
PrintWriter pw=new PrintWriter(socket.getOutputStream());
pw.println(buffer.toString());
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ChatClient cc=new ChatClient();
cc.pack();
cc.setVisible(true);
}
}
package org.jndx.client;
public class ClientThread extends Thread {
ChatClient client;
Socket socket;
BufferedReader br;
ClientThread(ChatClient client,Socket socket){
this.client=client;
this.socket=socket;
}
public void run(){
while(true){
//PEOPLE:
//MSG:
String message =null;
try {
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
message=br.readLine();
String[] mess=message.split(":");
if(mess[0].equals("People")){
client.JComboBox_loginName.removeAllItems();
for(int i=0;i<mess.length;i++){
client.JComboBox_loginName.addItem(mess[i]);
}
}
if(mess[0].equals("Msg")){
client.JTextArea_deliver.setText(mess[1]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package org.jndx.server;
public class Server {
private static List list=new ArrayList();
private static ServerSocket server=null;
private static Socket socket=null;
public static String getAllUsers(){
StringBuffer buffer=new StringBuffer("People");
for(int i=0;i<list.size();i++){
ServerThread st1=(ServerThread)list.get(i);
String id=st1.getId1();
buffer.append(":").append(id);
}
return buffer.toString();
}
public static void sendLoginInfomationToAllUser(String message){
for(int i=0;i<list.size();i++){
ServerThread st2=(ServerThread)list.get(i);
Socket socket=st2.getSocket();
try {
OutputStream os = socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.println(message);
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static List getList() {
return list;
}
public static void setList(List list) {
Server.list = list;
}
public static void main(String[] args) {
//主线程处理共同事件
// TODO Auto-generated method stub
boolean flg=true;
try {
server=new ServerSocket(7000);
System.out.println("Server is beginning...");
while(flg){
socket=server.accept();
ServerThread st=new ServerThread(socket);
list.add(st);
//子线程处理各自的事件
st.start();
String message=Server.getAllUsers();
Server.sendLoginInfomationToAllUser(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package org.jndx.server;
public class ServerThread extends Thread {
private String ip1;
private String id1;
private Socket socket;
private boolean flg=true;
BufferedReader br=null;
public ServerThread(Socket socket){
this.socket=socket;
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message=br.readLine();
String[] mess=message.split(":");
this.ip1=mess[1];
this.id1=mess[2];
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getIp1() {
return ip1;
}
public void setIp1(String ip1) {
this.ip1 = ip1;
}
public String getId1() {
return id1;
}
public void setId1(String id1) {
this.id1 = id1;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public void run(){
while(flg){
try {
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message=br.readLine();
System.out.println(message);
String[] mess=message.split(":");
List list=Server.getList();
if(mess[0].equals("Msg")){
if(mess[1].equals("People")){
for(int i=0;i<list.size();i++){
ServerThread st=(ServerThread)list.get(i);
Socket socket1=(Socket)st.getSocket();
OutputStream os = socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.println(mess[0]+mess[2]);
pw.flush();
}
}
else{
String ss1=null;
String ss2=null;
for(int i=0;i<list.size();i++){
ServerThread st=(ServerThread)list.get(i);
String user=st.getId1();
if(mess[1].equals(user)){
Socket socket2=(Socket)st.getSocket();
OutputStream os = socket2.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.println(mess[0]+mess[2]);
pw.flush();
}
}
}
}
if(mess[0].equals("quit")){
this.flg=false;
list.remove(this);
String mes=Server.getAllUsers();
Server.sendLoginInfomationToAllUser(mes);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}