Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. package messager;
  2.  
  3. import java.awt.EventQueue;
  4. import javax.swing.JOptionPane;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JTree;
  10. import javax.swing.GroupLayout;
  11. import javax.swing.GroupLayout.Alignment;
  12. import javax.swing.JButton;
  13. import javax.swing.LayoutStyle.ComponentPlacement;
  14. import javax.swing.JTextField;
  15. import java.awt.Font;
  16. import javax.swing.JMenuBar;
  17. import javax.swing.JMenuItem;
  18. import javax.swing.JMenu;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.ActionEvent;
  21. import javax.swing.AbstractAction;
  22. import javax.swing.Action;
  23. import javax.swing.tree.DefaultTreeModel;
  24.  
  25. import javax.swing.tree.DefaultMutableTreeNode;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import javax.swing.JScrollPane;
  29.  
  30. import java.io.BufferedOutputStream;
  31. import java.io.BufferedReader;
  32. import java.io.IOException;
  33. import java.io.InputStreamReader;
  34. import java.io.OutputStreamWriter;
  35.  
  36. import java.net.InetSocketAddress;
  37. import java.net.ServerSocket;
  38. import java.net.Socket;
  39. import java.sql.Connection;
  40. import java.sql.DriverManager;
  41. import java.sql.ResultSet;
  42. import java.sql.SQLException;
  43. import java.sql.Statement;
  44. import javax.swing.JTabbedPane;
  45.  
  46. public class main extends JFrame {
  47.  
  48. public String IP;
  49. public static String data_ip;
  50. public static String local_ip = null;
  51. public String send_ip = null;
  52. public int send_port = 9000;
  53. public String msg;
  54. private JPanel contentPane;
  55. private JTextField textField;
  56. private final Action action = new SwingAction();
  57. public static String user;
  58. public static String password;
  59. public String mysqlback_friends = null;
  60. public SocketServer task = new SocketServer();
  61. public Thread t = new Thread(task); // 產生Thread物件
  62.  
  63. /**
  64. * Launch the application.
  65. */
  66. public static void main(String[] args) {
  67. EventQueue.invokeLater(new Runnable() {
  68. public void run() {
  69. try {
  70.  
  71. main frame = new main(user, password, data_ip,local_ip);
  72. frame.setVisible(true);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. });
  78.  
  79. }
  80.  
  81. public void mysql_exc(String exc) {
  82. try {
  83. Class.forName("com.mysql.jdbc.Driver");
  84. Connection con = DriverManager.getConnection(
  85. "jdbc:mysql://"+this.data_ip+"/messager?useUnicode=true&characterEncoding=UTF8", "root",
  86. "x123456789");
  87. Statement st = con.createStatement();
  88. ResultSet rs = st.getResultSet();
  89. st.execute(exc);
  90. rs = st.getResultSet();
  91. } catch (ClassNotFoundException o) {
  92. System.out.println("DriverClassNotFound :" + o.toString());
  93. } // 有可能會產生sqlexception
  94. catch (SQLException x) {
  95. System.out.println("Exception :" + x.toString());
  96. }
  97. }
  98.  
  99. public main(String user, String password, String data_ip,String local_ip) {
  100. this.user = user;
  101. this.password = password;
  102. this.data_ip = data_ip;
  103. this.local_ip= local_ip;
  104. JMenuBar menuBar = new JMenuBar();
  105. JButton btnNewButton = new JButton("+");
  106. JPanel panel = new JPanel();
  107. JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
  108. String tmp2 = mysql_search("SELECT * FROM user where id='" + user + "' &&" + " password='" + password + "'","friends");
  109. JTree tree = new JTree();
  110. JScrollPane scrollPane = new JScrollPane();
  111. JMenu mnNewMenu = new JMenu("即時通");
  112. JMenuItem item = new JMenuItem("登出");
  113. mysql_exc("UPDATE user SET port = '"+send_port+"' where id='" + user + "' &&" + " password='" + password + "'");
  114.  
  115.  
  116. item.addActionListener(new ActionListener() {
  117. public void actionPerformed(ActionEvent e) {
  118. mysql_exc("UPDATE user SET online = '0' where id='" + user + "' &&" + " password='" + password + "'");
  119. System.exit(0);
  120. }
  121. });
  122. mysql_exc("UPDATE user SET ip = '"+local_ip+"' where id='" + user + "' &&" + " password='" + password + "'");
  123. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  124. setBounds(100, 100, 249, 414);
  125.  
  126.  
  127. setJMenuBar(menuBar);
  128.  
  129. menuBar.add(mnNewMenu);
  130. contentPane = new JPanel();
  131. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  132. setContentPane(contentPane);
  133.  
  134. mnNewMenu.add(item);
  135.  
  136.  
  137.  
  138. btnNewButton.setFont(new Font("微軟正黑體", Font.PLAIN, 9));
  139.  
  140. textField = new JTextField();
  141. textField.setColumns(10);
  142.  
  143.  
  144. JScrollPane scrollPane_1 = new JScrollPane();
  145.  
  146.  
  147. JTree tree_1 = new JTree();
  148. tree_1.addMouseListener(new MouseAdapter() {
  149. @Override
  150. public void mouseClicked(MouseEvent e) {
  151.  
  152. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree_1.getLastSelectedPathComponent();
  153. if (node == null)
  154. return;
  155. Object nodeInfo = node.getUserObject();// 通訊點入那個當client
  156. if (!nodeInfo.toString().equals("好友清單")) {
  157. IP = nodeInfo.toString();
  158. new SocketClient();
  159.  
  160. }
  161. }
  162. });
  163.  
  164. scrollPane_1.setViewportView(tree_1);
  165. btnNewButton.addActionListener(new ActionListener() {
  166. public void actionPerformed(ActionEvent arg0) {
  167. DefaultTreeModel model = (DefaultTreeModel) tree_1.getModel();
  168. DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
  169. if (!textField.getText().trim().equals("")) {
  170. root.add(new DefaultMutableTreeNode(textField.getText()));
  171. model.reload();
  172. }
  173.  
  174. else {
  175. JOptionPane.showMessageDialog(null, "fuck dick");
  176. }
  177.  
  178. }
  179. });
  180. GroupLayout gl_panel = new GroupLayout(panel);
  181.  
  182. JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
  183. GroupLayout gl_contentPane = new GroupLayout(contentPane);
  184. gl_contentPane.setHorizontalGroup(
  185. gl_contentPane.createParallelGroup(Alignment.LEADING)
  186. .addGroup(gl_contentPane.createSequentialGroup()
  187. .addGap(7)
  188. .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  189. .addGap(5)
  190. .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  191. .addGap(5)
  192. .addComponent(btnNewButton)
  193. .addGap(5)
  194. .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  195. .addContainerGap(69, Short.MAX_VALUE))
  196. .addComponent(tabbedPane_1, GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
  197. );
  198. gl_contentPane.setVerticalGroup(
  199. gl_contentPane.createParallelGroup(Alignment.LEADING)
  200. .addGroup(gl_contentPane.createSequentialGroup()
  201. .addGap(5)
  202. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  203. .addGroup(gl_contentPane.createSequentialGroup()
  204. .addGap(6)
  205. .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  206. .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  207. .addComponent(btnNewButton)
  208. .addGroup(gl_contentPane.createSequentialGroup()
  209. .addGap(8)
  210. .addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
  211. .addPreferredGap(ComponentPlacement.UNRELATED)
  212. .addComponent(tabbedPane_1, GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
  213. );
  214. tabbedPane_1.addTab("New tab", null, scrollPane_1, null);
  215.  
  216. tree_1.setModel(new DefaultTreeModel(
  217. new DefaultMutableTreeNode("廢物群") {
  218. {
  219. String tmp = tmp2;
  220. String[] tokens = tmp.split(",");
  221. for (String token : tokens) {
  222. add(new DefaultMutableTreeNode(token));
  223.  
  224. }
  225. }
  226. }
  227. ));
  228. this.setVisible(true);
  229. contentPane.setLayout(gl_contentPane);
  230. t.start();
  231. }
  232.  
  233. public String mysql_search(String exc,String label) {
  234. String tmp = null;
  235.  
  236. try {
  237. Class.forName("com.mysql.jdbc.Driver");
  238. // 註冊driver
  239. Connection con = DriverManager.getConnection(
  240. "jdbc:mysql://"+this.data_ip+"/messager?useUnicode=true&characterEncoding=UTF8", "root",
  241. "x123456789");
  242. // 取得connection
  243. Statement st = con.createStatement();
  244. st.execute(exc);
  245. ResultSet rs = st.getResultSet();
  246. while (rs.next()) {
  247. tmp = rs.getString(label);
  248.  
  249. }
  250.  
  251. if (tmp == null) {
  252. JOptionPane.showMessageDialog(null, "與伺服器取得資料失敗!");
  253. } else
  254. return tmp;
  255. } catch (ClassNotFoundException o) {
  256. System.out.println("DriverClassNotFound :" + o.toString());
  257. } // 有可能會產生sqlexception
  258. catch (SQLException x) {
  259.  
  260. System.out.println("Exception :" + x.toString());
  261. }
  262. return tmp;
  263. }
  264.  
  265. private class SwingAction extends AbstractAction {
  266. public SwingAction() {
  267. putValue(NAME, "SwingAction");
  268. putValue(SHORT_DESCRIPTION, "Some short description");
  269. }
  270.  
  271. public void actionPerformed(ActionEvent e) {
  272. }
  273. }
  274.  
  275. public class SocketServer extends java.lang.Thread {
  276.  
  277. private boolean OutServer = false;
  278. private ServerSocket server;
  279. private final int ServerPort = 10000;// 要監控的port
  280.  
  281. public SocketServer() {
  282. try {
  283.  
  284. server = new ServerSocket(ServerPort);
  285.  
  286. } catch (java.io.IOException e) {
  287. System.out.println("Socket啟動有問題 !");
  288. System.out.println("IOException :" + e.toString());
  289. }
  290. }
  291.  
  292. public void run() {
  293. InputStreamReader inSR = null;
  294. OutputStreamWriter outSW = null;
  295. Socket socket;
  296. java.io.BufferedInputStream in;
  297.  
  298. System.out.println("監聽伺服器已啟動 !");
  299. while (!OutServer) {
  300. socket = null;
  301. try {
  302.  
  303. synchronized (server) {
  304.  
  305. socket = server.accept();
  306. }
  307. System.out.println("取得連線 : InetAddress = " + socket.getInetAddress());
  308. // TimeOut時間
  309. socket.setSoTimeout(15000);
  310. inSR = new InputStreamReader(socket.getInputStream(), "UTF-8");
  311. BufferedReader br = new BufferedReader(inSR);
  312.  
  313. in = new java.io.BufferedInputStream(socket.getInputStream());
  314. byte[] b = new byte[1024];
  315. String data = "";
  316. int length;
  317. while ((length = in.read(b)) > 0)// <=0的話就是結束了
  318. {
  319. data += new String(b, 0, length, "big5");// 注意編碼
  320. }
  321. String tmp = data;
  322. String[] tokens = data.split(":");
  323. //String tmp3 = mysql_search("SELECT * FROM user where id='" +Integer.valueOf(tokens[1])+ "'","port");
  324. //if(Integer.valueOf(tmp3 )>send_port)
  325. send_port=Integer.valueOf(Integer.valueOf(tokens[1]));
  326. send_ip = tokens[0];
  327.  
  328.  
  329. new_message(send_ip, send_port);
  330. String tmp2 = "確實收到資料" + send_ip + ":" + tokens[1];
  331. System.out.print(tmp2);
  332.  
  333. in.close();
  334. in = null;
  335. socket.close();
  336.  
  337.  
  338. } catch (java.io.IOException e) {
  339. System.out.println("Socket連線有問題 !");
  340.  
  341. System.out.println("IOException :" + e.toString());
  342.  
  343. }
  344.  
  345. }
  346. server = null;
  347. }
  348.  
  349. public void stopThread() throws IOException {
  350. server.close();
  351. this.OutServer = true;
  352. }
  353. }
  354.  
  355. public void new_message(String ip, int port) {
  356. msg test = new msg(ip,port);
  357. test.setTitle(ip);
  358. send_port++;
  359. }
  360.  
  361. public class SocketClient extends java.lang.Thread {
  362.  
  363. private String address = IP;// 連線的ip
  364. private int port = 10000;// 連線的port
  365.  
  366. public SocketClient() {
  367.  
  368. Socket client = new Socket();
  369.  
  370. InetSocketAddress isa = new InetSocketAddress(this.address, this.port);
  371. try {
  372. client.connect(isa,10000);
  373. BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
  374. // 送出字串
  375.  
  376.  
  377. String tmp =local_ip+ ":" + String.valueOf(send_port);
  378. System.out.print("已向對方發送請求連接需求"+tmp);
  379. out.write(tmp.getBytes());
  380. new_message(IP, Integer.valueOf(send_port));
  381. out.flush();
  382. out.close();
  383. out = null;
  384. client.close();
  385. client = null;
  386.  
  387.  
  388. } catch (java.io.IOException e) {
  389. System.out.println("Socket連線有問題 !");
  390. System.out.println("IOException :" + e.toString());
  391. }
  392. }
  393.  
  394. }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement