Develop a network protocol (transfer) with Java New IO

xiaoxiao2021-03-06  62

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 getMerationLength (); / ** * Extract message header from BUFFER * / Void BuildFromBuffer (BYTEBUFFER BUFFER); / ** * Plany a message head in Buffer * / void service;} 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 * / voidetheader (MessageHeader header) in MessageChannel.Receive; / ** * Return message head * / messageHeader getHeader (); / ** * Buffer Takes the message body * / void building; / ** * puts the message into the buffer * / void service;} 3. MessageFactory interface This interface encapsulates all true to express messages The creation of the class is called 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 MessageChannel class is in this class, used to send and receive messages, and encapsulate all for Buffer operations. 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 IOException, MessageFormatException; / ** * Send a message * / public void send (Message message) throws IOException;} 5. BufferUtil class This is a Utility category, is acquired from the main function or ByteBuffer Put a string String, 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 * / static string getvarstringshortlength; / * * * 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 a length of one byte Byte represents * / static string getvarstringBytelength (); / ** * Add a string in Buffer, Length is the length * / static void Putstring (Bytebuffer Buffer, String Str, Int length); / ** * in buffer Put a string, Length is the maximum length. If the STR does not reach the maximum length, then fill it with 0.

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

New Post(0)