有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用java代码/补丁释放(或重置)服务器端口

我在做一个Java项目。我正在使用JADE服务器进行监听 连接端口并接收数据。另外,我正在使用MATLAB终端运行 将输出发送到java控制台的脚本。我需要运行这个MATLAB 多次编写脚本

但对于每次迭代,我都必须重置服务器端口。不做 因此,它抛出了一个绑定socket错误

我想知道,有没有一种方法(java代码/补丁)可以释放和重置 每次运行程序后使用的端口(socket端口)?在eclipse中,我 点击控制台上的“红色”按钮可以释放一个端口。然而,我的 项目应该独立于此类点击


共 (1) 个答案

  1. # 1 楼答案

    插座setReuseAddress(true);

    Enable/disable the SO_REUSEADDR socket option. When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed (typically known as the TIME_WAIT state or 2MSL wait state). For applications using a well known socket address or port it may not be possible to bind a socket to the required SocketAddress if there is a connection in the timeout state involving the socket address or port.

    Enabling SO_REUSEADDR prior to binding the socket using bind(SocketAddress) allows the socket to be bound even though a previous connection is in a timeout state.

    When a Socket is created the initial setting of SO_REUSEADDR is disabled.

    The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See isBound()) is not defined.