import java.awt.Color;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * 
 * @author tian QQ�ͻ���
 */
public class Client extends Clientframe {
	Message receivemsg; // �����û�֮���������Ϣ
	Usermessage usermsg; // �û���Ϣ
	Socket s;
	ObjectOutputStream oos;
	String ipaddress; // ������IP
	ArrayList al = new ArrayList();
	String cmd;
	boolean flag = false;

	/**
	 * ���췽��
	 */
	public Client() {
		usermsg = new Usermessage();
		
		try {
			usermsg.ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e3) {
			e3.printStackTrace();
		}
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//����ʱ�����������û���Ϣ(usermsg)�������
		jb1.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				String ipaddress = jtfname2.getText(); // �õ�������IP
				
					try {
						if (flag == false) {
						s = new Socket(ipaddress, 9999);
						oos = new ObjectOutputStream(s.getOutputStream());
						flag = true;
						}
					} catch (UnknownHostException e2) {
						e2.printStackTrace();
					} catch (IOException e2) {
						e2.printStackTrace();
					}
					try {
						usermsg.username = jtfname.getText();
						usermsg.password = jtfpwd.getText();
						usermsg.flag1 = true; // ��½����Ϊtrue
						oos.writeObject(usermsg);
						oos.reset();
					} catch (UnknownHostException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					ClientThread ct = new ClientThread();
					ct.start();
				}
		});

		
//		  �����û�������Ϣ
		jfb.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Usermessage usermsg1 = new Usermessage();
				usermsg1.flag1 = false; // �����û�����
				usermsg1.username = usermsg.username;
				usermsg1.password = usermsg.password;
				try {
					usermsg1.ip = InetAddress.getLocalHost().getHostAddress();
				} catch (UnknownHostException e2) {
					e2.printStackTrace();
				}
				// ���û���Ϣ���������˵���ÿͻ�Ҫ����
				try {
					oos.writeObject(usermsg1);
					System.exit(0);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

//		  ����˫�������û��б��¼�
		
		jli.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					JList source = (JList) e.getSource();
					Object[] selection = source.getSelectedValues();
					cmd = (String) selection[0];
					String[] ss = cmd.split("\\s+");
					jfaa.setTitle("��" + ss[0] + "����");
					jfaa.setVisible(true);
				}

			}

			public void mouseEntered(MouseEvent e) {

			}

			public void mouseExited(MouseEvent e) {

			}

			public void mousePressed(MouseEvent e) {

			}

			public void mouseReleased(MouseEvent e) {

			}

		});

		//����˽��send��ť
		jbaa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientUtil cu = new ClientUtil();
				Message m = new Message();
				m.fromname = usermsg.username;
				m.msg = jtabb.getText();
				String[] ss = cmd.split("\\s+");
				m.toname = ss[0];
				m.flag = true;
				try {
					oos.writeObject(m);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				jtabb.setText("");
//				 ����ClientUtil�е�dealmessage()��������Ϣ���д���
				ArrayList msglist = ClientUtil.dealmessage(m.msg);
				// ����ClientUtil�е�printmsg()����Ϣ��ʾ�������
				cu.printmsg(msglist, jtaaa,m);
			}

		});

		
		 // ����Ⱥ��send��ť
		jba.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Message m = new Message();
				m.fromname = usermsg.username;
				m.msg = textPane1.getText();
				m.toname = "alluser";
				try {
					oos.writeObject(m);
				} catch (IOException e1) {
					// e1.printStackTrace();
				}
				textPane1.setText("");
			}

		});

		// ����Transportfile��ť,ʵ���ļ�����,ֻ�ܴ���С�ļ�
		jbbc.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				FileDialog f = new FileDialog(jfaa, "�����", FileDialog.LOAD);
				f.setVisible(true);
				String filename = f.getDirectory() + f.getFile();
				File file = new File(filename);
				FileInputStream fis;
				try {
					fis = new FileInputStream(file);
					int len = (int) file.length();
					byte[] data = new byte[len];
					fis.read(data);
					Message m = new Message();
					m.data = new byte[len]; 
					m.fromname = usermsg.username;
					String[] ss = cmd.split("\\s+");
					m.toname = ss[0];
					m.flag = false;
					
					for (int i = 0; i < data.length; i++) {
						m.data[i] = data[i];
					}
					oos.writeObject(m);
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e2) {
					e2.printStackTrace();
				}

			}

		});

		// ����"Ⱥ��"��ť
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jfa.setVisible(true);
			}

		});
	}

	
	 //��ʾ�����û��б�(�ɷ���������)
	
	class ClientThread extends Thread {
		ObjectInputStream ois;

		public void run() {
			ClientUtil cu = new ClientUtil();
			try {
				ois = new ObjectInputStream(s.getInputStream());
				while (true) {
					Object[] o = (Object[]) ois.readObject();

					// ���?�û���������Ϣ
					if (o[0] instanceof String) {
						String s1 = (String) o[0];
						if (s1.equals("�û����ظ���������д")) {
							jtfname3.setText(s1);
						} else {
							jfb.setTitle(jtfname.getText());
							jfb.setVisible(true);
							jf.setVisible(false);
						}
					}

					// �����û���Ϣ
					if (o[0] instanceof Usermessage) {
						Usermessage u = (Usermessage) o[0];
						// �����û�������Ϣ
						if (u.flag1) {
							dlm.clear();
							for (int i = 0; i < o.length; i++) {
								dlm.addElement(o[i].toString());
							}
						} else {
							// �����û�������Ϣ
							for (int i = 0; i < dlm.size(); i++) {
								String s = dlm.get(i).toString();
								String[] ss = s.split("\\s+");
								if (ss[0].equals(u.username)) {
									dlm.remove(i);
									break;
								}
							}
						}
					}

					// ����������Ϣ
					if (o[0] instanceof Message) {
						Message u = (Message) o[0];
						// ����Ⱥ����Ϣ
						if (u.toname.equals("alluser")) {
							// ����ClientUtil�е�dealmessage()��������Ϣ���д���
							ArrayList msglist = ClientUtil.dealmessage(u.msg);
							// ����ClientUtil�е�printmsg()����Ϣ��ʾ�������
							cu.printmsg(msglist, textPane,u);
						}
						// ����˽����Ϣ
						if (u.toname.equals(usermsg.username)) {
							if (u.flag) {
								jfaa.setVisible(true);
								jfaa.setTitle("��" + u.fromname + "����");
//								 ����ClientUtil�е�dealmessage()��������Ϣ���д���
								ArrayList msglist = ClientUtil.dealmessage(u.msg);
								// ����ClientUtil�е�printmsg()����Ϣ��ʾ�������
								cu.printmsg(msglist, jtaaa,u);
								cmd = u.fromname;
							} else {
//								��������ļ�
								FileDialog f = new FileDialog(jfaa,
										"�����", FileDialog.SAVE);
								f.setVisible(true);
								String filename = f.getDirectory()
										+ f.getFile();
								FileOutputStream fos = new FileOutputStream(
										filename);
								byte[] buf = u.data;
								fos.write(buf);
								fos.close();
							}
						}
					}
				}
			} catch (IOException e) {
				 e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		Client c = new Client();
	}
}
最近下载更多
微信网友_7004855557083136  LV1 2024年5月22日
cksndh  LV4 2023年8月16日
fuyouou  LV5 2023年6月29日
yuanchuang  LV22 2023年2月14日
liys1234  LV9 2022年6月21日
微信网友_6003487859068928  LV5 2022年6月15日
微信网友_5989987974549504  LV5 2022年6月5日
17岁的孩子想糖吃  LV7 2021年12月31日
111111255  LV2 2021年12月24日
Aoifee  LV4 2021年11月10日
最近浏览更多
微信网友_7004855557083136  LV1 2024年5月22日
goccgoccgocc  LV4 2024年5月9日
krispeng  LV14 2024年4月15日
陈小灏  LV18 2023年12月26日
luamweise 2023年10月18日
暂无贡献等级
wertttak  LV1 2023年9月26日
小新Coding  LV9 2023年9月7日
cksndh  LV4 2023年8月16日
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友