第一个 socket服务重启,客户端不能自动连接报错Connection reset
第二个问题,,4G网打开app时发现,无法连接socket,下面是socket主要的代码:
@Override
public void run() {
InetAddress inetAddress = null;
try {
mSocket = new Socket(serverUrl, serverPort);
in = mSocket.getInputStream();
out = mSocket.getOutputStream();
mHeartTime = System.currentTimeMillis();
Log.v(SOCKET_TAG, "client socket create successed");
// 轮询发送消息列表中的数据
while (true) {
if (mMsgQueue.size() > 0) {
// 判断是否要发送心跳包
if (Math.abs(mHeartTime - System.currentTimeMillis()) > HEART_TIME)
sendHeart(new String(mMsgQueue.get(0).getBytes()));
Thread.sleep(SEND_TIME);
}
// 判断client socket 是否连接上Server
if (mSocket.isConnected()) {
Log.v(SOCKET_TAG, "### client socket connected ###");
// 发送数据
if (!mSocket.isOutputShutdown() && !isMsgQueueEmpty()) {
out.write(mMsgQueue.get(0).getBytes());
// // 将发送过的数据移除消息列表
// mMsgQueue.remove(0);
Log.i("socket2222222222", "连接上了!!!");
mHeartTime = System.currentTimeMillis();
}
} else {
// 重建连接
Log.v(SOCKET_TAG, "client socket disconnected");
if (!mSocket.isClosed()) mSocket.close();
// inetAddress = InetAddress.getByName(serverUrl);
mSocket = new Socket(serverUrl, serverPort);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
this.interrupt();
// Log.v(SOCKET_TAG,e.getMessage());
} catch (UnknownHostException e) {
e.printStackTrace();
Log.v(SOCKET_TAG, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.v(SOCKET_TAG, e.getMessage());
} finally {
if (out != null)
try {
out.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (in != null) {
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
Log.v(SOCKET_TAG, e.getMessage());
}
}
Log.v(SOCKET_TAG, "client socket close");
}
}
2 个解决方案
#1
Connection reset 错误应该是服务器那边的问题吧
#2
就是服务器的原因,服务器重启了,我这边报的错,问题是不能重新连接
#1
Connection reset 错误应该是服务器那边的问题吧
#2
就是服务器的原因,服务器重启了,我这边报的错,问题是不能重新连接