java与c/c++进行socket通信(3)
来源:互联网 发布:java dao模式 编辑:程序博客网 时间:2024/06/03 04:51
本部分提出另外一种做法, 供参考。
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
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
- java与c/c++进行socket通信(3)
- java与c/c++进行socket通信
- java与c/c++进行socket通信(1)
- java与c/c++进行socket通信(2)
- java与C进行socket通信中的通信异常问题
- java与c/c++进行socket通信的一些问题
- java与c/c++进行socket通信的一些问题
- java与c/c++进行socket通信的一些问题
- java和vb进行socket通信以及java与c/c++/oc 进行socket通信时汉字字符串编码问题解决
- c 和 java 通过 socket 进行通信
- java socket 与linux c通信问题
- java socket与C语言通信
- Java与C之间的socket通信
- Java与C之间的socket通信
- Java与C之间的socket通信
- java与c/c++进行socket通信的一些问题(1)
- java与c/c++进行socket通信的一些问题(2)
- java与c/c++进行socket通信的一些问题(1)
- Android RIL 调试分析全记录
- 用Windbg调试Silverlight应用死锁
- java与c/c++进行socket通信(2)
- 解决java虚拟机内存不足的错误 java.lang.OutOfMemoryError: PermGen space 没有文件TOMCAT_HOME/bin/catalina.sh
- jsp基础与应用
- java与c/c++进行socket通信(3)
- 摩尔盛宴的终结?(翻译自ACM Communication 2011年11月刊)
- Ext2那些事儿(一)
- ARM小键盘、LED、BEEP驱动
- 【DP】顺序对齐
- JDK切换版本
- 职责链模式(python)
- Android Google地图上overlay显示位置,路线
- android 为什么需要签名