• 大小: 1.93MB
    文件类型: .zip
    金币: 1
    下载: 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个文件信息

评论

共有 条评论