请高手指点c和java之间的socket通信
我写的程序是用java做客户端,向服务端发送请求,服务端是用c写的。服务端返回大量的数据信息,客户进行接收分段截取。然后用到了一个定时机制,即每天定时发送请求。现在遇到了问题,第一次请求接收到的数据是正常的,但是第二次请求开始接收到的数据就会出现空格状的方块,字节数就被空格占用了,我在解析就会出错。实在是搞不清楚是怎么回事,流也正常关闭了。
附解析代码如下
StringBuffer sb = new StringBuffer();
InputStream is = sc.socket().getInputStream();
byte[] bb = new byte[5];
//先从流中读5个字节
is.read(bb);
String xym = new String(bb,"GB18030");
sb.append(xym);
//第二次从流中读12个字节
bb = new byte[12];
is.read(bb);
int times = Integer.parseInt(new String(bb,"GB18030").trim());
sb.append(new String(bb,"GB18030"));
bb = new byte[334];
//循环读流中的字节,每次都334个
for(int i=0;i<times;i++){
byte[] bt = new byte[334];
is.read(bt);
fo.write(bt);
sb.append(new String(bt,"GB18030"));
}
关闭方法
public void close() throws IOException {
sc.finishConnect();
//sc.socket().getInputStream().close();
sc.socket().close();
sc.close();
}