我尝试将十六进制字节数组发送到服务器,我也会收到字节数组响应 这是一个来源
public void sendActivationApduToServer(final String command) {
class ClientAsycTask extends AsyncTask<String, Void, String> {
ProgressDialog progress;
DataOutputStream dataOutputStream = null;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(CardReader.this);
progress.setMessage("Connection Server Please wait");
progress.setIndeterminate(true);
progress.show();
}
@Override
protected String doInBackground(String... params) {
String result = null;
Socket socket = new Socket();
DataInputStream dataInputStream;
try {
socket.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT));
dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
byte[] messages = hexStringToByteArray(command);
dataOutputStream.writeInt(messages.length);
dataOutputStream.write(messages);
int length = dataInputStream.readInt();
String response = "";
if (length > 0) {
byte[] message = new byte[length];
dataInputStream.readFully(message, 0, message.length); // read the message
response= keyrandom.bytesToHexString(message);
}
Log.e("server response",response+"server response");
socket.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("server exception",e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String s) {
if (progress.isShowing()) {
progress.dismiss();
}
Toast.makeText(CardReader.this, "Connected " + s, Toast.LENGTH_SHORT).show();
}
}
ClientAsycTask clientAsycTask = new ClientAsycTask();
clientAsycTask.execute();
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static byte[] hexStringToByteArray(String s) {
s = s.replace(" ", "");
s = s.replace("\n", "");
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
当我运行我的应用程序时,我有异常
java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
int this line
int length = dataInputStream.readInt();
任何人都可以告诉我应该是什么问题?我尝试在全局IP地址和全局端口发送字节数组,而不是本地。我测试了连接 有什么建议吗?