Java communication with C and C ++

Source: Internet
Author: User

In recent days, I have seen many questions about communication between C/C ++ and Java on csdn, especially the data structures (such as struct) Unique to C ).
I would like to give an example based on a netizen's question, hoping to help beginners.
Original problem see: http://community.csdn.net/EXPert/topic/3886/3886989.XML? Temp =. 3527033.
This type of problem is usually caused by the inability to modify the original server or server (usually C/C ++.
For example, the server receives only one structure employee, which is defined as follows:
Struct userinfo {
Char username [20];
Int userid;
};
Struct employee {
Userinfo user;
Float salary;
};
Of course, it can also be defined
Struct employee {
Char name [20];
Int ID;
Float salary;
};
Java client test source code (for illustration, assuming struct byte alignment, sizeof (employee) = 28)
Import java.net .*;
/**
* Communication with C language (Java serves as the client, C/C ++ serves as the server, and transmits a structure)
* @ Author Kingfish
* @ Version 1.0
*/
Class employee {
Private byte [] Buf = new byte [28]; // determines the size of the problem and flexibly handles the event.
/**
* Convert the int type to the byte array after the low byte type and the high byte type.
*/
Private Static byte [] tolh (int n ){
Byte [] B = new byte [4];
B [0] = (byte) (N & 0xff );
B [1] = (byte) (N> 8 & 0xff );
B [2] = (byte) (N> 16 & 0xff );
B [3] = (byte) (N> 24 & 0xff );
Return B;
}
/**
* Convert float to the byte array with the front and back of the low byte
*/
Private Static byte [] tolh (float f ){
Return tolh (float. floattorawintbits (f ));
}
/**
* Construct and convert
*/
Public Employee (string name, int ID, float salary ){
Byte [] temp = Name. getbytes ();
System. arraycopy (temp, 0, Buf, 0, temp. Length );
Temp = tolh (ID );
System. arraycopy (temp, 0, Buf, 20, temp. Length );
Temp = tolh (salary );
System. arraycopy (temp, 0, Buf, 24, temp. Length );
}
/**
* Returns the array to be sent.
*/
Public byte [] getbuf (){
Return Buf;

}
/**
* Send Test
*/
Public static void main (string [] ARGs ){
Try {
Socket sock = new socket ("127.0.0.1", 8888 );
Sock. getoutputstream (). Write (new employee ("Kingfish", 123456789,888 8.99f ).
Getbuf ());
Sock. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
} // End
---------------------------------------------------------------------------
Of course, the writeint and writefloat methods can also be used for sending, but the byte order needs to be changed to lower.
This issue will be discussed later.
If you have any questions, please correct them!

Body:

See http://blog.csdn.net/kingfish/archive/2005/03/29/333635.aspx FOR THE FIRST PART

This section provides another method for reference.

Import java.net .*;
Import java. Io .*;

/**
* Communication with C language (Java serves as the client, C/C ++ serves as the server, and transmits a structure)
* @ Author Kingfish
* @ Version 1.0
*/
Public class employee2 {
Private string name;
Private int ID;
Private float salary;

/**
* Convert the int value to the int value after the low byte and the int value after the high byte.
*/
Private Static int tolh (INT in ){
Int out = 0;
Out = (in & 0xff) <24;
Out | = (in & 0xff00) <8;
Out | = (in & 0xff0000)> 8;
Out | = (in & 0xff000000)> 24;
Return out;
}

/**
* Convert float to the int with the low byte before and the high byte after
*/
Private Static int tolh (float f ){
Return tolh (float. floattorawintbits (f ));
}

/**
* Construct and convert
*/
Public employee2 (string name, int ID, float salary ){
This. Name = Name;
This. ID = ID;
This. Salary = salary;
}

/**
* Get the name and set the length of the byte array
*/
Public byte [] getname (){
Byte [] B = new byte [20];
System. arraycopy (name. getbytes (), 0, B, 0, name. getbytes (). Length );
Return B;
}

/**
* Get the number (low byte before)
*/
Public int GETID (){
Return tolh (ID );
}

/**
* Get the salary (low byte before)
*/
Public int getsalary (){
Return tolh (salary );
}

/**
* Send Test
*/
Public static void main (string [] ARGs ){
Try {
Employee2 P = new employee2 ("Kingfish", 123456789,888 8.99f );

Socket sock = new socket ("127.0.0.1", 8888 );
Dataoutputstream dos = new dataoutputstream (sock. getoutputstream ());
Dos. Write (P. getname ());
Dos. writeint (P. GETID ());
Dos. writeint (P. getsalary ());
Sock. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
} // End

Bytes -----------------------------------------------------------------------------------------------------

If you have any questions, please correct them!

Kingfish
2005.3.30

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.