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