67,513
社区成员
发帖
与我相关
我的任务
分享
public static String getBalance(String ip, int port, String transno,
String accno) throws Exception {
Socket socket = null;
PrintStream output = null;
BufferedReader reader = null;
try {
socket = new Socket(ip, port);
if (socket.isConnected()) {
socket.setSoTimeout(10000);
OutputStream outputStream = socket.getOutputStream();
output = new PrintStream(outputStream);
output.flush();
int i = transno.length() + accno.length();
String tempnum = "";
for (int j = 0; j < 6 - String.valueOf(i).length(); j++) {
tempnum += "0";
}
tempnum = tempnum + String.valueOf(i);
String temp = tempnum + transno + accno;
output.println(temp);
output.flush();
InputStream inputStream = socket.getInputStream();
StringBuffer sb = new StringBuffer();
byte[] b = new byte[1024 * 5];
int n = -1;
while ((n = inputStream.read(b)) != -1) {
sb.append(new String(b, 0, n));
}
System.out.println(sb.toString());
output.close();
inputStream.close();
socket.close();
return sb.toString();
}
} catch (Exception e) {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e1) {
throw e1;
}
throw e;
}
return "";
}