如何确保我想要使用的端口始终可用(即未使用)?
有没有办法强制释放端口?端口通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
while(running)
while(running)
不,因为如果两个程序都要求释放端口会发生什么?最终第二个想要端口的程序一定是不满意的。
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.