日期:2014-05-20 浏览次数:21414 次
public String send(String Command) throws Exception {
String ret="";
Socket soc = new Socket(getURL(), getPORT());//getURL=192.168.1.168;getPORT=4016
System.out.println("Connect Telnet:" + getURL() + "-" + getPORT());
DataInputStream din = new DataInputStream(soc.getInputStream());
DataOutputStream dout = new DataOutputStream(soc.getOutputStream());
dout.writeUTF(Command);
System.out.println("send:" + Command);
int re=0;
while (re != -1) {
re = din.readByte();//read();readInt();readUTF();方法都试了结果都不对
System.out.println("int" + re);
Character c = new Character((char) re);//这就开始乱码了
System.out.println("char:" + c);
ret = ret.concat(c.toString());
}
System.out.println("reply:" + ret);
din.close();
dout.close();
soc.close();
return ret;
}
StringBuffer sb = new StringBuffer();
int len = 0;
byte[] buffer = new byte[4096];
while ((len = din.read(buffer)) != -1) {
sb.append(new String(buffer,0,len,"UTF-8"));
}
System.out.println(sb.toString());