package ClientEXE;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Window.Type;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.DefaultCaret;
public class Client{
private JFrame frame;
private JScrollPane rightScroll;
private JScrollPane leftScroll;
private JPanel logPanle;
private JFrame loginframe;
private JLabel label_username;
private JLabel label_password;
private JTextField txt_login_name;
private JTextField txt_password;
private JTextField txt_login_ip;
private JTextField txt_login_port;
private JTextField txt_login_forget;
private JButton btn_submit;
private JButton btn_zhuce;
private JButton btn_forget_pass;
public DefaultListModel listModel;
private boolean isConnected = false;
private int sendfor_who;
private int server_port=0;
private ServerSocket serverSocket;
private ServerThread serverThread;
private Socket socketfor_p2p;
private boolean isConnected_p2p = false;
private ArrayList<ClientThread> clients;//客户线程数组
private PrintWriter P2P_printWriter;//点对点服务的输出流
private BufferedReader P2P_bufferReader;//点对点服务的输入流
private MessageThread_P2P messageThread_for_p2p;// 负责接收p2p消息的线程
private Map<String, Boolean> P2P_connected_user = new HashMap<String, Boolean>();
private Socket socket;
private PrintWriter writer;
private BufferedReader reader;
private MessageThread messageThread;// 负责接收消息的线程
private Map<String, User> onLineUsers = new HashMap<String, User>();// 所有在线用户
private String myIP = "";//每一个客户端都有唯一的IP地址
private JPanel panel;
private Label label;
private JScrollPane scrollPane;
private JList userList;
private Panel panel_2;
private JPanel panel_3;
private JButton button;
private JButton button_1;
private JButton button_2;
private JPanel panel_4;
private Label label_1;
private JScrollPane scrollPane_1;
private JTextArea textArea;
private JPanel panel_5;
private Button btn_send;
private Button btn_send_file;
private Button button_5;
private JTextArea textField;
private Button button_7;
private JLabel label_2;
private Button button_8;
private Button button_9;
private static Client client;
// 得到显示器屏幕的宽高
public int width = Toolkit.getDefaultToolkit().getScreenSize().width;
public int height = Toolkit.getDefaultToolkit().getScreenSize().height;
private Point OriginalPoint=null,MobileDistance=null; //鼠标移动前的位置和鼠标移动的距离
private boolean isDraging = false; //用与判断鼠标书否松开
private static final long serialVersionUID = -3081282189290446349L;
private JTextField Username,PortText,IP,GetPD;
private JPasswordField Password;
private JButton title ;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
// 主方法,程序入口
public static void main(String[] args) throws BindException {
client=new Client();
}
class SelecTry implements ListSelectionListener
{
int change=0,who;
public void valueChanged(ListSelectionEvent e){
//System.out.println("you selected:"+listModel.getElementAt(userList.getSelectedIndex()));
sendfor_who=userList.getSelectedIndex();
isConnected_p2p = false;
}
}
/**
* 连接服务器
*
* @param port
* @param hostIp
* @param name
*/
public boolean connectServer_p2p(int port, String hostIp, String name) {
// 连接服务器
try {
socketfor_p2p = new Socket(hostIp, port);// 根据端口号和服务器ip建立连接
P2P_printWriter = new PrintWriter(socketfor_p2p.getOutputStream());
P2P_bufferReader = new BufferedReader(new InputStreamReader(socketfor_p2p
.getInputStream()));
messageThread_for_p2p = new MessageThread_P2P(P2P_bufferReader);
messageThread_for_p2p.start();
P2P_connected_user.put(name,true);
isConnected_p2p = true;// 已经连接上了
return true;
} catch (Exception e) {
textArea.append("与端口号为:" + port + " IP地址为:" + hostIp+ " 的服务连接失败!" +"\t"+df.format(new Date())+ "\r\n");
isConnected_p2p = false;// 未连接上
return false;
}
}
/**
* 关闭服务
*/
@SuppressWarnings("deprecation")
public void closeServer() {
try {
if (serverThread != null)
serverThread.stop();// 停止服务器线程
for (int i = clients.size() - 1; i >= 0; i--) {
// 给所有在线用户发送关闭命令
clients.get(i).getWriter().println("CLOSE#"+frame.getTitle());
clients.get(i).getWriter().flush();
// 释放资源
clients.get(i).stop();// 停止此条为客户端服务的线程
clients.get(i).reader_ptp.close();
clients.get(i).writer_ptp.close();
clients.get(i).socket.close();
clients.remove(i);
}
if (serverSocket != null) {
serverSocket.close();// 关闭服务器端连接
}
// listModel.removeAllElements();// 清空用户列表
// isStart = false;
} catch (IOException e) {
e.printStackTrace();
// isStart = true;
}
}
// 不断接收消息的线程
class MessageThread_P2P extends Thread {
private BufferedReader reader_ptp;
// 接收消息线程的构造方法
public MessageThread_P2P(BufferedReader reader) {
this.reader_ptp = reader;
}
// 被动的关闭连接
public synchronized void closeCon() throws Exception {
System.out.println("close :**