tcpip 传输

it2022-05-05  69

 

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

cin =new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"GBK")); cout = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(),"GBK"));

 

 

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

 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(); }

 

private void startConnect() throws Exception {

logger.debug("尝试连接 " + hostIp + ":" + hostPort + "...");

InetAddress addr = InetAddress.getByName(hostIp); // server端ip地址 // 连接服务器端 clientSocket = new Socket(); clientSocket.connect(new InetSocketAddress(addr, hostPort), timeOut);

clientSocket.setSoTimeout(timeOut); // 超时时间 clientSocket.setTcpNoDelay(true); // 立即发送 clientSocket.setSoLinger(false, 0); // 关闭连接立即返回

if (clientSocket.isClosed() == false && clientSocket.isConnected() == true) logger.debug("已建立连接..."); else logger.debug("建立连接失败..."); // 获取流对象 //cin = new BufferedInputStream(clientSocket.getInputStream()); cin =new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"GBK")); cout = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(),"GBK")); }

转载于:https://www.cnblogs.com/li-xy/p/4225095.html

相关资源:TCP/IP详解 全三卷 pdf 高清 非扫描 简体中文 完整书签 完美版

最新回复(0)