两个窗口交替发送一次就报错,说一个端口已经被绑定使用,如果一个窗口发,一个接就不会报错,怎么解决



```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.*;

public class TestFrame01 extends JFrame {
    JTextArea jta=new JTextArea();
  JTextField jtf=new JTextField(15);

    JPanel jp=new JPanel();
    JButton jb=new JButton("发送");
    DatagramSocket ds;
    DatagramPacket dp;
   String id;
   TestFrame01 tf=this;
    int port;
    public TestFrame01( String id,int port) throws IOException {
        this.port=port;
        this.id=id;
        init();
        operation();
        Receive();
    }
    public void init() throws UnknownHostException {
        this.setTitle("窗口1");
        this.setSize(500,500);
        jp.add(jtf);
        jp.add(jb);
        // 创建一个JScrollPane,并将JTextArea添加到其中
        JScrollPane scrollPane = new JScrollPane(jta);
        this.add(scrollPane, "Center");
        this.add(jp,"South");


        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public  void operation() throws IOException {

        //发送给自己和另一个窗口
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jb) {
                    byte[] bytes = jtf.getText().trim().getBytes();
                    try {
                        ds = new DatagramSocket();
                        dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(id), port);
                        ds.send(dp);
                        ds.close();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    jta.setEditable(false);
                    jta.setFont(new Font("宋体", Font.BOLD, 25));
                    jta.setForeground(Color.red);
                    jta.append("我:" + jtf.getText().trim());
                    jta.append("\n");
                }
            }
        });
    }
    public void Receive(){
        try {
            jta.setEditable(false);
            jta.setFont(new Font("宋体", Font.BOLD, 25));
            jta.setEditable(false);
            while (true) {
                byte[] bytes = new byte[1024];
                ds = new DatagramSocket(10087);
                dp = new DatagramPacket(bytes, bytes.length);
                ds.receive(dp);
                String message = new String(dp.getData(), 0, dp.getLength());
                jta.append("窗口2发送了信息:" + message);
                jta.append("\n");
                ds.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    public static void main(String[] args) throws IOException {
        new TestFrame01("127.0.0.1",10086);
    }
}

public class TestFrame02 extends JFrame{
    JTextArea jta=new JTextArea();
    JTextField jtf=new JTextField(15);
    JPanel jp=new JPanel();
    JButton jb=new JButton("发送");
    DatagramSocket ds;
    DatagramPacket dp;
    String sendID;
    int sendPORT;
    public TestFrame02( String sendID,int port) throws IOException {
        this.sendID=sendID;
        this.sendPORT=port;
        init();
        operation();
        Receive();
    }

    public void init() throws UnknownHostException {
        this.setTitle("窗口2");
        this.setSize(500,500);

        jp.add(jtf);
        jp.add(jb);
        // 创建一个JScrollPane,并将JTextArea添加到其中
        JScrollPane scrollPane = new JScrollPane(jta);
        this.add(scrollPane, "Center");

        this.add(jp,"South");
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public  void operation() throws IOException {

        //发送给自己和另一个窗口
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==jb){
                    byte[] bytes = jtf.getText().trim().getBytes();
                    try {
                        ds=new DatagramSocket();
                        dp=new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sendID),sendPORT);
                        ds.send(dp);
                        ds.close();
                    }  catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    jta.setFont(new Font("宋体",Font.BOLD,25));
                    jta.setForeground(Color.red);
                    jta.append("我:"+jtf.getText().trim());
                    jta.append("\n");
                }

            }
        });

    }

    public void Receive(){
        jta.setFont(new Font("宋体",Font.BOLD,25));
        jta.setForeground(Color.red);
        jta.setEditable(false);
        while (true) {
            try {
                ds=new DatagramSocket(10086);
                byte[]bytes=new byte[1024];
                dp=new DatagramPacket(bytes,bytes.length);
                ds.receive(dp);
                String message=new String(dp.getData(),0,dp.getLength()) ;

                jta.append("窗口1发送了信息:"+message);
                jta.append("\n");
                ds.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    public static void main(String[] args) throws IOException {
        new TestFrame02("127.0.0.1",10087);
    }
}


窗口1:ip本机 发送端口10086 接收端口10087     
窗口2:ip本机 发送端口10087 接收端口10086 


```

img

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/233459
  • 你也可以参考下这篇文章:封装一个接口方法,根据条件,调用不同接口数据
  • 除此之外, 这篇博客: 如何写一颗二叉树(递归)【菜鸟学习日记】中的 有一点要注意的是,因为我们遍历时要有根节点参数,但类外拿不到树的根节点,所以再写一些要用根节点的接口时,要有一个无参类型的函数,在类的内部再将根节点传参访问 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • //前序遍历
        void PrevOrder()
        {
            PrevOrder(_root);
        }
        //根->左->右
        void PrevOrder(Node* root)
        {
            //为空树,返回
            if (root == NULL)
            {
                return;
            }
            //不为空树
            cout << root->_data <<" ";
            PrevOrder(root->_left);
            PrevOrder(root->_right);
    
        }
        //中序
        void MidOrder()
        {
            MidOrder(_root);
        }
        //左->根->右
        void MidOrder(Node* root)
        {
            if (root == NULL)
            {
                return;
            }
            MidOrder(root->_left);
            cout << root->_data <<" ";
            MidOrder(root->_right);
    
        }
        //后序
        void PostOrder()
        {
            PostOrder(_root);
        }
        //左->右->根
        void PostOrder(Node* root)
        {
            if (root == NULL)
            {
                return;
            }
            PostOrder(root->_left);
            PostOrder(root->_right);
            cout << root->_data <<" ";
    
        }
    //测试一下
    void TestBinaryTree()
    {
        int arr[] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, '#', '#'};
        BinaryTree<int> t(arr, sizeof(arr) / sizeof(int), '#');
    
        cout << "前序:";
        t.PrevOrder();
        cout << endl;
    
        cout << "中序:";
        t.MidOrder();
        cout << endl;
    
        cout << "后序:";
        t.PostOrder();
        cout << endl;
    }

    这里写图片描述

  • 您还可以看一下 思寒老师的持续集成接口自动化测试从零基础到实战精通课程中的 实战-如何从零开始搞定接口测试(一)小节, 巩固相关知识点