【高分求救】关于一个socket客户端程序的错误----求教高手!
我写了一个客户端 发送一个16进制的命令字符串到服务器,服务器做出一定处理返回给我一个字符串
可是现在情况是:如果我发送字符串格式符合要求,返回的就是“message length error”;如果格式不对,程序就死在read()这里了,请教大虾 帮我看看这个程序 在线等待~~
程序中如果有什么明显是新手做法的地方 也请指教更好的做法!
protected Socket socket;
protected InputStream in;
protected OutputStream out ;
protected String cmd="W21K0001DFB4524F78948341000531106039989;010120041014075300-L02;2004-06-25;28347.65";
String outStr = new String();
public Decoder(){
try{
if(getConn()){
System.out.println("connected");
int iSize=cmd.length(); //取得发送字符串长度
char[] inputChar=new char[iSize+2] ;
tools.intC(inputChar,iSize); //字符串长度高低位反转 以2个字符的形式写入数组前2个位置
cmd.getChars(0,iSize,inputChar,2);//将发送字符串以字符形式写入char数组
out =socket.getOutputStream();
in = socket.getInputStream();
InputStreamReader iw = new InputStreamReader(in);
BufferedReader br=new BufferedReader(iw);
OutputStreamWriter ow = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(ow,inputChar.length);//指定发送缓冲区长度
//发送
for(int i=0;i<inputChar.length;i++){
bw.write(inputChar[i]);
};
bw.flush();
String userInput;
System.out.println("--读取数据中--");
//读取
int num=tools.returnNum(br.read(),br.read());//读接受到的前2个字符 转化为1个整数
socket.setReceiveBufferSize(num); //用来判断接受到的消息长度
char[] rs= new char[num];
br.read(rs,0,num);
for(int i=0;i<num;i++){
outStr+=rs[i];
}
System.out.print("--返回数据为--"+outStr);
System.out.println();
//打印结果出来
}else{
System.out.println("can't connected to the server");
}
//关闭连接
if(closeConn()) System.out.println("connection has been closed");
}catch(IOException e){
System.out.println("Error1:"+e);
closeConn();
}
}