[英]Is there a way to check if a port is open on android programmatically?
I have already looked at some of the answers on SO but to no avail. 我已经看过关于SO的一些答案,但无济于事。 How can I check if a port is open already, close it if open already and then open a socket on that port from Java code? 如何检查端口是否已经打开,如果已经打开则将其关闭,然后从Java代码打开该端口上的套接字?
Details: I want to open port number 5000 and bind it to a udp socket. 详细信息:我想打开端口号5000并将其绑定到udp套接字。 However, every time I open the socket on that port, I get an exception "Address already in use". 但是,每次打开该端口上的套接字时,都会出现一个异常“地址已在使用中”。 How can I make sure the port is available first? 如何确保端口首先可用? Overridind onDestroy() to close the socket when terminating the application does not seem to do it. 在终止应用程序似乎没有执行此操作时,重写OverDefined onDestroy()以关闭套接字。
I have already looked at some of the answers on SO but to no avail. 我已经看过关于SO的一些答案,但无济于事。
Hard to believe. 难以置信。
How can I check if a port is open already 如何检查端口是否已打开
Try to bind to it as below. 尝试如下绑定。 If you get a BindException, it's in use. 如果您收到BindException,则它正在使用中。
close it if open already 如果已经打开,请关闭它
You can't. 你不能
and then open a socket on that port from Java code? 然后从Java代码打开该端口上的套接字?
DatagramSocket ds = new DatagramSocket(port);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.