0

In order to check if I can reach a particular TCP port, I use:

TcpStream::connect_timeout(&socket, Duration...

There is no clear equivalent with UdpSocket, so would like to know what would work similarly. The closest I could find is using the set_write_timeout, then doing a send or send_to, but these yield wrong results in that they show that the connection is successful, when the port is not even open on the other side.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • If it matters, I want to check if openvpn is running on remote, port 1194 – tshepang Jun 07 '18 at 21:44
  • 6
    This is not really specific to Rust. There is no good and reliable way to check whether a UDP port is open because UDP is a send-and-forget protocol. You also won't get confirmation that messages have actually been received, that's why all `send` seem to be successful. – mcarton Jun 07 '18 at 22:33
  • 1
    see also https://stackoverflow.com/questions/42539898/how-to-check-if-remote-udp-port-is-open-c – Zan Lynx Jun 08 '18 at 01:56
  • 1
    and https://stackoverflow.com/questions/10886228/checking-open-udp-port-in-c – Zan Lynx Jun 08 '18 at 01:57
  • Possible duplicate of [Checking open UDP Port in C++](https://stackoverflow.com/questions/10886228/checking-open-udp-port-in-c) – user207421 Jun 20 '18 at 03:12

1 Answers1

-3

Contrary to other answers and comments, there is a way. Connect to it with connect() and try a few sends. If the API is written correctly you should eventually get an error corresponding to the underlying ICMP UNREACHABLE message that is sent if the remote port isn't there.

user207421
  • 305,947
  • 44
  • 307
  • 483