socket请求

public static String sendData2HF(String data,int busType) throws UnknownHostException, IOException {
  Log.info(null, SocketUtil.class, "send data to HF>>>>: " + data);
  String ip = HFConstant.PBOC_IP;
  int port = HFConstant.PBOC_PORT;
  Socket socket = new Socket(ip, port);
  socket.setSoTimeout(3000);
  socket.setTcpNoDelay(true);
  OutputStream os = socket.getOutputStream();
  InputStream is = socket.getInputStream();
  byte[] head = new byte[2];
  byte[] body = null;
  int busTypes = busType;
  if(busTypes==1){
   body = new byte[121];
  }else if(busTypes==2){
   body = new byte[308];
  }else if(busTypes==3){
   body = new byte[269];
  }else if(busTypes==4){
      body = new byte[148];
  }
  try {
   
   byte[] buf = CrbUtil.hexString2Ba(data);
   os.write(buf);
   os.flush();
   
   BufferedInputStream bufer = new BufferedInputStream(socket.getInputStream());
   bufer.read(head);
   //前4字节为报文长度
   String headlen = CrbUtil.ba2HexString(head);
   Log.info(null, SocketUtil.class, "receive data from HF 报文长度(不含报文总长度)<<<: " +headlen);
   bufer.read(body);
   Log.info(null, SocketUtil.class, "receive data from HF<<<: " +  CrbUtil.ba2HexString(body));
   return  CrbUtil.ba2HexString(head) + CrbUtil.ba2HexString(body);
  }catch(Exception e){
   e.printStackTrace();
   Log.error("", SocketUtil.class, "异常"+e);
  } finally {
   if (os != null) {
    os.close();
   }
   if (is != null) {
    is.close();
   }
   if (socket != null) {
    socket.close();
   }
  }
  return null;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值