美好的一天,我从服务器发送消息后,客户端并不总是收到响应。用什么可以连接? 这是用于处理消息并从服务器发送响应的代码:
class CommunicationThread implements Runnable {
private Socket clientSocket;
private InputStream input;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
tempClientSocket = clientSocket;
try {
this.input = this.clientSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
showMessage("Error Connecting to Client!!", Color.RED);
}
showMessage("Connected to Client!!", greenColor);
}
public byte[] intToByteArray(int a)
{
byte[] ret = new byte[4];
ret[3] = (byte) (a & 0xFF);
ret[2] = (byte) ((a >> 8) & 0xFF);
ret[1] = (byte) ((a >> 16) & 0xFF);
ret[0] = (byte) ((a >> 24) & 0xFF);
return ret;
}
public byte[] hexStringToByteArray(String s) {
byte[] b = new byte[s.length() / 2];
for (int i = 0; i < b.length; i++) {
int index = i * 2;
try{
int v = Integer.parseInt(s.substring(index, index + 2), 16);
b[i] = (byte) v;
}catch(NumberFormatException ex){ // handle your exception
}
}
return b;
}
int iread = 0;
int max_read = 120;
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
boolean send = false;
String message_str = "";
byte[] content = new byte[ 2048 ];
int bytesRead = -1;
while( ( bytesRead = input.read( content ) ) != -1 ) {
baos.write( content, 0, bytesRead );
}
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
int b;
int index_for_byte_size = 0;
int length_mess = 0;
List<Byte> List_byte = new ArrayList<Byte>();
List<Byte> List_byte_for_size = new ArrayList<Byte>();
while((b=bais.read())!=-1){
if (index_for_byte_size < 4){
List_byte_for_size.add((byte) (b & 0xFF));
++index_for_byte_size;
}
else{
List_byte.add((byte) (b & 0xFF));
++length_mess;
}
}
byte[] byte_message = new byte[length_mess];
byte[] byte_message_size = new byte[4];
for (int i = 0; i < List_byte.size(); ++i){
byte_message[i] = List_byte.get(i);
}
for (int i = 0; i < List_byte_for_size.size(); ++i){
byte_message_size[i] = List_byte_for_size.get(i);
}
String string_size_Hex = bytesToHex(byte_message_size);
String string_mess_Hex = bytesToHex(byte_message);
//showMessage("Client(byte size): " + string_size_Hex, greenColor);
//showMessage("Client(mess): " + string_mess_Hex, greenColor);
String str = decodingByteArray(byte_message, byte_message_size);
//showMessage("Client(decode): " + str, greenColor);
int i2 = 0;
String TA_ID = "";
String mess_id = "";
String QR_ID = "";
String Card_type = "";
String message[] = new String[100];
for (int i = 0; i < 100; ++i){
message[i] = "";
}
for (int i = 0; i < str.length(); ++i){
if (str.charAt(i) == ';'){
++i2;
}
else{
message[i2] += str.charAt(i);
}
}
showMessage("Client(code): " + message[0], greenColor);
//There is missing code for processing responses.
.................................................
//sendMessage(encodingByteArray());
if (send == true) {
byte[] message_str2 = message_str.getBytes("windows-1251");
//sendMessage(encodingByteArray(message_str2));
DataOutputStream dOut = new DataOutputStream(tempClientSocket.getOutputStream());
dOut.write(encodingByteArray(message_str2)); // write the message
dOut.flush();
if (!tempClientSocket.isClosed()){
if (message != null){
showMessage("TA send mess!", Color.RED);
}
else{
showMessage("message null", Color.RED);
}
showMessage("Ta Disconnected", Color.RED);
showMessage("TA local ip:" + clientSocket.getLocalAddress().toString(), Color.RED);
}
else{
showMessage("socket close", Color.RED);
}
dOut.close();
}
try {
showMessage("socket close", Color.RED);
tempClientSocket.close();
} catch (IOException e) {
showMessage("socket not close", Color.RED);
// TODO Auto-generated catch block
e.printStackTrace();
}
//serverThread.interrupt();
//serverThread = null;
//serverThread = new Thread(new ServerThread());
//serverThread.start();
Thread.interrupted();
break;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
可能是什么问题?路由器是否无法到达数据? 已经尝试了一切,但我不明白原因。请帮忙,非常必要!