socket请求

来源:互联网 发布:网络语言暴力案例图片 编辑:程序博客网 时间:2024/06/05 20:08
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;
 }
原创粉丝点击