简体   繁体   English

有没有办法检查端口是否在Android上以编程方式打开?

[英]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.

相关问题 以可移植的方式以编程方式打开文档 - Open a document programmatically in a portable way Android检查WebService是否在端口上可用 - Android Check if WebService is available on port Android - 强制搜索视图以编程方式打开 - Android - force searchview to open programmatically 是否可以在不连接IP端口的情况下检查IP端口是否打开? - Can I check if an IP port is open or not without connecting to the port? 需要一种以编程方式检查 Windows 服务状态的方法 - Need a way to check status of Windows service programmatically 如果在JVM上设置了-noverify,是否可以通过编程方式进行检查? - Is there any way to programmatically check, if `-noverify` is set on the JVM? 有没有一种方法可以通过编程方式使Tomcat停止监听http端口 - Is there a way to programmatically pause Tomcat from listening to http port 以编程方式打开设备密码更改 android - Open device password change programmatically android 如何以编程方式检查 Android 中互联网连接的可用性? - How to programmatically check availibilty of internet connection in Android? 如何检查 selenium 中是否已打开具有特定端口的 Chrome 实例 - How to check if there is a Chrome instance already open with a specific port in selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM