C++服务器与java进行socket通信案例(五)

2014-11-24 09:44:19 · 作者: · 浏览: 1
Command.getID()+"command Lparam="+mCommand.getLparam());
}
public void ExecFilePro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
public void DelFilePro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());

}
public void FileInfoPro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
public void CreateDirPro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
public void GetFilePro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
public void PutFilePro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
public void GetScreenPro(Socket mSocket,NetDataCommand mCommand){
System.out.println("command ID="+mCommand.getID()+"command Lparam="+mCommand.getLparam());
}
}

2.NetDataTypeTransform.java//顾名思义:数据转换的类
[java]
import java.io.UnsupportedEncodingException;

/*
*@author: ZhengHaibo
*web: blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2012-9-25 Nanjing njupt
*/
public class NetDataTypeTransform {
public NetDataTypeTransform(){

}
/**
* 将int转为低字节在前,高字节在后的byte数组
*/
public byte[] IntToByteArray(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数组转化为int
* 将低字节在前转为int,高字节在后的byte数组

*/
public int ByteArrayToInt(byte[] bArr) {
int n = 0;
for(int i=0;i int left = i*8;
n+= (bArr[i] << left);
}
return n;
}
/**
* 将byte数组转化成String
*/
public String ByteArraytoString(byte[] valArr,int maxLen) {
String result=null;
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);
try {
result= new String(temp,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
public byte[] StringToByteArray(String str){
byte[] temp = null;
try {
temp = str.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
}
}


3.NetDataCommand.java//该类就是实现与C++服务器中COMMMAND结构体对应的java类
[java]
/*
*@author: ZhengHaibo
*web: blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2012-9-26 Nanjing njupt
*/
public class NetDataCommand {
private static final int IDLen=4;
private static