Void deleetest () {// 1 create a set of character system. net. sockets. socket S = new socket (addressfamily. interNetwork, sockettype. dgram, protocoltype. UDP); S. setsocketoption (system. net. sockets. socketoptionlevel. socket, socketoptionname. reuseaddress, true); // s. setsocketoption (socketoptionlevel. socket, socketoptionname. reuseaddress, true); // 2 bind to port 4567 ipendpoint IPE = new ipendpoint (IPaddress. any, 4503); endpoint Ep = (endpoint) IPE; // reuse the port and specify the same port, //// // This sentence must be added. Adding this sentence can enable two Program Listen to the same port S. exclusiveaddressuse = false; // reuse port S. setsocketoption (system. net. sockets. socketoptionlevel. socket, socketoptionname. reuseaddress, true); // s. BIND (EP); // 3 add to multicast group 234.5.6.7 multicastoption optionvalue = new multicastoption (IPaddress. parse ("239.239.239.239"); S. setsocketoption (socketoptionlevel. IP, socketoptionname. addmembership, optionvalue); // 4 receives multicast group data console. writeline ("start receiving data on multicast group 234.5.6.7... "); byte [] buffer = new byte [1024]; while (true) {int nret = S. receivefrom (buffer, ref EP); If (nret> 0) {string data = encoding. utf8.getstring (buffer, 0, nret); console. writeline (data); uclient. send (system. text. encoding. getencoding ("gb2312 "). getbytes ("this is from IIS"), 15 );}}}
Note that the most important part is
// This sentence must be added to allow the two programs to listen to the same port S. exclusiveaddressuse = false; // reuse port S. setsocketoption (system. net. sockets. socketoptionlevel. socket, socketoptionname. reuseaddress, true );