socket连接问题,4G网不能连接,

死不得 2017-07-05 01:27:16
有两个问题;
第一个 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");
}
}

...全文
527 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
死不得 2017-07-13
  • 打赏
  • 举报
回复
引用 1 楼 heaimnmn 的回复:
Connection reset 错误应该是服务器那边的问题吧
就是服务器的原因,服务器重启了,我这边报的错,问题是不能重新连接
哎,真难 2017-07-05
  • 打赏
  • 举报
回复
Connection reset 错误应该是服务器那边的问题吧

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧