如何确保我想要使用的端口始终可用(即未使用)?

发布于 2025-01-05 04:43:20 字数 781 浏览 6 评论 0原文

有没有办法强制释放端口?端口通过args[]传递。 我正在尝试构建一个等待来自多个设备的 UDP 数据包的服务器应用程序,因此我不想确保它始终侦听适当的端口。数据包中的数据将被处理,结果将写入数据库。我正在使用 while(true) 循环来保持它监听新数据包。当多个设备(例如 2000 个)同时发送时,是否可能会出现并发问题?除了使用 while(true) 之外还有其他方法吗?有什么建议吗?

编辑(可能与主题不正确) 我使用这些方法来转换 2 字节和 4 字节长度的有符号二进制补码。我找不到更简单的方法...

public static Long twosComp16(String str) throws java.lang.Exception
    {
        Long num = Long.valueOf(str, 16);

        int fix = 65536;

        if (num > (fix/2))
            return num - fix;
        else return num;
    }
    public static Long twosComp32(String str) throws java.lang.Exception
    {
        Long num = Long.valueOf(str, 16);

        long fix = 4294967296L;

        if (num > (fix/2))
            return num - fix;
        else return num;
    }

Is there a way to force-free ports? The port is passed through args[].
I'm trying to build a server app that waits for UDP packets from multiple devices, so I wan't to make sure that it will always listen to the appropriate port. The data from the packets is to be processed and the results to be written in a DB. I'm using a while(true) loop to keep it listening for new packets. Is it possible that I might have concurrency issues when multiple devices (say 2000) send at once? Is there any other way than using while(true)? Any suggestions?

EDIT (it may not be right on topic)
I'm using these methods to convert signed twos complements of 2 bytes and 4 bytes length. I couldn't find a more simple way...

public static Long twosComp16(String str) throws java.lang.Exception
    {
        Long num = Long.valueOf(str, 16);

        int fix = 65536;

        if (num > (fix/2))
            return num - fix;
        else return num;
    }
    public static Long twosComp32(String str) throws java.lang.Exception
    {
        Long num = Long.valueOf(str, 16);

        long fix = 4294967296L;

        if (num > (fix/2))
            return num - fix;
        else return num;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2025-01-12 04:43:20
  1. 我不认为你可以强制释放端口。无论如何,这取决于用户。尽量不替用户做选择;而是问他/她。是什么让您认为您的应用程序比该端口上运行的其他应用程序更重要? ;)
  2. while(running)
  3. 如果您要进行繁重的处理,请让您的服务器为每个客户端生成一个线程,而不是只有一个处理而其他则等待。
  1. I don't think you can force free a port. And anyway, that's up to the user. Try to not make choices for the user; ask him/her instead. What makes you think your app is more important than the other one running on that port? ;)
  2. while(running)
  3. If you're going to have heavy processing, make your server spawn a thread for each client instead of having only one processing and the others waiting.
-黛色若梦 2025-01-12 04:43:20

有没有办法强制释放端口

不,因为如果两个程序都要求释放端口会发生什么?最终第二个想要端口的程序一定是不满意的。

Is there a way to force-free ports

No, because what would happen if two programs both demanded a port to be free? Ultimately the second program to want the port must be unsatisfied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文