Using JAVA to realize Peer-to-peer network communication __java
Source: Internet
Author: User
Guide:
Absrtact: This paper analyzes the basic concept of peer-to-peer and its basic working principle, discusses the technology of realizing Peer-to-peer network communication with Java, and further expounds it with an example.
Tags: p2p,c/s,java,xml,jdk, tcp,udp
First, the preface Peer-to-peer (peer-to-peer end-to-end) model corresponds to the C/s (client/server) model. The communication between users based on C/s needs to be relayed by the server, and the server failure in C/s will cause the whole network communication to be paralyzed. and Peer-to-peer communication between users is direct communication, remove the server this layer, bring a significant advantage is the communication without a single failure point, a user's failure will not affect the entire Peer-to-peer network. This paper provides a way to realize Peer-to-peer network communication with Java.
The key technology analysis of Peer-to-peer communication (i) Transmission of information
1. Peer-to-peer Communication Model
As can be seen from the above diagram, direct communication can be achieved between any two endpoints in a Peer-to-peer network. In a C/s network, clients can locate each other by registering with the server (obtaining IP and Port). For Peer-to-peer networks, how to achieve each other's positioning and communication, the following to do an elaboration.
2. Get the IP and port on the network that can communicate the endpoint
Suppose that there is an endpoint a, to communicate with other endpoints in the Peer-to-peer network, before communication, endpoint a must first notify its own IP and port to each of the other endpoints in the Peer-to-peer network. After each other endpoint receives this information, it obtains the IP and port of endpoint A, then feeds its IP and port information to endpoint A, so that endpoint a also obtains the IP and port of each endpoint in the Peer-to-peer network.
Here are two technologies that enable endpoint A to notify other endpoints of their IP and port work, one is broadcast technology, the other is multicast technology.
Broadcast technology is mainly used in LAN, each endpoint (host) in the local area network has to accept and process a broadcast packet. Therefore, in order to avoid network congestion, routers restrict the passing of broadcast packets. Therefore, the design of internet-based peer-to-peer programs is not suitable for the use of broadcast technology.
Multicast technology is a network technology that allows one or more senders (multicast sources) to send a single packet of data to multiple receivers (once, at the same time). A multicast source sends packets to a specific multicast group, and only the addresses belonging to that multicast group can receive packets. Multicast can save network bandwidth greatly and improve the efficiency of data transmission. Reduces the likelihood of congestion in the backbone network. Endpoints (hosts) in a multicast group can be on the same physical network or from a different physical network (if supported by a multicast router). Therefore, multicast technology is our choice.
In Java, the method of sending and receiving multicast information:
To send multicast information, you need to go through steps
Determine what specific information is sent
String msg = "Hello";
Create a multicast group by selecting a Class D IP address (224.0.0.1 to 239.255.255.255) that is specifically designated for multicast
InetAddress Group = inetaddress.getbyname ("228.5.6.7");
Sets up a multicast socket using the specified port (typically more than 1024 port numbers are selected)
MulticastSocket s = new MulticastSocket (6789);
Join a multicast group
S.joingroup (group);
Create a datagram package multicast information
Datagrampacket hi = new Datagrampacket (Msg.getbytes (), Msg.length (),
Group, 6789);
Send
S.send (HI);
Steps to receive multicast information
Open Receive Buffer
byte[] buf = new byte[1000];
Create a Receive datagram
Datagrampacket recv = new Datagrampacket (buf, buf.length);
Receive
S.receive (recv);
Note: The above send and receive programs are implemented in the same file, and if implemented in different files, you should define the multicast sockets separately and join the multicast group.
3. Communicating with endpoints of known IP and ports
TCP and UDP are mainly used on the Internet to realize the communication between two points. TCP can transmit information reliably, but it spends more time, but it can transmit information quickly with UDP, but it can't guarantee reliable transmission.
The method of implementing TCP Communication in Java
Create a client socket using the socket (inetaddress addr, int port) and socket (String host, int port), and create a server-side socket using serversocket (int port). Port ports are the ports on which the server listens for connection requests by calling accept () to return a recently created socket object that binds the client's IP address or port number. By calling the getInputStream () method of the socket to obtain the input stream read transmission, it may also be possible to get the output stream to send the message by calling the socket's Getoutputstream () method.
The method of implementing UDP communication in Java
Use Datagrampacket (byte [] buffer, int length, inetaddress addr, int port) to determine the packet array, the length of the array, the address of the packet, and the port information. Use Datagramsocket () to create a client socket, and the server side uses datagramsocket (int port) to invoke Send (Datagrampacket DGP) and receive (Datagrampacket DGP ) to send and receive packets. The program designed in this paper uses UDP.
(ii) Representation of information
Using XML technology
XML is a very flexible format based on text data. During the communication process, the transmitted data can be specified in any format according to the actual needs. However, XML is widely chosen in applications because XML provides a realistic and clearly described and easily read-write format. Writing XML files and parsing XML files is easy to accomplish. A text editor allows you to write XML files with 100% of pure ASCII text. XML parsing work can be implemented either by itself or by using XML parsers provided by several companies. Because XML is an international standard, the adoption of XML can bring many benefits to the future expansion of the program's functionality.
(iii) Response to information
In the communication process, the information response is critical. For real-time communication system Peer-to-peer, more value the speed of information response. This paper designs the Peer-to-peer communication program to improve the response speed mainly from the following two aspects of processing.
1. Queue
Because the endpoints (hosts) in Peer-to-peer Networks usually receive information from many endpoints, if a program waits for a message to be processed and then receives new information, it is bound to lead to the omission of some information. So receive information in the program to put the received information into the queue buffer, processing information from the queue in order to take out information processing.
2. Multithreading
Multithreading in Java allows multiple pieces of programs to run at the same time. Using multithreading technology, the user interface, information receiving and processing can be processed separately, which makes the program more stable and smooth, and greatly improves the response speed of information.
Three, Peer-to-peer communication program Example (i) Functional description
This program is a Peer-to-peer communication program, users of this software can establish peer-to-peer communication network. Communication can be easily communicated between users in the network. At the same time, this network is dynamic, in other words, users can join and exit this network at any time.
The main functions are:
Obtain the IP of endpoint (host) in Peer-to-peer Network and dynamically update
You can send text messages to the endpoints of any known IP
Can receive text information sent by other endpoints
Can get information about some endpoints exiting the network
The embodiment of the function in the user interface
(ii) Procedural framework
1. Information Flow Diagram
The information flow diagram describes the process of sending and receiving messages. In fact, each endpoint must have both the ability to send and receive information, so the capabilities of the diagram above will be integrated into a single user program.
2. Class table
According to the information flow diagram, determine the corresponding class to implement its function.
(ii) Program flow chart
Iv. Concluding remarks This procedure is designed to build Peer-to-peer network communication Basic framework, so the program provides a simple function, such as only the exchange of text information, network sockets, such as Port fixed. The reader can extend its functions on this basis, such as file transfer, dynamic allocation port, etc.
This article turns from
Http://bbs.itren.cn/html/bbs8593.html
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.