现在的位置: 首页 > 综合 > 正文

java与c/c++进行socket通信的一些问题(1)

2013年11月03日 ⁄ 综合 ⁄ 共 1516字 ⁄ 字号 评论关闭
   近几天看到csdn上问c/c++和java通信的问题比较多,特别是c特有的数据结构(如struct)。

特地根据网友的一个问题举个例子,希望对初学者有所帮助。

原问题见:http://community.csdn.net/Expert/topic/3886/3886989.xml?temp=.3527033

这类问题通常是为了利用原有Server或者Server不能做修改(通常是c/c++)造成。

比如Server端只接收一个结构Employee,定义如下:

struct UserInfo {
char UserName[20];
int UserId;
};
struct Employee {
UserInfo user;
float salary;
};
当然也可以定义为

struct Employee {
char name[20];
int id;
float salary;
};

java client 测试源码(为说明问题,假设struct字节对齐,sizeof(Employee)=28)

import java.net.*;

/**
* 与C语言通信(java做Client,c/c++做Server,传送一个结构)
* @author kingfish
* @version 1.0
*/
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;
}

/**
* 将float转为低字节在前,高字节在后的byte数组
*/
private static byte[] toLH(float f) {
return toLH(Float.floatToRawIntBits(f));
}

/**
* 构造并转换
*/
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);
}

/**
* 返回要发送的数组
*/
public byte[] getBuf() {
return buf;
}

/**
* 发送测试
*/
public static void main(String[] args) {
try {
Socket sock = new Socket("127.0.0.1", 8888);
sock.getOutputStream().write(new Employee("kingfish", 123456789, 8888.99f).
getBuf());
sock.close();
}
catch (Exception e) {
e.printStackTrace();
}

} //end

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

当然,也可以利用writeInt,writeFloat方法发送,但字节顺序需要改为低在前。
这个问题稍后在讨论。

【上篇】
【下篇】

抱歉!评论已关闭.