c# - UDP client assigns free port too late -



c# - UDP client assigns free port too late -

in c#, .net 4, have need send udp message on port , hear responses on same port.

i using fixed port, client ran issue that, want utilize available port. seems can using 0 port number, isn't working. digging more, appears doesn't assign port until used, 2 bind statements might going different ports.

from msdn:

"if not care local port used, can create ipendpoint using 0 port number. in case, service provider assign available port number between 1024 , 5000. if utilize above approach, can find local network address , port number has been assigned calling localendpoint. ... if using connectionless protocol, not have access info until have completed send or receive."

trouble is, want set send , receive on initialization. if wait until first send set receive, might miss responses. there improve reply sending garbage message assign port can finish initialization?

my code:

public bool initializesockets() { seek { ipaddress localaddr = localipaddress(); localep = new ipendpoint(localaddr, 0); //(was port 50000); //----------------------------------------------------------------- // set listener port responses coming on same port //----------------------------------------------------------------- listener = new udpclient(); listener.exclusiveaddressuse = false; listener.client.setsocketoption(socketoptionlevel.socket, socketoptionname.reuseaddress, true); listener.client.bind(localep); detailsoutputtext = "ready hear on " + localep; ustate = new udpstate(); ustate.e = localep; ustate.u = listener; //------------------------ // set broadcast port //------------------------ bcast = new udpclient(); bcast.client.setsocketoption(socketoptionlevel.socket, socketoptionname.reuseaddress, true); bcast.client.bind(localep); //------------------------------- // start listening responses //------------------------------- msgrxcallback = listener.beginreceive(new asynccallback(discoverycallback), ustate); homecoming true; } grab (exception exc) { if (exc socketexception) { // catches if process has opened port without sharing // or if firewall blocks it? messagebox.show("error opening ip address:port : " + localep; } else messagebox.show(exc.tostring()); homecoming false; } }

thanks

two options:

create configuration file contains port number. on initialization, read config file , utilize port number when setting clients. supply config file default port number, , give client instructions how alter if required. in initialization, create receiver, give short receive timeout, , phone call receive. cause port bind. can local end point , utilize when create sender. see can set timeout udpclient in c#? regarding setting receive timeout.

c# sockets udp udpclient

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -