C # uses UDP client to write chat programs

Source: Internet
Author: User
Tags ip number reserved port number

The UdpClient class uses UDP to communicate with network services. The advantage of UDP is its ease of use and the ability to broadcast messages to multiple addresses at the same time. However, because the UDP protocol is a connectionless protocol, UDP datagrams sent to remote endpoints are not necessarily reachable or can be reached in the same order in which they were sent. Applications that use UDP must be prepared to handle lost and incorrectly sequenced datagrams.

To send datagrams using UDP, you must know the network address of the network device that hosts the required services and the UDP port number that the service uses for communication.

Special network addresses are used to support UDP broadcast messages on ip-based networks. The following discussion is an example of the IP version 4 address family used on the Internet.

The IP version 4 address uses 32-bit to specify the network address. For class C addresses that use the 255.255.255.0 netmask, these bits are divided into four eight-bit bytes. When expressed as a decimal number, these four eight-bit bytes form a familiar, dotted, four-part representation, such as 192.168.100.2. The first two eight-bit bytes (192.168 in this example) Form the network number, the third eight-bit byte (100) defines the subnet, and the last eight-bit byte (2) is the host identifier.

Setting all the bits of an IP address to 1 (that is, 255.255.255.255) can constitute a limited broadcast address. Sending a UDP datagram to this address delivers a message to any host on that broadcast network. Because routers never forward messages that are sent to this address, only hosts on connected networks can see these broadcasts.

You can direct a broadcast to a specific network section by setting all the bits in a part of the address to 1. For example, to send a broadcast to all hosts on a network identified by a 192.168 IP address, set the subnet and host portions of the address to 1, such as 192.168.255.255. To limit broadcasts to a single subnet, only the host portion is set to 1, such as 192.168.100.255.

The UdpClient class can broadcast to any network broadcast address, but it cannot listen for broadcasts sent to the network. You must use the Socket class to listen for webcasts.

Broadcast addresses work when all recipients are in a single network, or when many clients need to receive broadcasts. When the receiver is a small part of the network, the message should be sent to the multicast group, where only clients joining this group can receive the message. The range of IP addresses from 224.0.0.2 to 244.255.255.255 is reserved for the primary group address. The IP number 224.0.0.0 is reserved, and the 224.0.0.1 is assigned to a fixed group of all IP hosts.

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.