import java.net.*;
/**
* 与c语言通信(java做client,c/c++做server,传送一个结构)
*
* @author kingfish
* @version 1.0
*/
public class Employee {
private byte[] buf = new byte[28]; // 为说明问题,定死大小,事件中可以灵活处理
/**
* 将int转为低字节在前,高字节在后的byte数组
*/
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;
}
/**
* 将byte数组转化成String
*/
private static String toStr(byte[] valArr,int maxLen) {
int index = 0;
while(index < valArr.length && index < maxLen) {
if(valArr[index] == 0) {
break;
}
index++;
}
byte[] temp = new byte[index];
System.arraycopy(valArr, 0, temp, 0, index);
return new String(temp);
}
/**
* 将低字节在前转为int,高字节在后的byte数组
*/
private static int vtolh(byte[] bArr) {
int n = 0;
for(int i=0;i<bArr.length&&i<4;i++){
int left = i*8;
n+= (bArr[i] << left);
}
return n;
}
public String name = "";
public int id = 0;
public float salary = 0;
/**
* 将float转为低字节在前,高字节在后的byte数组
*/
private static byte[] tolh(float f) {
return tolh(Float.floatToRawIntBits(f));
}
private static Employee getEmployee(byte[] bArr) {
String name = "";
int id = 0;
float salary = 0;
byte[] temp = new byte[20];
name = toStr(bArr,20);
System.arraycopy(bArr, 20, temp, 0, 4);
id = vtolh(temp);
return new Employee(name, id, salary);
}
/**
* 构造并转换
*/
public Employee(String name, int id, float salary) {
this.name = name;
this.id = id;
this.salary = 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);
}
/**
* 返回要发送的数组
*/
public byte[] getbuf() {
return buf;
}
/**
* 发送测试
*/
public static void main(String[] args) {
try {
int index = 0;
byte[] receive = new byte[28];
int k=15;
while(k>0){
Socket sock = new Socket("127.0.0.1", 5050);
System.out.println("send content: name=kingfish "+"value="+(1+index));
sock.getOutputStream().write(new Employee("kingfish"+index, 1+index++, 10.99f + index).getbuf());
sock.getInputStream().read(receive);
Employee ee = getEmployee(receive);
System.out.println(" response: name="+ee.name+" "+"value="+ee.id);
k--;
sock.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} // end
java与c通过socket通信——传递对象与结构体
5星 · 超过95%的资源 需积分: 47 51 浏览量
2013-01-18
15:10:56
上传
评论 7
收藏 1.14MB RAR 举报
whdx666666
- 粉丝: 3
- 资源: 120
最新资源
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
- 修复Windows 10 LTSC 2021资源占用率高
- Hash工具,小巧绿色hash校验工具,免费hash工具
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 鸭子开车记(儿童绘本)
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页