java socket编程

 
引用
/** * 工行实名认证处理方法 * * @param buff * @return */ public String icbcRealname(Buffer buff) { try { /**拼装报文信息**/ log.info("#############拼装上送工行实名认证报文信息###############"); String strMessage = appendMessage(buff); if(strMessage.length() == 0){ log.info("#############拼装上送工行实名认证报文为空"); throw new Exception("实名认证不通过"); } /**获取IP端口号超时时间**/ String[] info = getMerchantInfo(buff.getMchntId()); String hostName = info[0]; int portNum = Integer.parseInt(info[1]); int delaySecond= Integer.parseInt(info[2]);//接收返回报文延时60s log.info("#############获取IP端口号超时时间:"+hostName+"#"+portNum+"#"+delaySecond); /**通过socket发送报文**/ Socket socket = null; try { socket = new Socket(hostName, portNum); if (socket == null) { // 未能得到指定的Socket对象,Socket通讯为空 log.error("##########################Socket通讯为空!"); throw new Exception("实名认证不通过"); } log.info("#############---->发送报文="+strMessage); OutputStream os=socket.getOutputStream(); os.write(strMessage.toString().getBytes("GBK")); os.flush(); BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream(),"GBK")); long sendTime = System.currentTimeMillis(); long receiveTime = System.currentTimeMillis(); String serverMessage = ""; serverMessage = in.readLine(); log.info("#############---->返回报文:"+serverMessage); receiveTime = System.currentTimeMillis(); if (serverMessage == null || (receiveTime - sendTime) > delaySecond){ // 字符串不为空,接收成功 log.error("##########################---->连接超时"); throw new Exception("实名认证不通过"); } in.close(); os.close(); socket.close(); /**对从工行接收到的报文进行截取返回码,返回信息**/ String rspCode = serverMessage.substring(4, serverMessage.indexOf("|")); String rspDesc = serverMessage.split("\\|")[1]; buff.setRspCd(rspCode); buff.setRespDesc(rspDesc); log.info("##########################---->buff.getRspCd:"+rspCode); log.info("##########################---->buff.getRespDesc:"+rspDesc); if (ErrCodeConversion.getRspToExtCode(buff.getRspCd()).equals( ServerRspCode.SUCCESS)) { buff.setTransStep(ServerConstants.TRANS_STEP_INITIAL); buff.setTransSt(ServerConstants.TRANSST_SUCCESS); buff.setTxnSysDt(ServerCommFunction.getCurrDate()); } else { log.error("###############RspCd:"+buff.getRspCd(), "实名认证不通过!"); buff.setIntRspCd(ErrCodeConversion.getRspToExtCode(buff .getRspCd())); buff.setRspCd(ErrCodeConversion.getRspToExtCode(buff .getIntRspCd())); buff.setRespDesc(ServerCommFunction.getUserRespDesc(buff, buff.getRspCd())); return ServerRspCode.SEND_REALNAME_AUTH_ERR; } } catch (UnknownHostException e) { log.error("#############---->出现未知主机错误。主机信息="+hostName+";端口号="+portNum); throw new Exception("实名认证不通过"); } catch (IOException e) { log.error("#############---->出现IO异常"); e.printStackTrace(); throw new Exception("实名认证不通过"); } catch (Exception e) { log.error("#############---->出现未知异常" + e.getMessage()); throw new Exception("实名认证不通过"); } finally { socket = null; } } catch (Exception e) { log.error(ServerRspCode.SEND_REALNAME_AUTH_ERR, "实名认证不通过!", null, e); buff.setIntRspCd(ServerRspCode.SEND_REALNAME_AUTH_ERR); buff.setRspCd(ErrCodeConversion .getRspToExtCode(ServerRspCode.SEND_REALNAME_AUTH_ERR)); buff.setRespDesc(ServerCommFunction.getUserRespDesc(buff, buff .getRspCd())); return ServerRspCode.SEND_REALNAME_AUTH_ERR; } return ServerRspCode.SUCCESS; } /** * 建行实名拼接报文 * @param buff * @return */ private String appendMessage(Buffer buff){ StringBuffer strMessage = new StringBuffer(); String len = ""; try { //个人:报文体长度(4位不足前补零)+交易码(01)|账号|姓名|身份证号码|; if(buff.getCustType().equals(ServerConstants.REALNAME_MER_PRI)){ strMessage.append(ServerConstants.REALNAME_ICBC_PRI); strMessage.append(buff.getBankAcc()).append("|"); strMessage.append(buff.getCustomerNm()).append("|"); strMessage.append(buff.getIdNo()).append("|"); len = String.valueOf(strMessage.toString().getBytes("GBK").length-ServerConstants.REALNAME_ICBC_PUB.length()); while (len.length()<4) { len = "0"+len; } strMessage.insert(0,len); } //对公:报文体长度(4位不足前补零)+交易码(03)|账号|户名|组织机构代码|; if(buff.getCustType().equals(ServerConstants.REALNAME_MER_PUB)){ strMessage.append(ServerConstants.REALNAME_ICBC_PUB); strMessage.append(buff.getBankAcc()).append("|"); strMessage.append(buff.getCustomerNm()).append("|"); strMessage.append(buff.getOrgCode()).append("|"); len = String.valueOf(strMessage.toString().getBytes("GBK").length-ServerConstants.REALNAME_ICBC_PUB.length()); while (len.length()<4) { len = "0"+len; } strMessage.insert(0,len); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return strMessage.toString(); }

你可能感兴趣的:(java,socket)