A local network conferencing program based on UDP data broadcasting
content:
Introduction Program Design Project Test Conclusions About Authors
Jiang Qingye (Qjiang@ieee.org) US Navigation and Control Company
This article describes the General Transmission Control Protocol (TCP) and User Packet Protocol (UDP) in Network Communications, and the Java language is designed with a simple UDP data broadcasting local area network conferencing program, showing UDP in the Java language. The general step of data transmission and reception. Due to the excellent cross-platform characteristics of Java language, this system can not be modified on a series of different platforms such as Windows, Linux, Mac OS.
Introduction With the popularity of network technology, the application of online conferences in companies, enterprises, and units is getting wider and wider. A network conference system typically includes a server program and a client program. The server is responsible for user management, information interaction, and voting statistics; the client is implemented to listen, publicly speaking, private discussion, voting voting. In a design and implementation of a network conferencing system, a series of technologies such as graphical user interface design, TCP / IP connection, UDP data broadcast, multi-threads are usually involved. This article shows a simple example program that shows the general steps of UDP data transmission and reception in the Java language and the role of UDP data broadcast in the local area network conference system. TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-based communication protocol. When reliable data transmission between the two computers, they establish a stable connection over the network, which is generally also referred to as a data chain. Similar to the telephone network, this data chain is a point-to-point, and the communication is transmitted back and forth through this data chain. Based on this stable data chain, the TCP protocol can ensure that the data transmitted by the receiver receives the data and the data transmitted by the receiver in the content and sequence, thereby realizing reliable transmission of data. . Different between UDP (User DataGram Protocol, User Packet Protocol) and TCP protocols are not a communication protocol based on a stable connection. The UDP protocol transmits an independent packet from one computer to another computer, but does not guarantee that the acceptor can receive the packet, nor does it guarantee the data received by the receiver and the data sent by the sender in the content. And the order is exactly consistent. Therefore, the UDP protocol is more similar to ordinary postal services. The sender cannot guarantee that the letter sent can be received in time, and the letter issued will arrive earlier than the letter issued. For many applications, it is important to ensure a reliable and stable data chain between two computers that communicate with each other. In this case, the TCP protocol should be considered to establish a TCP / IP connection between the TCP protocol. In HTTP (Hyper-Text Transfer Protocol, Super Text Transfer Protocol), FTP (File Transfer Protocol, File Transfer Protocol), and Telnet Applications, it is required to establish a stable and reliable data chain between the two sides of the communication, so they all The TCP protocol is used to perform data transmission. In the TCP protocol, the sender and the recipient must exchange additional information to ensure that the recipient has received the transmitted data packet and the received data and the data transmitted by the sender are completely consistent on the content and order. These additional information exchange improves the reliability of data transmission, but also brings additional burden to the network, resulting in the delay of data exchange, thereby reducing the data exchange capability of the entire network. Such delays may be unacceptable for applications with higher real-time requirements. For example, a millisecond-level clock server provides time data to the client in accordance with a certain frequency, if these time data is greatly delayed during transmission, these outdated time data is completely meaningless, even if the client is accurate The incorrect received this data. Conversely, if each packet received by the client is real-time, even if the client is wrong, one or two packets can be accepted, because he can always follow the data packets received later. Correction. Therefore, the UDP protocol is obviously a suitable option for applications that are relatively low for real-time requirements, but the transmission reliability requirement. Under the EHTERNET architecture, the data exchange between computers is done by a switch.
If a data needs to be transmitted to multiple recipients, in the case of using a TCP / IP connection, the data sender needs to send N the same copy to the switch, and the switch is responsible for sending this N copy to all received. In the case of using UDP data broadcast, the data sender only needs to send a copy to the switch, and the switch is responsible for making N copies to all machines. In this case, use the TCP / IP connection to increase the burden on the network. In an ordinary local area network, it can be considered that due to poor network conditions, the possibility of data loss is relatively small, and the data exchange using UDP data broadcast can greatly reduce the burden on the network, so it is designed a local area network based on UDP data broadcasting. The conference system is fully feasible. Generally, only one physical interface is connected to the network, and all applications receive data from the network through the physical interface or send data to the network. Since there are multiple computers on a network, and a computer may have multiple applications to exchange data exchange with the network, we usually use IP and port numbers to identify computers and applications that need to be exchanged. Each computer is identified by a 32-bit IP address, in a network, the IP address of each computer is unique, so the application can send data to the correct computer according to the IP address. Each of the applications that need to be exchanged with the network is allocated a 16-bit port number, and the system forwards the data received from the network to the corresponding application based on this port number. The port number ranges from 0 to 65535, which is reserved from 0 to 1023, mainly for system services such as HTTP, FTP, and Telnet, so the user's own application should not try to use ports of less than 1023. A significant advantage of the Java language is that it provides support from the network from the height of the language, making programmers to build a network-based application. 5 interfaces and 21 Java classes are available in the standard class library of Java 1.3. On the basis of these interfaces and classes, programmers can easily implement almost all common network applications. For example, Serversocket can build TCP / IP-based server programs that can be used to build TCP / IP-based client programs, while DataGrampacket and DataGramsocket 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 secure licenses. Since these Java libraries have exceeded the scope of this article, interested readers can further refer to Java's documentation and Sun's Java Tutorial and other materials. This simple program includes the following three modules:
Data Broadcasting and Receiving Module - Broadcast.java Data Receive Thread - Receiver.java Graphical User Interface - Chat.java Program Design Data Broadcast and Receiving Module Broadcast.java is the core part of this sample program. This class includes a construction method, a data transmission method, and a data receiving method. In order for this class to be more widely applied to other applications, the author adds a port configuration method. In a constructor, we first use inetaddress to define a data broadcast group while constructing a DataGramsocket for transmitting data with a multicastsocket for receiving data. In this this, we use 230.0.0.1 as the marker of the data broadcast group, although this marker is the same as the IP address, but it does not represent a machine on the Internet. In addition, we specify 1235 ports and 1236 port bit data transmission and data receiving ports in port configuration methods. If a UDP data broadcast system is compared to a radio broadcast system, the data broadcast marker can be considered a band, and the data receiving port can be considered a frequency. The radio user must adjust the radio to the corresponding band and frequency to receive the radio signal. Our UDP data receiver must also join the corresponding data broadcast group and use the correct data receiving port to properly receive UDP broadcast data. . In a construction method, we use the MulticastSocket constructor to specify the data receiving port (frequency) and specify the data broadcast group (band) using its JOINGROUP method. Public Broadcast ()
{
GetBroadcasts ();
Try
{
// Construct data broadcast group marker (band)
BroadcastGroup = inetaddress.getbyname ("230.0.0.1");
// Construct data send port
Sender = New DataGramsocket (Serverport);
// Construct data receiving port (frequency)
Receiver = New MulticastSocket (Clientport);
/ / Specify the data broadcast group (band) of the data receiving port
Receiver.joingroup (BroadcastGroup);
} catch (exception e) {}
}
In the data transmission method, we construct a DataGrampacket packet based on the data provided by the user and the data broadcast destination port (frequency), and then transmit the packet to the local area network using the Send method of sending data. In contrast, in the data receiving method, we first construct an empty DataGrampacket packet, and then populate the contents of the packet using the Receive method of receiving data. In order to avoid trouble such as data loss due to different data packets, we specially set the size of the two packets.
// Data transmission method
Public Void Senddata (String MSG)
{
Byte [] b = new byte [1024];
DataGrampacket packet;
Try
{
// byte sequence B includes data that needs to be sent
B = msg.getbytes ();
/ / Construct a packet, BroadcastGroup is a data broadcast group marker (band),
// ClientPort is a data broadcast target port (frequency).
Packet = New DataGrampacket (B, B.Length, BroadcastGroup, Clientport);
// Send a packet
Sender.send (packet);
} catch (exception e) {}
}
// Data receiving method
Public String ReceiveData () {
Byte [] b = new byte [1024];
/ / Construct an empty packet
DataGrampacket Packet = New DataGrampacket (B, 1024);
String Inmsg;
Try
{
// Receive data
Receiver.Receive (Packet);
} catch (ioexception e) {}
// Clusted data in the packet
B = packet.getdata ();
INMSG = New String (b);
Return INMSG;
}
Data Receive Thread Receiver.java's task is to receive broadcast data and update the graphical user interface. The constructor of this class includes two parameters, and the parameter listener specifies the Broadcast object used to receive data, and the parameter Display specifies the TextArea object used to display the contents of the meeting. In its operating method RUN, the data receiving method of the Broadcast object is recalled, and the newly received content is displayed on the graphical user interface using the APPEND method of the TextArea object.
Public Class Receiver Extends Thread
{
Broadcast Listener;
Textarea display;
String Inmsg;
// Construction method
Public Receiver (Broadcast Listener, Textarea Display)
{
// Listener is a data transmission and receiving object for receiving data.
Listener = listener;
/ Display is a TextArea object that displays the content of the meeting.
Display = display;
}
// Operation method
Public void Run ()
{
While (True)
{
// Receive broadcast data
INMSG = listener.ReceiveData ();
/ / Update the graphical user interface
Display.Append (Inmsg);
Display.Append ("/ n");
}
}
}
The graphical user interface Chat.java is built based on the Java Abstract Window Toolkit AWT. This interface includes a TextRea for displaying the contents of the conference, one for receiving the user-entered textField, and a data send command button. In the configuration method of the CHAT, we first create a graphical interface, construct a data transmission and receive object and a data receiving thread, and then start the thread to start receiving the meeting information. In this method, we also use the inetaddress.getlocalHost () method to get the user's machine name, which is used as a username in the user in the web conference.
// Construction method
Public chat ()
{
// Create a graphical interface
Add (CreateGUI ());
// ....
// Other operations
// ....
/ / Construct a data transmission and reception object
Device = new Broadcast ();
// Construct a data receiving thread
Receiver Recv = New Receiver (Device, Inmsg);
Recv.start ();
}
Program Test Using JDK 1.3 Compile All Source Codes: Javac * .java Launched Network Conference Programs: Java Chat This sample program is in one including Windows 98, Windows 2000, Red Hat Linux 6.1 / 7.0, Mac OS, Sun Solaris, etc. The system's local area network is tested. Conclusion This paper introduces the General Transmission Control Protocol (TCP) and User Packet Protocol (UDP) in Network Communications, and uses Java language to design a simple UDP data broadcasting LAN conferencing program, showing in the Java language. UDP data transmission and reception of general steps. This sample program illustrates the general functionality of the LAN conference using UDP data broadcasting. Due to the excellent cross-platform characteristics of Java language, a Java-based local area network conferencing system is not modified on a series of different platforms. About the author Jiang Qingye, software engineering expert. In July 1999, a bachelor's degree in Tsinghua University was obtained in January 2001, the Master of Illinois at Urbana-Champaign, is currently an expert in American GNC Corporation. The main research sectors include remote sensing image information processing, GPS applications, inertial navigation, wireless communications, and high-speed network technology. Email: Qjiang@ieee.org. -------------------------------------------------- -------------------------------------------------- ---- Reserved: http://www-900.ibm.com/developerWorks/Cn/java/l-jchat/index.shtml ------------------- -------------------------------------------------- -----------------------------------