UDP is a very practical protocol. But it also has a bad drawback, that is, it is not safe. However, for some data processing, it is still very good. Let's discuss the design of UDP data broadcast. This article introduces the Transmission Control Protocol (TCP) and user data packet protocol (UDP) used in network communication ), in addition, a simple local network Conferencing program based on UDP data broadcast is designed using the Java language, showing the general steps for sending and receiving UDP data in the Java language. Thanks to the outstanding cross-platform features of Java, the system can run on Windows, Linux, Mac OS, and other platforms without modification.
With the popularization of network technology, the application of network conferences in companies, enterprises and organizations is becoming wider and wider. A network conferencing system usually includes a server program and a client program. The server is responsible for user management, information interaction, and voting statistics. The client can listen to speeches, make public speeches, discuss in private, and vote for users. In the design and implementation of a network conference system, a series of technologies are usually involved, such as graphic user interface design, TCP/IP connection, UDP data broadcast, and multithreading. This article uses a simple example program to demonstrate the general steps for sending and receiving UDP data in Java and the role of UDP data broadcast in the local network conferencing system.
TCP (Transport Control Protocol) is a connection-based communication Protocol. When two computers need reliable data transmission, they establish a stable connection through the network, which is also called a data link. Similar to the telephone network, this data link is point-to-point, and the communication parties transmit data back and forth through this data link. Based on this stable data link, the Information Verification of TCP protocol ensures that the data received by the receiver is completely consistent with the data sent by the sender in terms of content and order, this achieves reliable data transmission.
The difference between UDP (User data packet Protocol) and TCP is that UDP is not a stable connection-based communication Protocol. The UDP protocol transmits an independent data packet from one computer to another, but it does not guarantee that the receiver can receive the data packet, it is not guaranteed that the data received by the receiver is completely consistent with the data sent by the sender in terms of content and order. Therefore, the UDP protocol is more similar to the ordinary postal service. The sender cannot ensure that the sent mail can be received by the recipient in a timely manner, and the sent mail may arrive earlier than the sent mail. For many applications, it is vital to ensure a reliable and stable data link between two computers that communicate with each other. In this case, you should first consider using the TCP protocol to establish a TCP/IP connection between the computers in the cooling station. In HTTP (Hyper-Text Transfer Protocol), FTP (File Transfer Protocol), and TELNET applications, A stable and reliable data link must be established between both parties. Therefore, both parties use the TCP protocol for data transmission.
In TCP, the sender and receiver must exchange additional information to ensure that the receiver has received the sent data packets and the received data is completely consistent with the data sent by the sender in terms of content and order. This extra information exchange improves the reliability of data transmission, but it also brings an extra burden to the network, resulting in a delay in data exchange, thus reducing the data exchange capability of the entire network. For some applications with high real-time requirements, such latency may be unacceptable. For example, a millisecond-level clock server provides the current time data to the client at a certain frequency. If the time data is greatly delayed during transmission, the outdated time data is completely meaningless, even if the client receives the data accurately. On the contrary, if each packet received by the client is real-time, it is acceptable even if the client misses one or two packets, because he can always correct himself Based on the packets received later. Therefore, UDP protocol is a suitable choice for applications that require high real-time performance but low transmission reliability.
Under the General Ethernet (Ehternet) architecture, data exchange between computers is achieved through switches. If a copy of data needs to be sent to multiple recipients, the data sender needs to send N identical copies to the switch when TCP/IP connection is used, the switch is responsible for distributing the N copies to all receivers. When UDP data broadcast is used, the data sender only needs to send a copy to the switch, the switch is responsible for making N copies of the information and sending it to all machines. In this case, using a TCP/IP connection will greatly increase the burden on the network. In a common local area network, we can think that the possibility of data loss due to poor network conditions is relatively small, and the use of UDP data broadcast for data exchange can greatly reduce the burden on the network, therefore, it is feasible to design a local network conferencing system based on UDP data broadcast.
Generally, a computer has only one physical interface connected to the network. All applications receive data from or send data to the network through the physical interface. Because multiple computers exist on one network at the same time, and multiple applications may need to exchange data with the network at the same time on one computer, we usually use IP addresses and port numbers to identify computers and applications that require data exchange. Each computer is identified by a 32-bit IP address. In a network, the IP address of each computer is unique. Therefore, applications can send data to the correct computer based on the IP address. Each application that needs to exchange data with the network is assigned a 16-bit port number by the system, which forwards the data received from the network to the corresponding application. The range of port numbers is from 0 to 65535, and from 0 to 1023 is retained by the system. It is mainly used to provide system services such as HTTP, FTP, and TELNET, therefore, your applications should not try to use ports smaller than 1023.
One of the notable advantages of Java is that it provides network support from the language level, so that programmers can easily build network-based applications. Java 1.3 standard class library java.net provides 5 interfaces and 21 Java classes. Based on these interfaces and classes, programmers can easily implement almost all common network applications. For example, ServerSocket can be used to build TCP/IP-based server programs. Socket can be used to build TCP/IP-based client programs, while initrampacket and initramsocket can be used to build UDP-based data broadcast programs. Other Java libraries in java.net can be used to implement a series of functions such as domain name resolution, identity authentication, and security license. As the functions and usage of these Java libraries are beyond the scope of this article, interested readers can refer to the Java documentation and Sun's Java Tutorial documents.
This simple program includes the following three modules:
Data Broadcasting and receiving module-Broadcast. java
Data receiving thread -- Receiver er. java
Graphical user interface-Chat. java
Address: http://www.voipchina.cn/technology/2004-03-24/36859.shtml