-
大小: 1.93MB文件類型: .zip金幣: 2下載: 0 次發布日期: 2023-11-18
- 語言: Java
- 標簽:
資源簡介
建立一個局域網內的簡單的P2P消息系統,程序既是服務器又是客戶,服務器端口(自擬服務器端口號并選定)。

代碼片段和文件信息
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;?
//?所有在線用戶
????private?ConcurrentHashMap?onlineUsers?=?new?ConcurrentHashMap();??
????private?String?sendTarget?=?“ALL“;??//默認發送對象
??
????//Socket??
????private?Socket?socket;??
????private?PrintWriter?writer;????//輸出流
????private?BufferedReader?reader;?//輸入流?
??
????//?負責接收消息的線程??
????private?MessageThread?messageThread;??
??
????//Status??
????private?boolean?isConnected;???//判斷是否連接到服務端
????
????//構造函數
????public?ClientMain()?{
????
????//?寫消息的文本框中按回車鍵時事件
????messageTextField.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????send();??
????????}??
????});
????
????//?單擊發送按鈕時事件
????sendButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????send();??
????????}??
????});??
????//?單擊連接按鈕時事件
????connectButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????if?(!isConnected)?{??
????????????????connect();??
????????????}??
????????}??
????});??
????//?單擊斷開按鈕時事件
????disconnectButton.addActionListener(new?ActionListener()?{??
????????public?void?actionPerformed(ActionEvent?e)?{??
????????????if?(isConnected)?{??
????????????????disconnect();??
????????????}??
????????}??
????});??
????//?關閉窗口時事件
????frame.addWindowListener(new?WindowAdapter()?{??
????????public?void?windowClosing(WindowEvent?e)?{??
????????????if?(isConnected)?{??
????????????????disconnect();??
????????????}??
????????????System.exit(0);??
????????}??
????});??
????//?為在線用戶添加點擊事件
????userList.addListSelectionListener(new?ListSelectionListener()?{?
????
????????public?void?valueChanged(ListSelectionEvent?e)?{??
????????????int?index?=?userList.getSelectedIndex();??//獲取被點擊的用戶的序號
????????????if?(index?0)?return;??
?
????????????if?(index?==?0)?{??//默認為所有人
????????????????sendTarget?=?“ALL“;??
????????????????messageToLabel.setText(“To:?所有人“);??
????????????}?else?{??
????????????????String?name?=?(String)listModel.getElementAt(index);??//獲取被點擊用戶的名字
????????????????if?(onlineUsers.containsKey(name))?{??
????????????????????sendTarget?=?onlineUsers.get(name).description();??
????????????????????messageToLabel.setText(“To:?“?+?name);??//將To..標簽改為To?用戶名
????????????????}?else?{??
????????????????????sendTarget?=?“ALL“;??
????????????????????messageToLabel.setText(“To:?所有人“);??
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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個文件信息
評論
共有 條評論