android——Socket长连接

///把MyType的一些方法替换成Writer的方法
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;

  import sean.Sysout;
  import sean.business.BusinessCenter;
  import sean.business.LoginManager;
  import sean.format.MyType;
  import sean.io.Reader;
  import sean.transfer.BytesBuffer;
  import sean.transfer.DataCenter;

  public class SocketThread implements Runnable {

  String Server = "";
  int Port = 0;
  static Socket cs = null;
  // Thread ioThread=null;
  static boolean bool_SocketThread = false;
  static OutputStream output = null;

  public SocketThread(String server, int port) {
  Server = server;
  Port = port;
  bool_SocketThread = true;
  }

  @Override
  public void run() {
  // TODO Auto-generated method stub
  while (bool_SocketThread) {
  try {
  // if (cs == null) {
  DataCenter.setBool_Login(false);// 设置登录失败
  Sysout.println("正在尝试连接ClientSocket...", Sysout.TempOutDebug);
  cs = new Socket(InetAddress.getByName(Server), Port);
  if (cs != null) {
  Sysout.println("ClientSocket连接成功!__" + cs,
  Sysout.TempOutDebug);
  cs.setKeepAlive(true);//让socket保持活动状态

  InputStream input = cs.getInputStream();
  output = cs.getOutputStream();
  BusinessCenter.sendLoginData();

  BytesBuffer bBuffer = new BytesBuffer();
  byte[] Buffer = new byte[1024];
  int ReadBytes = input.read(Buffer);
  while (ReadBytes != -1) {
  Sysout.println("已读取" + ReadBytes + "个字节到缓冲区",
  Sysout.TempOutDebug);
  byte[] b = new byte[ReadBytes];
  b = MyType.BytesInsertToBytes(Buffer, b, 0);
  Reader r = new Reader(b);
  Sysout.println(r.toString() + "____ReadBytes=="
  + ReadBytes, Sysout.TempOutDebug);
  bBuffer.InsertToBuffer(Buffer, ReadBytes);
  ReadBytes = input.read(Buffer);
  }
  } else {
  Sysout.printException("ClientSocket连接失败!请确认网络正常且服务器已开启。");
  }
  // }
  // 执行到这里说明inputstream.read()已中断,说明socket已断开连接
  // cs=null;
  LoginManager.setLoginValue(-1);// 业务中心登录注销,即登录管理器注销登录
  DataCenter.setBool_Login(false);// 数据中心登录注销
  Sysout.printException(cs + "已断开。");
  Thread.sleep(2 * 1000);// 睡眠2秒后继续循环

  // try {
  // // 判断ClientSocket是否已断开
  // cs.sendUrgentData(0);
  // } catch (IOException e) {
  // // TODO Auto-generated catch block
  // Sysout.printException("ClientSocket已断开,重新连接。"+e);
  // cs.close();
  // cs = null;
  // }
  } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  Sysout.printException("SocketThread.java====解析服务器名称发生异常!" + e);
  // e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  Sysout.printException("SocketThread发生IO异常,异常消息:" + e);
  try {
  if (cs != null) {
  Sysout.println("准备关闭" + cs, Sysout.TempOutDebug);
  cs.shutdownOutput();
  cs.shutdownInput();
  cs.close();
  cs = null;
  output = null;
  LoginManager.setLoginValue(-1);// 业务中心登录注销,即登录管理器注销登录
  DataCenter.setBool_Login(false);// 数据中心登录注销
  Sysout.println(cs + "已关闭。", Sysout.TempOutDebug);
  }
  try {
  Thread.sleep(5000);
  } catch (InterruptedException e2) {
  // TODO Auto-generated catch block
  Sysout.printException("SocketThread.java====线程睡眠异常!!"
  + e2);
  // e2.printStackTrace();
  }
  String ExceptionInfos=e.toString();
  if(ExceptionInfos.endsWith("Connection refused")){
  stopSocketThread();
  }
  } catch (IOException e1) {
  // TODO Auto-generated catch block
  Sysout.printException(cs + "关闭发生异常::" + e1);
  // e1.printStackTrace();
  try {
  Thread.sleep(5000);
  } catch (InterruptedException e2) {
  // TODO Auto-generated catch block
  Sysout.printException("SocketThread.java====线程睡眠异常!!"
  + e2);
  // e2.printStackTrace();
  }
  }
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }// while(bool_SocketThread)
  Sysout.println("SocketThread已停止。", Sysout.TempOutDebug);
  }

  public static Socket getSocket() {
  return cs;
  }

  // public void setBool(boolean bool0) {
  // bool_SocketThread = bool0;
  // }

  public static OutputStream getOutputStream() {
  return output;
  }

  public static void stopSocketThread() {
  try {
  // 停止SocketThread线程,必须先把循环的标志bool_SocketThread置为false,否则可能继续循环,重新建立socket连接
  bool_SocketThread = false;
  // 关闭socket
  if (cs != null) {
  cs.shutdownOutput();
  cs.shutdownInput();
  cs.close();
  cs = null;
  output = null;

  Sysout.println("ClientSocket已被强制关闭。");
  //				LoginManager.setLoginValue(-1);// 业务中心登录注销,即登录管理器注销登录
  //				DataCenter.setBool_Login(false);// 数据中心登录注销
  //				byte[] lock=LoginActivity.getLock();
  //				synchronized(lock){
  //					lock.notify();
  //				}
  }
  } catch (IOException e) {
  // TODO Auto-generated catch block
  Sysout.printException("强制关闭" + cs + "发生异常::" + e);
  // e.printStackTrace();
  }
  }

  }

  必须先在android里启动一个服务,由服务去启动这个socket线程,因为如果是UI去启动的话,页面会卡住。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值