C # socket UDP broadcast

Source: Internet
Author: User

First, broadcast News

Because TCP is connected, it cannot be used to send broadcast messages. To send a broadcast message, you must use UDP,UDP to send a message without establishing a connection. The destination IP address of a broadcast message is a special IP address, called a broadcast address. The broadcast address consists of an IP address network prefix plus a full 1 host suffix, such as: 192.168.1.255 is the broadcast address of the 192.169.1.0 network, and 130.168.255.255 is the broadcast address of the 130.168.0.0 network. If you send a message to all 1 IP addresses (255.255.255.255), then theoretically all networked computers in the world can be received. But this is not the case, the general router is set to abandon such a package, only in the local network broadcast, so the effect and to the local network broadcast address is the same as sending messages.

With UDP broadcasts you can implement a server like CS, and the client can receive a server message to connect.

Second, the service side

The open thread keeps broadcasting its own IP address, waiting for the client to receive

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Net;6 usingSystem.Net.Sockets;7 usingSystem.Threading;8 usingSystem.Threading.Tasks;9 Ten namespaceSCOKET_UDP_ Server One { A     class Program -     { -         Private StaticSocket sock; the         Private StaticIPEndPoint Iep1; -         Private Static byte[] data; -         Static voidMain (string[] args) -         { +Sock =Newsockets (AddressFamily.InterNetwork, Sockettype.dgram, - protocoltype.udp); +             //255.255.255.255 AIEP1 = at             NewIPEndPoint (Ipaddress.broadcast,9050); -  -             stringhostname =dns.gethostname (); -data =Encoding.ASCII.GetBytes (hostname); -  - sock. SetSocketOption (Socketoptionlevel.socket, inSocketoptionname.broadcast,1); -  toThread T =NewThread (broadcastmessage); + T.start (); -             //sock. Close (); the  * Console.readkey (); $ Panax Notoginseng         } -  the         Private Static voidbroadcastmessage () +         { A              while(true) the             { + sock. SendTo (data, IEP1); -Thread.Sleep ( -); $             } $              -         } -  the     } -}

Third, the client

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Net;6 usingSystem.Net.Sockets;7 usingSystem.Threading.Tasks;8 9 namespaceSocket Client UDPTen { One     class Program A     { -         Static voidMain (string[] args) -         { theSocket sock =NewSocket (AddressFamily.InterNetwork, - Sockettype.dgram, protocoltype.udp); -IPEndPoint IEP = -             NewIPEndPoint (Ipaddress.any,9050); + sock. Bind (IEP); -EndPoint EP =(EndPoint) IEP; +Console.WriteLine ("Ready to receive ..."); A  at             byte[] data =New byte[1024x768]; -             intrecv = sock. ReceiveFrom (data,refEP); -             stringStringData = Encoding.ASCII.GetString (data,0, recv); -  -Console.WriteLine ("received: {0} from: {1}", - StringData, Ep.           ToString ());  in sock. Close (); -  to Console.readkey (); +         } -     } the}

C # socket UDP broadcast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.