程序如下:
Socket socket = new Socket(host,port);
InputStream is = socket.getInputStream();
int c = 0;
StringBuffer sb = new StringBuffer();
while((c = is.read()) != -1) {
sb.append((char)c);
}
System.out.println("receive string is " + sb.toString());程序运行不到System.out.println("receive string is " + sb.toString());
好象在is.read()的时候就不动了。这段程序代码要读取以下内容(我用sniffer截取的数据)
01 5e 09 d5 bc 32 98 79 5c f0 00 0a 50 10 43 cd 7c 08 00 00
Socket socket = new Socket(host,port);
InputStream is = socket.getInputStream();
int c = 0;
StringBuffer sb = new StringBuffer();
while((c = is.read()) != -1) {
sb.append((char)c);
}
System.out.println("receive string is " + sb.toString());程序运行不到System.out.println("receive string is " + sb.toString());
好象在is.read()的时候就不动了。这段程序代码要读取以下内容(我用sniffer截取的数据)
01 5e 09 d5 bc 32 98 79 5c f0 00 0a 50 10 43 cd 7c 08 00 00
而StringBuffer是按字符读取的。
不匹配的...
换成这样:
while(is.read()!=-1)
{
c=is.read();
sb.append((char)c);
}
c=is.read();
sb.append((char)c);
}
试试这个啊
byte[] buf = in.readFully();这样接收报文更简单啊。