-
大小: 1.93MB文件類(lèi)型: .zip金幣: 2下載: 0 次發(fā)布日期: 2023-11-18
- 語(yǔ)言: Java
- 標(biāo)簽:
資源簡(jiǎn)介
建立一個(gè)局域網(wǎng)內(nèi)的簡(jiǎn)單的P2P消息系統(tǒng),程序既是服務(wù)器又是客戶(hù),服務(wù)器端口(自擬服務(wù)器端口號(hào)并選定)。

代碼片段和文件信息
package?yyj.main;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.io.BufferedReader;
import?java.io.InputStreamReader;
import?java.io.PrintWriter;
import?java.net.Socket;
import?java.util.StringTokenizer;
import?java.util.concurrent.ConcurrentHashMap;
import?javax.swing.JOptionPane;
import?javax.swing.event.ListSelectionEvent;
import?javax.swing.event.ListSelectionListener;
import?yyj.model.User;
import?yyj.view.*;
public?class?ClientMain?extends?ClientView{
//model
private?User?me;?
//?所有在線(xiàn)用戶(hù)
????private?ConcurrentHashMap?onlineUsers?=?new?ConcurrentHashMap();??
????private?String?sendTarget?=?“ALL“;??//默認(rèn)發(fā)送對(duì)象
??
????//Socket??
????private?Socket?socket;??
????private?PrintWriter?writer;????//輸出流
????private?BufferedReader?reader;?//輸入流?
??
????//?負(fù)責(zé)接收消息的線(xiàn)程??
????private?MessageThread?messageThread;??
??
????//Status??
????private?boolean?isConnected;???//判斷是否連接到服務(wù)端
????
????//構(gòu)造函數(shù)
????public?ClientMain()?{
????
????//?寫(xiě)消息的文本框中按回車(chē)鍵時(shí)事件
????messageTextField.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????send();??
????????}??
????});
????
????//?單擊發(fā)送按鈕時(shí)事件
????sendButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????send();??
????????}??
????});??
????//?單擊連接按鈕時(shí)事件
????connectButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????if?(!isConnected)?{??
????????????????connect();??
????????????}??
????????}??
????});??
????//?單擊斷開(kāi)按鈕時(shí)事件
????disconnectButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????if?(isConnected)?{??
????????????????disconnect();??
????????????}??
????????}??
????});??
????//?關(guān)閉窗口時(shí)事件
????frame.addWindowListener(new?WindowAdapter()?{??
????????public?void?windowClosing(WindowEvent?e)?{??
????????????if?(isConnected)?{??
????????????????disconnect();??
????????????}??
????????????System.exit(0);??
????????}??
????});??
????//?為在線(xiàn)用戶(hù)添加點(diǎn)擊事件
????userList.addListSelectionListener(new?ListSelectionListener()?{?
????
????????public?void?valueChanged(ListSelectionEvent?e)?{??
????????????int?index?=?userList.getSelectedIndex();??//獲取被點(diǎn)擊的用戶(hù)的序號(hào)
????????????if?(index?0)?return;??
?
????????????if?(index?==?0)?{??//默認(rèn)為所有人
????????????????sendTarget?=?“ALL“;??
????????????????messageToLabel.setText(“To:?所有人“);??
????????????}?else?{??
????????????????String?name?=?(String)listModel.getElementAt(index);??//獲取被點(diǎn)擊用戶(hù)的名字
????????????????if?(onlineUsers.containsKey(name))?{??
????????????????????sendTarget?=?onlineUsers.get(name).description();??
????????????????????messageToLabel.setText(“To:?“?+?name);??//將To..標(biāo)簽改為T(mén)o?用戶(hù)名
????????????????}?else?{??
????????????????????sendTarget?=?“ALL“;??
????????????????????messageToLabel.setText(“To:?所有人“);??
??????
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\
?????文件?????????295??2017-07-10?09:32??P2P_chater-master\P2P_Chater\.classpath
?????文件?????????369??2017-07-10?09:32??P2P_chater-master\P2P_Chater\.project
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\.settings\
?????文件?????????587??2017-07-10?09:32??P2P_chater-master\P2P_Chater\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\
?????文件?????????671??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$1.class
?????文件?????????671??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$2.class
?????文件?????????759??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$3.class
?????文件?????????758??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$4.class
?????文件?????????785??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$5.class
?????文件????????1939??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$6.class
?????文件????????4123??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain$MessageThread.class
?????文件????????7209??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ClientMain.class
?????文件?????????671??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$1.class
?????文件?????????671??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$2.class
?????文件?????????758??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$3.class
?????文件?????????758??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$4.class
?????文件?????????785??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$5.class
?????文件????????5248??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$ClientServiceThread.class
?????文件????????2896??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain$ServerThread.class
?????文件????????6313??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\main\ServerMain.class
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\model\
?????文件????????1148??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\model\User.class
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\view\
?????文件????????3738??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\view\ClientView.class
?????文件????????3431??2017-07-10?09:32??P2P_chater-master\P2P_Chater\bin\yyj\view\ServerView.class
?????目錄???????????0??2017-07-10?09:32??P2P_chater-master\P2P_Chater\src\
............此處省略15個(gè)文件信息
評(píng)論
共有 條評(píng)論