"Go" discussion on socket communication between Java and C programs

Source: Internet
Author: User

1. Background
Use sockets for interprocess communication between Java programs and C programs. This paper mainly describes the Java implementation function of the client side which communicates with C program.

1.1. Language of Use
Client side: JAVA,JVM (JDK1.3)
Server side: C,unix (Sun Solaris)

1.2. Scope of discussion
Data sending: Only the discussion of int integer series in Java, including Byte,short,int, is involved.
Data acceptance: involves Byte,short,int,long,float,double,char.

Comparison of data types for 1.3.Java and C
Type Java C
Short 2-byte 2-byte
int 4-byte 4-byte
Long 8-byte 4-byte
Float 4-byte 4-byte
Double 8-byte 8-byte
Boolean 1-bit N/A
BYTE 1-byte N/A
Char 2-byte 1-byte

2. Implement
Output stream: Sends data to the C terminal using the OutputStream stream.
Input stream: Using the DataInputStream stream to accept data from the C terminal

2.1. Data transmission
Since the DataOutputStream stream provides a "write" method for each basic data type of Java, such as Wrightshort and Wrightint, when interprocess communication (sockect communication) occurs, We always prioritize the use of dataoutputstream streams.
Below we analyze the DataOutputStream stream and its member methods:

2.1.1. DataOutputStream Flow
The DataOutputStream stream implements the interface DataOutput.
This article discusses only writebyte (int v), writeshort (int v), and writeint (int v) Parts (this is because the data we need to send involves only int,short and byte, other long,double, etc. are not described here) , and they all have a common feature, which is the unique input parameter of type int.
The functional descriptions of these member methods also provide a theoretical basis for us to manually perform byte-order conversions later.
2.1.2. Network byte order
Rule: The data transmitted on the network is uniformly used in the big endian format ("High byte in front"), which we call "network byte order".

Big endian format:
High byte low byte
1 2 3 4
Byte[0] byte[1] byte[2] byte[3] Output buffer

Therefore, regardless of the order in which the native byte order takes place, it is converted to the network byte order before being sent to the network for transmission. This is especially important when it comes to communicating between Java and C two of applications in different languages. (if communication between two Java programs may be guaranteed to accept and send in the same byte order, you can not convert the format, but this practice is not good, not good portability)

2.1.3. Data sending: Manual byte conversion/Writeint method
Take writeint (int v) as an example to describe:
Read the documentation for the dataoutput writeint (int v) method:
Using the Writeint method, you can write a 4-byte int value v to the output stream with byte order:

(byte) (0xFF & (v >>)) byte[0] High-byte
(byte) (0xFF & (v >>)) byte[1]
(byte) (0xFF & (v >> 8)) byte[2]
(byte) (0xFF & V) byte[3] Low byte
Such byte order is in the big endian format, the standard "network byte order".
However, in the actual work of the output stream using the Dataoutputstream.readint (int) method when the Write data error, you need to manually follow the above-mentioned V value to write the conversion (through the shift completion), the code is shown below the conversion, Refer to the Byteconverter.inttobyte () method in program Socketclient.java.
Static public final byte[] Inttobyte (
int value, int offset, int length, byte[] buffer)
{//High byte first on network
for (int i=0,j=length-1; i<length; i++,j--) {
if (j+offset >= 0 && J+offset < 1024) {
Buffer[j+offset] = (byte) ((Value >> i*8) & 0xFF);
} else {
System.out.println (
"Array index out of the bounds:index=" + (J+offset));
}
}
return buffer;
}


2.2. Data reception
Same as data sending, because the DataInputStream stream provides a "read" method for each basic data type of Java, such as Readshort and Readint, so that when interprocess communication (sockect communication) occurs, We always prioritize the use of datainputstream streams.
Unlike data transmission, the member method under DataInputStream is actually tested, "basically" can read the corresponding value correctly according to the data type.
But not perfect, especially when communicating with applications in different languages (such as C).

According to table 1 (comparison of data types between Java and C):
(1) The number of bytes in the long type is 4 bytes in Java and C:
Therefore, the value read by the Readlong method should be a signed right shift (4-byte) bit to get the corresponding long value in the C program.
Type Java C
Long 8-byte 4-byte

(2) because the char type in Java is 2 bytes and the char type in C is 1 bytes, you cannot use the Readchar method to read the char value in a C program.
In Java, however, the byte type is 1 bytes long, so you can use the ReadByte method to get the char value in the C program.
Type Java C
BYTE 1-byte N/A
Char 2-byte 1-byte

"Go" discussion on socket communication between Java and C programs

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.