socket 请求 超时重试


public class ZzSocketClientUtil {
 public static String water_no = "";
 static final Map<Integer, Integer> MAPS = new HashMap<Integer,Integer>();
 // socket超时重试时间
 static final int TIMEOUT = Constant.socketTimeOut;
 static {
  MAPS.put(0, 0);
  MAPS.put(1, Constant.socketRetry);
  
 }
 
 public static void main(String[] args) {
  int times = “”

  String ip = "";
  int port = ;

  times = 1;
  byte[] dt1 = null,dt2 = null,dt3 = null,dt4 = null,dt5 = null,dt6 = null,dt7 = null;

  try {
   
   dt1 = ZzSocketClientUtil.sender(times, ip, port, init6000());
   
   //System.out.println("End : " + new String(dt1, 7, dt1.length - 7));
  } catch (Exception ex) {
   Logger.getLogger(ZzSocketClientUtil.class.getName()).log(Level.SEVERE,nu


 public static byte[] sender(int times, String hostIP, int port,byte[] content) {
  System.gc();
  if (times <= 0 || content == null) {
   System.out.println("times <= 0 || content == null");
   return null;
  }
  byte[] retData = null;
  byte[] returnData = null;
  int bodyCounter = 0;
  int bodyLength = 0;

  Socket client = null;
  SocketAddress socketAddress = null;
  InputStream input = null;
  OutputStream output = null;

  try {
   int result = 0;
   boolean flag = false;
   do {
    Thread.sleep(ZzSocketClientUtil.MAPS.get(result));
    result++;
    try {
     socketAddress = new InetSocketAddress(hostIP, port);
     client = new Socket();
     client.connect(socketAddress, ZzSocketClientUtil.TIMEOUT);
     flag = true;// 正常连接
     result = 3; // 跳出循环
    } catch (IOException e) {
     Log.error(null, TaskTaskProcess.class, "socket请求:[IP=" + hostIP + ",PORT=" + port + "]失败", e);
    }
   } while (result <= 1);

   if (!flag)
    return returnData;
   
   input = client.getInputStream();
   output = client.getOutputStream();

   for (int a = 0; a < times; a++) {// loop test
    bodyCounter = 0;
    output.write(content);
    output.flush();

    byte[] head = new byte[7];
    for (int i = 0; i < 7; i++) {
     head[i] = (byte) input.read();
    }
    bodyLength = ((head[5] & 0X000000FF) << 8 | (head[6] & 0X000000FF)) - 7;
    retData = new byte[bodyLength];

    for (int tt = 0; tt < bodyLength; tt++) {
     ++bodyCounter;
     retData[tt] = (byte) input.read();
    }

    if (bodyCounter != bodyLength) {
     System.out.println(a + " :: asdfasdfsdf :: bodyLength :: "
       + bodyLength + " ## bodyCounter :: " + bodyCounter);
     System.out.println(a + " :: " + new String(retData));
     break;
    } else {
     returnData = new byte[bodyLength + 7];
     System.arraycopy(head, 0, returnData, 0, 7);
     System.arraycopy(retData, 0, returnData, 7, bodyLength);
     System.out.println(a + " - " + new String(retData));
    }
   }
  } catch (Exception e) {
   Log.error(null, TaskTaskProcess.class, "请求失败",e);
  } finally {
   try {
    if (client != null) {
     client.close();
    }
    input = null;
    output = null;
    client = null;
   } catch (IOException e) {
   }
   System.gc();
  }
  return returnData;
 }
 

 public static byte[] getByte(byte[] head, byte[] body) {
  byte[] tempLen = intToWord(7 + body.length);

  head[5] = tempLen[0];
  head[6] = tempLen[1];
  byte[] sendData = new byte[head.length + body.length];
  System.arraycopy(head, 0, sendData, 0, head.length);
  System.arraycopy(body, 0, sendData, head.length, body.length);
  return sendData;
 }
 public static byte[] getByteBank(byte[] head, byte[] body, byte[] foot) {
  byte[] tempLen = intToWord(7 + body.length+foot.length);
  head[5] = tempLen[0];
  head[6] = tempLen[1];
  byte[] sendData = new byte[head.length + body.length+foot.length];
  System.arraycopy(head, 0, sendData, 0, head.length);
  System.arraycopy(body, 0, sendData, head.length, body.length);
  System.arraycopy(foot, 0, sendData, head.length+body.length, foot.length);
  return sendData;
 }

 private static final byte[] intToWord(int i) {
  byte[] word = new byte[2];
  word[0] = (byte) (i >>> 8 & 0xFF);
  word[1] = (byte) (i & 0xFF);
  return word;
 }
 
 public static byte[] hexStringToBytes(String hexString) { 
      if (hexString == null || hexString.equals("")) { 
          return null; 
      } 
      hexString = hexString.toUpperCase(); 
      int length = hexString.length() / 2; 
      char[] hexChars = hexString.toCharArray(); 
      byte[] d = new byte[length]; 
      for (int i = 0; i < length; i++) { 
          int pos = i * 2; 
          d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); 
      } 
      return d; 
  }
 public static byte charToByte(char c) { 
      return (byte) "0123456789ABCDEF".indexOf(c); 
  }
 
 
 //byte转成C
 public static char[] getChars (byte[] dt1) {
       Charset cs = Charset.forName ("UTF-8");
       ByteBuffer bb = ByteBuffer.allocate (dt1.length);
       bb.put (dt1);
       bb.flip ();
       CharBuffer cb = cs.decode (bb);
  
    return cb.array();
 }

}

发布了23 篇原创文章 · 获赞 9 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览