Java processing of network data stream

xiaoxiao2021-03-06  85

content:

1: Mugex Java Flow Treatment 2: Network Data Stream Transceree 3: Java Treatment of Network Data Stream 4: Conclusion Reference About the author

Guo Hongfeng (GHF_EMAI@sohu.com)

September 2001

This article helps the Java programmers written by the client server, can resolve the program to continue to run stable when the other party has failed.

Preface: Java programs have a lot of network data, network data transmission and reception, and data stream processing is aspects of Java programs, with Java's development, these methods are increasingly valued and strengthened. This article explains the elements of Java to properly handle network data streams from several aspects, which is the basic knowledge of Java programmers must understand. 1: The huge Java stream is first, the reason why the Java stream is huge, because the flow processing in Java is much more than the flow of flow in other languages. The Java stream is divided into a character stream and byte stream. The character stream processing unit is 2 bytes of Unicode characters, separately operates characters, characters or strings, and one byte stream processing unit is one byte, an operation byte, and byte arrays. Java is used to store characters in Java, and the character stream processing class is responsible for converting the exterior other coded character stream and the conversion between Unicode characters within Java. The Class InputStreamReader and OutputStreamWriter handles the conversion of the characters and byte streams. Character stream (one buffer can be handled) Operational stripping (once one byte) efficiency is high. For different streams, different flow structures are required or stream filtering. Java is still gradually increasing its stream processing method. Although the Java class library created a lot of reasons to illustrate the advantages of this, I still think that Java begins to become as complicated to other languages. 2: The transmission and reception of the network data stream Java to the transmission and reception processing of network data, also borrows general flow processing. We know that in almost all other languages, network data transmit and receive does not have obvious stream processing when using methods similar to Send (or WRITE) and RECV (or RECV (or Read). However, Java and these language transceivers have a great difference, and it is necessary to accomplish it by means of a flow:

.......

SOCK = New Socket (Addr, Port);

Outputstream Os = Sock.getOutputStream ();

InputStream IS = Sock.getInputStream ();

Os.write (byte [] b);

Is.read (byte [] b);

These methods have always given a feeling of uncomfortable feelings. However, this is now made up from JDK1.4. JDK1.4 has added new I / O stream processing, making new processing in buffer management, scalable network, and file IO, character set support, regular expression matching. The buffer management and channel concept is a strengthening of the transmission and reception processing support for network data streams. Bytebuffer classes in buffer support support network data stream processing. In a network connection, the channel represents the connection of Sockets. Based on these new IO processing, the above code can be rewritten as:

......

Bytebuffer Bytebuf = bytebuffer.allocate (2048); // Create a buffer for specified size

Inetsocketaddress isa = new inetsocketaddress (Hostname, Port);

SC = SocketChannel.Open (); // Create a Socket channel

Sc.Connect (ISA); // Create a Socket connection

...

Sc.write (bytebuf); // Send data

...

Sc.read (Bytebuf); // Receiving a program such that this seems to be smooth.

3: Java processes the network data stream Java program to pay attention to the network data streams: data stream encoding, byte order, data format correspondence, and number of numbers. This is four different issues, but it affects the correct reception of network data. 3.1 Decoding and encoding network data streams The encoding and decoding of network data streams primarily for strings that appear in the stream. The strings in the network data stream are the original byte stream form. To properly receive strings in the network data stream, you must first know the encoding scheme of the string. Then you can call the decoded method to get the Unicode encoded string that Java can know. You can use the following code to process the encoding and decoding of the strings in the network data stream:

// Get the encoding object, that is, the network peers understand the character string encoding.

Charset charset = charset.Forname ("???"); // ??? is the codeword name, Java must support.

/ / Generate an encoder and decoder object.

CharSetDecoder decoder = charset.newdecoder ();

Charsetencoder Encoder = charset.newencoder ();

.......

/ / A java string is obtained from the byte stream obtained from the network data stream.

Charbuffer Charbuf = decoder.decode (Bytebuff);

.......

// encode the Java string into the designated byte stream for the encoded, so that the network is sent

Bytebuff Bytebuff = Encoder.Encode (Charbuffer.wrap ("Test String");

.......

3.2 Network data streams The byte order There are two categories: BIG_ENGIAN and LITTLE_ENDIAN. The bytes supported by each platform are different, such as AIX, TRU64UNIX, Windows and other operating system platforms use Little_endian byte sequence, Solaris and other operating system platforms using BIG_ENGIAN. Java itself uses a BIG_ENGIAN word sequence, and when Java and communication programs written in other languages ​​on other platforms, the byte order of data must be considered. JKD1.4 The newly added package NIO ByteORDER has brought certain convenience. For the byte sequence from the network data stream, we can easily process the byte sequence as soon as we increase the line: bytebuf.order (Byteorder.Little_endian); / / According to the Little_endian byte, the data sc.read (Bytebuf); / / Receive the data above, although our programming is simplified, but there is no truly network data byte problem of distributed applications. For example, Java is simultaneously and in the Tru64Unix, the above method does not solve the problem. Because the same packet, it may not be possible to determine whether the word sequence is that. At this time, the network packets are required to carry additional byte sequence information is obviously unrealistic. In this case, the Java language needs to provide support for XDR (external data expression). At present, XDR has been in the standard format of the actual network data stream, and the network data stream of distributed applications will basically follow this format. If Java Language provides support for XDR to solve versatility issues. For processing of network data streams in distributed applications, there is no need to judge its word sequence according to its platform, as long as it is processed in the XDR format. 3.3 Network Data Format The data format in the C / C language typically transmits data in a buffer of the data structure, and when receiving data in the Java side, there will be some problems caused by data organizations:

Structure Typedef struct {

Int ID;

Char name [32]; short val;

Float fval;

Senddata

In the 32-bit operating system, its size is not 42, but 44! The organization of the data is shown below: When sending to the client over the network, the client also receives 44 bytes, and if the corresponding value is taken in sequence, the final acquired floating point value is not correct. This is because two bits that have no meaning after short integer are used as two of the floating point numbers. If you want to receive this data correctly, you must skip two bits that have no meaning after short integer data, and then float. And if the above structure becomes:

Typedef struct {

Int ID;

CHAR Name [32];

Float fval;

Short Val;

}

The Java end does not have problems in sequential sequential sequence. Therefore, it is also very important to write the correct organization of the data when writing a program. 3.4 When the data acquired from the network data stream is programmed when the C / C Socket programming, the data structure transmits and receiving data is convenient, especially when receiving data, the data type of the network data stream can be automatically obtained by the data stream. However, in Java, we must currently analyze the data, and obtain the data they need one by one, and since the network data stream is the original data stream, the network data stream is decoded according to the data type required by the program. The data is also required to encapsulate data when sending network data. This process also increases the upset of Java programs. For example, the above structure is used to obtain the corresponding data with the following code:

INT ID = bytebuf.Getint (); // Get an integer value int limit = bytebuf.limit (); // Get the limit of the byte buffer Bytebuf.limit (36); // Setting the byte buffer limit Value, the first byte position next to the string CharBuffer Charbuf = decoder.decode (bytebuf); // Decoding getting string Bytebuf.limit (limit); // Restore byte buffer original limited value float fval = Bytebuf.Getfloat (); // Get floating-point value short val = bytebuf.getshort (); // Get short integer value 4: Conclusion From the above introduction, the processing of network data streams in the Java program involves There are more problems. When writing a network program, you must pay attention to these issues to make the program correctly process the content of communication. Reference

http://java.sun.com/j2se/1.4/docs/api/index.html

About Guo Hongfeng, working in the center of Yantai Oriental Electronic Information Industry Group Corporation, has been engaged in the development and research of distributed systems.

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

New Post(0)