首页 > 代码库 > tcp/ip 传输

tcp/ip 传输

 

 // socket读取流
 protected BufferedReader cin = null; // socket读取流
 // socket发送流
 protected PrintWriter cout;

/***************************************************************************   * 发送信息到 建设银行   **************************************************************************/

 public static String sendMsgJSYH(String msg, String sydwId)    throws FESException {

  String message = "";  

 // 获取服务IP   String serviceIp= "";

  // 获取服务端口   int port=0;

  try {    

// 获取服务IP    serviceIp =  FESUrlConfig.getFESConfig().getItCCBIp();

//   serviceIp="172.16.17.24";    

// 获取服务端口    port =  FESUrlConfig.getFESConfig().getItCCBPort();

//   port=8000;  

  SocketClients sc = new SocketClients(serviceIp, port);   

 log.error("建设银行服务(IP:" + serviceIp + " 端口:" + port );  

  message = sc.sendXmlMsgToJSYHLS(msg.getBytes());   

} catch (ConnectException e) {

   log.error("建设银行服务(IP:" + serviceIp + " 端口:" + port + ")连接失败!"      + e.getMessage());

   throw new FESException(e);   

} catch (SocketTimeoutException e) {

    log.error("建设银行服务(IP:" + serviceIp + " 端口:" + port + ")响应超时!"      + e.getMessage());  

   throw new FESException(e);   

} catch (Exception e) {  

  log.error(e.getMessage(), e);   

}  

 return message;  }  

 

 

方法2:

public String sendXmlMsgToJSYHLS(byte[] sendMsg) throws Exception {   

  // 建立连接   this.startConnect();

  logger.info("\nSend message:\n"+ new String(sendMsg));  

 // 发送消息   this.sendBuffer(sendMsg);

  // 接收消息   byte[] responseMsg = this.recieveBufferYH();  

 // 关闭连接   this.shutdownConnection();  

 String msgRes =new String(responseMsg);

  return msgRes;

 }

 

/**
  * @description 发送信息
  * @author fei_yfan
  * @param bufferString
  *            发送消息内容
  * @throws IOException
  */
 public void sendBuffer(byte[] bufferString) throws IOException {
  cout.write(new String(bufferString));
  cout.flush();
 }

 

 

public byte[] recieveBufferYH() throws Exception {
  StringBuffer  sb = new StringBuffer();
  String s ="";
  //从文件中按字节读取内容,到文件尾部时read方法将返回-1
  while((s=cin.readLine())!= null){
   sb.append(s+"\n");
  }
  return sb.toString().getBytes();
 }

 

tcp/ip 传输