我正在尝试使tcp上的连接保持活动状态,为此,我正在使用套接字的静态实例。我的服务器保持其连接处于活动状态,并将响应写入套接字。这是客户端代码。但是我陷入了读取输入流的循环中。有人可以建议更好的方法吗? 我正在发布我正在使用的代码。
public class ConnectionManager {
private String m_Response = "";
byte[] m_RequestData;
boolean m_Read_Response_Completed = false;
Thread l_WorkerThread;
ConnectionListener m_ConnectionListener;
boolean m_IsFetchCompleted;
Context mContext;
private static boolean isNeedToStop = false;
public ConnectionManager(Context context, ConnectionListener f_LoginListener) {
mContext = context;
m_ConnectionListener = f_LoginListener;
m_IsFetchCompleted = false;
}
public String request(byte[] f_RequestData) throws Exception {
m_RequestData = f_RequestData;
l_WorkerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
if (NetworkUtils.m_socket == null || !NetworkUtils.m_socket.isBound() || NetworkUtils.m_socket.isClosed() /*|| Constants.IS_SERVER_CONFIG_CHANGED*/) {
NetworkUtils.m_socket = new Socket("IP", PORT);
NetworkUtils.m_socket.setKeepAlive(true);
}
if (m_RequestData == null) {
m_Read_Response_Completed = true;
if (!NetworkUtils.m_socket.isClosed()) {
m_ConnectionListener.notifyReadCompleted("Connected");
// return;
} else {
m_ConnectionListener.notifyReadCompleted("Disconnected");
// return;
}
} else {
Log.i(encodePackets(new String(m_RequestData)), "---->Request");
}
int bytesRead = 0;
ArrayList<String> str = new ArrayList<String>();
while (!isNeedToStop) {
if (NetworkUtils.m_socket != null) {
/*if (NetworkUtils.m_socket.getInputStream().available() != 0) {*/
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1048576);
byte[] buffer = new byte[1048576];
while ((bytesRead = NetworkUtils.m_socket.getInputStream().read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
m_Response = byteArrayOutputStream.toString();//JSON ARRAY
m_ConnectionListener.notifyReadCompleted(m_Response);
try {
Thread.sleep(500);
} catch (Exception e) {
Log.i(e);
}
}
}
}
//}
} catch (
UnknownHostException e)
{
Log.i(e.getMessage(), "");
m_ConnectionListener.notifySocketError(true);
NetworkUtils.m_socket = null;
} catch (
SocketTimeoutException e)
{
Log.i(e.getMessage(), "");
m_ConnectionListener.notifySocketError(false);
NetworkUtils.m_socket = null;
e.printStackTrace();
} catch (
SocketException e)
{
Log.i(e.getMessage(), "");
m_ConnectionListener.notifySocketError(true);
NetworkUtils.m_socket = null;
e.printStackTrace();
} catch (
IOException e)
{
Log.i(e.getMessage(), "");
m_ConnectionListener.notifySocketError(true);
NetworkUtils.m_socket = null;
e.printStackTrace();
}
}
});
l_WorkerThread.start();
return m_Response;
}
}
public interface ConnectionListener {
void notifyReadCompleted(String f_Response);
void notifySocketError(boolean isServerDown);
void onReceivePacket(int total, int current);
}
public class NetworkUtils {
public static Socket m_Socket;
public static void resetSocket() {
if (m_Socket != null) {
try {
m_Socket.close();
m_Socket = null;
} catch (IOException e) {
Logger.log(e);
e.printStackTrace();
}
}
}
}
任何帮助将不胜感激。