UDPClient in C# -
i use in udpclient in c#. invoke receive function, when running app. program enter eternity loop. why phenomenon? maybe because no data available on port? can do?
i write following code:
udpclient udpclient = new udpclient(623); try { udpclient.connect("10.0.0.16", 623); // sends message host have connected. byte[] sendbytes = encoding.ascii.getbytes("is there?"); udpclient.send(sendbytes, sendbytes.length); // sends message different host using optional hostname , port parameters. udpclient udpclientb = new udpclient(); udpclientb.send(sendbytes, sendbytes.length, "10.0.0.16", 623); //ipendpoint object allow read datagrams sent source. ipendpoint remoteipendpoint = new ipendpoint(ipaddress.any, 0); // blocks until message returns on socket remote host. byte[] receivebytes = udpclient.receive(ref remoteipendpoint); string returndata = encoding.ascii.getstring(receivebytes); // uses ipendpoint object determine of these 2 hosts responded. console.writeline("this message received " + returndata.tostring()); console.writeline("this message sent " + remoteipendpoint.address.tostring() + " on port number " + remoteipendpoint.port.tostring()); udpclient.close(); udpclientb.close(); } catch (exception e) { console.writeline(e.tostring()); }
thanks
i suspect it's because of no data, test this, try implementing 'beginrecieve' instead of recieve. msdn has example:
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx
Comments
Post a Comment