Developing a network protocol with Java new IO

Source: Internet
Author: User
Tags socket

Recently with the Java new IO developed a set of simple network protocol, where I put some of the development of ideas sorted out, summed up a simple reusable network protocol development framework, hoping to give beginners a little help.

The basic communication unit of a network protocol is one message packet. One of the first issues to solve when transferring these packets with a socket is how to solve the boundary problems between packages and packages. Socket is the stream, a send in the message, in the other is not necessarily in a recv received, may have to multiple recv, or a recv receive multiple send in the packet. Therefore, it is necessary to solve the problem of packet delimitation by the Application layer protocol itself. There are usually two ways of one is that each package ends with a special character or string, such as the HTTP protocol being the end tag of two ' \ n ' as a message, and the other is that all messages have a fixed-length message header that indicates the length of the message in the message header. Our agreement is to adopt the second approach, which is also the approach used in most agreements. The framework proposed in this paper is also the solution to this protocol.

Java's new IO is introduced in the j2se1.4, the main introduction of the concept of buffer, send the acceptance data are on the buffer, and for beginners, the operation of the buffer is more complex, error prone. So in this frame, as much as possible to the operation of the buffer package.

The framework consists mainly of MessageHeader, message, messagefactory three interfaces, two classes Messagechannel, Bufferutil, and an exception class messageformatexception composition. The features of these interfaces and classes are described below.

1. MessageHeader interface

In such a set of network protocols, there is always a fixed-length message header, different protocols have different headers, but almost all message headers define the length of this message and the type of this message. Types are used to identify different message packs. The same type of package, the format is the same, you can use the same Java class to express. Packages of different types may or may not be the same format, depending on the protocol. The interface is defined as follows:

public interface MessageHeader {
/**
* 返回消息类型
*/
int getMessageType();
/**
* 返回消息长度
*/
int getMessageLength();
/**
* 从Buffer中提取消息头
*/
void buildFromBuffer(ByteBuffer buffer);
/**
* 把消息头放到Buffer中
*/
void appendToBuffer(ByteBuffer buffer);
}

2. Message interface

The message represents a packet of messages. Each message packet has a message header. The definition is as follows:

public interface Message {
/**
* 设置消息头,在MessageChannel.receive中调用
*/
void setHeader(MessageHeader header);
/**
* 返回消息头
*/
MessageHeader getHeader();
/**
* 从Buffer中取出消息体
*/
void buildBodyFromBuffer(ByteBuffer buffer);
/**
* 把消息体放到Buffer中
*/
void appendBodyToBuffer(ByteBuffer buffer);
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.