国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

java與c/c++進行socket通信的一些問題(2)

2019-11-18 14:38:36
字體:
來源:轉載
供稿:網友

  本部分提出另外一種做法, 供參考。


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

/**
* 與C語言通信(java做Client,c/c++做Server,傳送一個結構)
* @author kingfish
* @version 1.0
*/
public class Employee2 {
PRivate String name;
private int id;
private float salary;

/**
* 將int轉為低字節在前,高字節在后的int
*/
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;
}

/**
* 將float轉為低字節在前,高字節在后的int
*/
private static int toLH(float f) {
return toLH(Float.floatToRawIntBits(f));
}

/**
* 構造并轉換
*/
public Employee2(String name, int id, float salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

/**
* 取得名字,定長byte數組
*/
public byte[] getName() {
byte[] b = new byte[20];
System.arraycopy(name.getBytes(), 0, b, 0, name.getBytes().length);
return b;
}

/**
* 取得編號(低字節在前)
*/
public int getId() {
return toLH(id);
}

/**
* 取得工資(低字節在前)
*/
public int getSalary() {
return toLH(salary);
}

/**
* 發送測試
*/
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(p.getName());
dos.writeInt(p.getId());
dos.writeInt(p.getSalary());
sock.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
} //end

-----------------------------------------------------------------------------------------------------

如有任何問題,請指正!

kingfish

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 手机| 兰西县| 南澳县| 通许县| 巫溪县| 锡林浩特市| 房产| 繁昌县| 义马市| 小金县| 涟源市| 横山县| 新疆| 琼海市| 洪江市| 赞皇县| 神木县| 于都县| 平遥县| 雷山县| 兴国县| 永宁县| 平原县| 张掖市| SHOW| 定边县| 沂南县| 盐城市| 海宁市| 瑞安市| 黄大仙区| 宜阳县| 榆社县| 南溪县| 瑞昌市| 洛阳市| 洛阳市| 肥东县| 额济纳旗| 博野县| 阿克陶县|