Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package messager;
- import java.awt.EventQueue;
- import javax.swing.JOptionPane;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTree;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JButton;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.JTextField;
- import java.awt.Font;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JMenu;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.AbstractAction;
- import javax.swing.Action;
- import javax.swing.tree.DefaultTreeModel;
- import javax.swing.tree.DefaultMutableTreeNode;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JScrollPane;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JTabbedPane;
- public class main extends JFrame {
- public String IP;
- public static String data_ip;
- public static String local_ip = null;
- public String send_ip = null;
- public int send_port = 9000;
- public String msg;
- private JPanel contentPane;
- private JTextField textField;
- private final Action action = new SwingAction();
- public static String user;
- public static String password;
- public String mysqlback_friends = null;
- public SocketServer task = new SocketServer();
- public Thread t = new Thread(task); // 產生Thread物件
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- main frame = new main(user, password, data_ip,local_ip);
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void mysql_exc(String exc) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection(
- "jdbc:mysql://"+this.data_ip+"/messager?useUnicode=true&characterEncoding=UTF8", "root",
- "x123456789");
- Statement st = con.createStatement();
- ResultSet rs = st.getResultSet();
- st.execute(exc);
- rs = st.getResultSet();
- } catch (ClassNotFoundException o) {
- System.out.println("DriverClassNotFound :" + o.toString());
- } // 有可能會產生sqlexception
- catch (SQLException x) {
- System.out.println("Exception :" + x.toString());
- }
- }
- public main(String user, String password, String data_ip,String local_ip) {
- this.user = user;
- this.password = password;
- this.data_ip = data_ip;
- this.local_ip= local_ip;
- JMenuBar menuBar = new JMenuBar();
- JButton btnNewButton = new JButton("+");
- JPanel panel = new JPanel();
- JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
- String tmp2 = mysql_search("SELECT * FROM user where id='" + user + "' &&" + " password='" + password + "'","friends");
- JTree tree = new JTree();
- JScrollPane scrollPane = new JScrollPane();
- JMenu mnNewMenu = new JMenu("即時通");
- JMenuItem item = new JMenuItem("登出");
- mysql_exc("UPDATE user SET port = '"+send_port+"' where id='" + user + "' &&" + " password='" + password + "'");
- item.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- mysql_exc("UPDATE user SET online = '0' where id='" + user + "' &&" + " password='" + password + "'");
- System.exit(0);
- }
- });
- mysql_exc("UPDATE user SET ip = '"+local_ip+"' where id='" + user + "' &&" + " password='" + password + "'");
- setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- setBounds(100, 100, 249, 414);
- setJMenuBar(menuBar);
- menuBar.add(mnNewMenu);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- mnNewMenu.add(item);
- btnNewButton.setFont(new Font("微軟正黑體", Font.PLAIN, 9));
- textField = new JTextField();
- textField.setColumns(10);
- JScrollPane scrollPane_1 = new JScrollPane();
- JTree tree_1 = new JTree();
- tree_1.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree_1.getLastSelectedPathComponent();
- if (node == null)
- return;
- Object nodeInfo = node.getUserObject();// 通訊點入那個當client
- if (!nodeInfo.toString().equals("好友清單")) {
- IP = nodeInfo.toString();
- new SocketClient();
- }
- }
- });
- scrollPane_1.setViewportView(tree_1);
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- DefaultTreeModel model = (DefaultTreeModel) tree_1.getModel();
- DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
- if (!textField.getText().trim().equals("")) {
- root.add(new DefaultMutableTreeNode(textField.getText()));
- model.reload();
- }
- else {
- JOptionPane.showMessageDialog(null, "fuck dick");
- }
- }
- });
- GroupLayout gl_panel = new GroupLayout(panel);
- JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
- GroupLayout gl_contentPane = new GroupLayout(contentPane);
- gl_contentPane.setHorizontalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(7)
- .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(5)
- .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(5)
- .addComponent(btnNewButton)
- .addGap(5)
- .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addContainerGap(69, Short.MAX_VALUE))
- .addComponent(tabbedPane_1, GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
- );
- gl_contentPane.setVerticalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(5)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(6)
- .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(btnNewButton)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(8)
- .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addComponent(tabbedPane_1, GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
- );
- tabbedPane_1.addTab("New tab", null, scrollPane_1, null);
- tree_1.setModel(new DefaultTreeModel(
- new DefaultMutableTreeNode("廢物群") {
- {
- String tmp = tmp2;
- String[] tokens = tmp.split(",");
- for (String token : tokens) {
- add(new DefaultMutableTreeNode(token));
- }
- }
- }
- ));
- this.setVisible(true);
- contentPane.setLayout(gl_contentPane);
- t.start();
- }
- public String mysql_search(String exc,String label) {
- String tmp = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- // 註冊driver
- Connection con = DriverManager.getConnection(
- "jdbc:mysql://"+this.data_ip+"/messager?useUnicode=true&characterEncoding=UTF8", "root",
- "x123456789");
- // 取得connection
- Statement st = con.createStatement();
- st.execute(exc);
- ResultSet rs = st.getResultSet();
- while (rs.next()) {
- tmp = rs.getString(label);
- }
- if (tmp == null) {
- JOptionPane.showMessageDialog(null, "與伺服器取得資料失敗!");
- } else
- return tmp;
- } catch (ClassNotFoundException o) {
- System.out.println("DriverClassNotFound :" + o.toString());
- } // 有可能會產生sqlexception
- catch (SQLException x) {
- System.out.println("Exception :" + x.toString());
- }
- return tmp;
- }
- private class SwingAction extends AbstractAction {
- public SwingAction() {
- putValue(NAME, "SwingAction");
- putValue(SHORT_DESCRIPTION, "Some short description");
- }
- public void actionPerformed(ActionEvent e) {
- }
- }
- public class SocketServer extends java.lang.Thread {
- private boolean OutServer = false;
- private ServerSocket server;
- private final int ServerPort = 10000;// 要監控的port
- public SocketServer() {
- try {
- server = new ServerSocket(ServerPort);
- } catch (java.io.IOException e) {
- System.out.println("Socket啟動有問題 !");
- System.out.println("IOException :" + e.toString());
- }
- }
- public void run() {
- InputStreamReader inSR = null;
- OutputStreamWriter outSW = null;
- Socket socket;
- java.io.BufferedInputStream in;
- System.out.println("監聽伺服器已啟動 !");
- while (!OutServer) {
- socket = null;
- try {
- synchronized (server) {
- socket = server.accept();
- }
- System.out.println("取得連線 : InetAddress = " + socket.getInetAddress());
- // TimeOut時間
- socket.setSoTimeout(15000);
- inSR = new InputStreamReader(socket.getInputStream(), "UTF-8");
- BufferedReader br = new BufferedReader(inSR);
- in = new java.io.BufferedInputStream(socket.getInputStream());
- byte[] b = new byte[1024];
- String data = "";
- int length;
- while ((length = in.read(b)) > 0)// <=0的話就是結束了
- {
- data += new String(b, 0, length, "big5");// 注意編碼
- }
- String tmp = data;
- String[] tokens = data.split(":");
- //String tmp3 = mysql_search("SELECT * FROM user where id='" +Integer.valueOf(tokens[1])+ "'","port");
- //if(Integer.valueOf(tmp3 )>send_port)
- send_port=Integer.valueOf(Integer.valueOf(tokens[1]));
- send_ip = tokens[0];
- new_message(send_ip, send_port);
- String tmp2 = "確實收到資料" + send_ip + ":" + tokens[1];
- System.out.print(tmp2);
- in.close();
- in = null;
- socket.close();
- } catch (java.io.IOException e) {
- System.out.println("Socket連線有問題 !");
- System.out.println("IOException :" + e.toString());
- }
- }
- server = null;
- }
- public void stopThread() throws IOException {
- server.close();
- this.OutServer = true;
- }
- }
- public void new_message(String ip, int port) {
- msg test = new msg(ip,port);
- test.setTitle(ip);
- send_port++;
- }
- public class SocketClient extends java.lang.Thread {
- private String address = IP;// 連線的ip
- private int port = 10000;// 連線的port
- public SocketClient() {
- Socket client = new Socket();
- InetSocketAddress isa = new InetSocketAddress(this.address, this.port);
- try {
- client.connect(isa,10000);
- BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
- // 送出字串
- String tmp =local_ip+ ":" + String.valueOf(send_port);
- System.out.print("已向對方發送請求連接需求"+tmp);
- out.write(tmp.getBytes());
- new_message(IP, Integer.valueOf(send_port));
- out.flush();
- out.close();
- out = null;
- client.close();
- client = null;
- } catch (java.io.IOException e) {
- System.out.println("Socket連線有問題 !");
- System.out.println("IOException :" + e.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement