c# - How can I handle when a UdpClient is listening on an address which is no longer available? -
i'm using udpclient, bound provided end point, listen incoming packets. code follows:
... string myipaddress = "192.168.1.100"; int myport = 50000; ipendpoint selfendpoint = new ipendpoint(ipaddress.parse(myipaddress), myport); ipendpoint anysourceendpoint = new ipendpoint(ipaddress.any, 0); udpclient receivingclient = new udpclient(); receivingclient.client.bind(selfendpoint); // can throw. receivingclient.receive(ref anysourceendpoint); ...
if interface ip address '192.168.1.100' not exist, throws socketexception` (as expected). in case such interface exists, udpclient binds , waits incoming packet. problem follows.
how can detect case blocking on receive
, interface's address no longer available?
for example, application running , waiting incoming packets. user disables network interface on system. application still assumes can receive packets, though cannot.
the closes i've come setting receivetimeout
on client , re-creating/binding udpclient when timeout occurs; i'm hoping alternative or other suggestions.
udp stateless. rule of thumb is: cannot rely on anything. not on sending, not on receiving, not on getting errors when goes wrong.
udp sits quietly , waits next packet. binding endpoint, merely tell discard not sent explicitly endpoint. if user disables network card, still waits packets sent bound endpoint, though became impossible packets arrive. , activate card again, , sends endpoint, udp client receive it, if nothing happened.
long story short: udpclient
not choice of software find out if network card disabled. have find out other way , disable udpclient
manually, if want handle case. instance, initialize tcp on other port. in fact produce error if disable network card.
Comments
Post a Comment