我正在为一个小型聊天应用程序编写一个Java代码作为网络课程作业 . 它应该只是一个类(p2p);不是(客户/服务器) . 每个对等体都应该连接到聊天并发送/接收所有对等体必须看到的消息 . 我做了代码,它在两台机器之间工作,因为我在每台机器的LAN中明确指定了IP地址,并从每台机器获得响应 . 我需要的是:1 - 一旦点击CONNECT按钮,将每个对等体连接到套接字 . 2 - 检测所有连接的对等体并将它们放入列表(数组)中 . 3 - 将所有消息发送给所有连接的对等体,因此每个人都可以将所有信

任何帮助将不胜感激 . 代码 :

import javax.swing . *;

import java.awt.event.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException; 
import javax.swing.JFrame;
import java.util.ArrayList;

class GUIExample extends JFrame implements ActionListener{  
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JButton b1;  
    JButton b2;  
    JLabel Name;
    JLabel port;
    JLabel HostName;
    JLabel Participants;
    JLabel Messages;
    JTextArea area;
    JTextPane pane;
    JTextPane pane1;
    JTextArea area1;
    JTextPane pane2;
    JTextArea area2;
    JTextField name;
    String s = "hi";
    JTextField p;
    JTextField IP;
    ArrayList<String> list = new ArrayList<String>();


    GUIExample() throws Exception { 

         InetAddress addr = InetAddress.getLocalHost();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        Name = new JLabel ("NAME");
        Name.setBounds(10, 10, 100, 20);
        add(Name);

        name = new JTextField();
        name.setBounds(100, 10, 200, 20);
        add(name);

        HostName=new JLabel("HostName");
        HostName.setBounds(10, 50, 100, 20);
        add(HostName);

        IP = new JTextField(addr.getHostAddress());
        IP.setBounds(100, 50, 100, 20);
        add(IP);

        port = new JLabel("PORT");
        port.setBounds(220, 50, 100, 20);
        add(port);

        p = new JTextField();
        p.setBounds(280, 50, 100, 20);
        add(p);

        Participants = new JLabel("Participants");
        Participants.setBounds(10, 100, 100, 20);
        add(Participants);

        area=new JTextArea(300,300);  
        area.setBounds(10,150,200,300);  
        add(area);

        Messages = new JLabel ("Messages");
        Messages.setBounds(300, 100, 100, 20);
        add(Messages);

        area1=new JTextArea(200,300);  
        area1.setBounds(300,150,300,300);  
        add(area1);

        area2 = new JTextArea(100,100);
        area2.setBounds(10, 500, 500, 50);
        add(area2);

        b1=new JButton("connect");  
        b1.setBounds(600,50,80,30);  
        b1.addActionListener(this);  
        add(b1); 

        b2=new JButton("Send");  
        b2.setBounds(600,500,80,30);  
        b2.addActionListener(this);  
        add(b1);  
        add(b2);  

        setSize(750,575);  
        setLayout(null);  
        setVisible(true);  



    }



    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1)
        {
            if(name.getText().equals("") && IP.getText().equals(null) && !(p.getText().equals("11222"))){
                area1.append(" name or hostname or port fields are empty or invalid try again ");
            }
            else {
                area.append(name.getText());
                area1.append(name.getText()+ " is connected now \n");
                list.add(name.getText());
                list.add(IP.getText());
                list.add(p.getText());


            }
        }


        if(e.getSource() == b2)
        {
            BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader
                     (System.in));
            DatagramSocket clientSocket = null;
            try {
                clientSocket = new DatagramSocket();
            } catch (SocketException e1) {
                System.out.println(" There is problem with Socek ");
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            InetAddress IPAddress = null;
            try {
                byte[] ipp = new byte[]{(byte)192,(byte)168,1,48};
                IPAddress = InetAddress.getByAddress(ipp);
            } catch (UnknownHostException e1) {
                System.out.println(" There is problem with IP ");
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            byte[] sendData = new byte[1024];
            String sentence = inFromUser.readLine();
            area2.setText(sentence);
            sendData = sentence.getBytes();
            DatagramPacket sendPacket =
               new DatagramPacket(sendData, sendData.length, 
                            IPAddress, 11222);
            try {
                clientSocket.send(sendPacket);
            } catch (IOException e1) {
                System.out.println("There is problem with Sending the packet");
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            if(name.getText()!= null && area2.getText()!= null){
            area1.append(name.getText()+" : "+sentence+"\n");
            area2.setText(null);
            }
        }
    }  
    public static void main(String args[]) throws Exception{
        Runnable task = () -> {         
        DatagramSocket serverSocket = null;
        try {
            serverSocket = new DatagramSocket(11222);
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];

            while(true)
              {
                 DatagramPacket receivePacket =
                    new DatagramPacket(receiveData, 
                             receiveData.length);
                 try {
                    serverSocket.receive(receivePacket);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                 String sentence = new String(
                             receivePacket.getData());
                 InetAddress IPAddress =
                             receivePacket.getAddress();
                 int port = receivePacket.getPort();
                     System.out.println("FROM Client:" +
                     sentence);
                 sendData = sentence.getBytes();
                 DatagramPacket sendPacket =
                 new DatagramPacket(sendData,
                          sendData.length, 
                             IPAddress, port);
                 try {
                    serverSocket.send(sendPacket);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
            };
            Thread thread = new Thread(task);
            thread.start();
        new GUIExample();  
    }

    }  


 is to detect all