人生我靠演戏 2023-07-02 15:31 采纳率: 91.2%
浏览 19
已结题

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



```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 


```

  • 写回答

2条回答 默认 最新

  • 人生我靠演戏 2023-07-02 16:25
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月10日
  • 已采纳回答 7月2日
  • 创建了问题 7月2日

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费