Develop a network protocol with Java's New IO

zhaozj2021-02-16  60

Recently, I have developed a simple network agreement with Java's New IO. Here I will organize some of the experience in the development. Summarize a simple set of reusable network protocol development frameworks, hoping to help beginners. The basic communication unit of the network protocol is a message package. When transferring these packages with Socket, the first question to be solved is how to solve the boundary between the package and the package. Socket transmission is a stream, a message issued in a Send, not necessarily received in a RECV, may have to receive multiple recv, or a RECV receives the package released in multiple Send. So you must solve the boundary issue of the package by the application layer protocol. There are usually two ways, one is to end each package with a special character or string, such as the HTTP protocol is the end tag of two '/ n' as a message; another method is that all messages There is a fixed length message head, pointing out the length of this message in the message header. Our protocol is the second method, which is also a method of most protocols. The framework proposed in this paper is also solving this protocol. Java's New IO is introduced in J2SE1.4, mainly introduced in the concept of buffer, and sending acceptance data is performed on the buffer, and for beginners, Buffer operation is complicated, easy to make mistakes. Therefore, in this framework, the operation of buffer is encapsulated as much as possible. The frame mainly has three interfaces, two types of MessageChannel, BuffERUTIL, and an exception class MessageFormAnXception. The features of these interfaces and classes will be described below.

1. The MessageHeader interface is in such a set of network protocols, there is always a fixed length message head, and the different protocols have different message heads, but almost all message headers define the length of this message and the type of this message. Types are used to identify different messages. The same package, the format is the same, can be expressed with the same Java Class. Different packages of different types, format may be the same or different, depend on the protocol. The interface is defined as follows: Public interface messageHeader {/ ** * Return message type * / int getMessageType (); / ** * Return message length * / int GetMelength ();

/ ** * Extract a message header from Buffer * / Void BuildFromBuffer (Bytebuffer Buffer);

/ ** * Put the message header in the buffer * / void appendtobuffer (bytebuffer buffer);

2. Message Interface Message represents a message package. Each message package has a message header. It is defined as follows: public interface message {/ ** * Set the message header, call * / voideter header in MessageChannel.Receive;

/ ** * Return message head * / MessageHeader getHeader ();

/ ** * Take a message from Buffer * / void BuildBodyFromBuffer (BYTEBUFFER BUFFER); / ** * put the message into buffer * / void appendbodytobuffer (bytebuffer buffer);

3. MessageFactory Interface This interface encapsulates all the creation of the class for expressing the message, calls in the MessageChannel's Receive. Here is an abstract factory model. It is defined as follows: public interface messagefactory {/ ** * Return the number of bytes of the message * / int getMessageHeaderLength (); / ** * Create a message header object * / messageHeader createMessageHeader (); / ** * Create a message object * @PARAM TYPE message type, get * / message createMessage (int type) from the message header;

4. The main features of the MessageChannel class are in this class, used to send and receive messages, and encapsulate all of the operations for buffer. Class MessageChannel {/ ** * Constructor requires the size of the transmission buffer and the received buffer * / public MessageChannel (int ReceiveBuffersize, Int SendBuffersize, SocketChannel SC, MessageFactory MF); / ** * Receive a message, when the message is not Complete, the received message length is too large (exceeding the received buffer size) or does not throw MessageFormATexception when the message type created by MessageFactory. * / Public message receive () throws oException, messageformatexception;

/ ** * Send a message * / public void send (Message Message) throws oews oException;

}

5. Bufferutil class This is a Utility class, the main function is to acquire or put a string String from bytebuffer, and different protocols have different string processing methods. Class bufferutil {/ ** * get a string from Buffer, Length is the length * / static string getString (Bytebuffer Buffer, int LENGTH); / ** * Get a string with '/ 0' end from Buffer, Length is Maximum length * / static string getcstring (Bytebuffer Buffer, int LENGTH); / ** * Get a growing string from Buffer, with two bytes of Short Type indicates * / static string getvarstringshstlength;

/ ** * From buffer to get a growing string, the length uses the four-byte int type * / static string getvarstringintLength; BYTEBUFFER BUFFER

/ ** * Get a growing string from Buffer, with one byte of Byte represents * / static string getvarstringBytelength (); / ** * Add a string in the buffer, Length is the length * / static void Putstring (Bytebuffer Buffer, String Str, Int Length);

/ ** * Put a string in Buffer, Length is the maximum length. If the STR does not reach the maximum length, then fill it with 0. * / Static void Putcstring (Bytebuffer Buffer, String Str, INT Length); / ** * Put a string in Buffer, with a short indicate * / static void putvarstringshLength (Bytebuffer Buffer); / ** * In Buffer Put a string, the length is used to indicate * / static void putvarstringintLength (Bytebuffer Buffer); / ** * Add a string in the buffer, with a Byte to represent * / static void putvarstringBytelength; BYTEBuffer buffer; } Currently, this class does not consider encoding mode, which can be expanded. In this frame, Message.Receive is the most complex portion, which describes the process of the process: (1) Receive the message header according to MessageFactory.getMessageHeaderLength (). (2) Construct the message header with a messageFactory.createMessageHeader () and call MessageHeader.BuildFromBuffer () obtains message header data. (3) The message body (4) constructs message according to the information in the message head, and calls Message.BUILDBODYFROMBUFFER () to obtain message body data. (5) Call Message.setHeader (). (6) Returns the message of the structure. One of this is an error, and will throw a MessageFormATexception.

Source programs, including an example program, can download from the following URL: http://jinhuaxing.myrice.com/src.rar

转载请注明原文地址:https://www.9cbs.com/read-24068.html

New Post(0)