Some problems with Java and CC ++ for Socket communication (2)

xiaoxiao2021-03-06  48

For the first part, please see http://blog.9cbs.net/kingfish/archive/2005/03/29/333635.aspx

This section presents another approach for reference.

Import java.net. *; import java.io. *;

/ ** * Communication with C language (Java Make Client, C / C Making Server, transmitting a structure) * @Author kingfish * @version 1.0 * / public class Employee2 {private string name; private int id; private float salary;

/ ** * Int * / private static int ticketh (int in) {int out = 0; OUT = (IN & 0xFF) << 24; OUT | = (in & 0xff00) << 8; OUT | = (in & 0xFF0000) >> 8; OUT | = (in & 0xFF000000) >> 24; Return Out;}

/ ** * turn float into low byte in int * / private static int tolh (float.floattorawintbits (f));} (f));}

/ ** * Construct and convert * / public Employee2 (String Name, Int, Float Salry) {this.name = name; this.iD = ID; this.salary = Salary;}

/ ** * Name, fixed length Byte array * / public byte [] getName () {byte [] b = new byte [20]; system.Arraycopy (Name.getbytes (), 0, b, 0, name. Gettes (). Length); Return B;

/ ** * acquisition number (low byte) * / public int getId () {Return Tolh (ID);}

/ ** * acquisition (low byte before) * / public int getsalary () {Return Tolh (Salary);}

/ ** * Send test * / public static void main (string [] args) {try {Employee2 P = new Employee2 ("Kingfish", 123456789, 8888.99f);

Socket Sock = New Socket ("127.0.0.1", 8888); DataOutputStream Dos = New DataOutputStream (Sock.getOutputStream ()); dos.write ()); dos.writeint ()); Dos.WriteInt (p.getsalary ()); sock.close ();} catch (exception e) {E.PrintStackTrace ();}}} // end

-------------------------------------------------- -------------------------------------------------- - If there is any problem, please correct it!

Kingfish2005.3.30

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

New Post(0)