面向IBM Tivoli Monitor 6.1 Universal Agent的Socket解决方案(java语言实现)

解决方案详细情况请参考 http://blog.csdn.net/jaminwm/archive/2007/02/26/1515180.aspx

这里提供核心文件UASocketClient.java

/**
 * UASocketClient class is invoked by other clients program. 
 * Through it make a communication with UA and send data to UA.
 * @author Tony
 */
import java.io.*;
import java.net.Socket;
import java.net.InetAddress;
public class UASocketClient {
 String initPromoptMess;
 String proPromoptMess;
 String endPromoptMess;
 String errorMess;
 String hostName;
 String localName;
 String sourceName;
 String applName;
 int remotePort;
 int localPort;
 int interval;
 InetAddress hostAddress;
 OutputStream os;
 Socket sock;
 //**************************************
 //Initialize the class for invoking   //
 //**************************************
 public UASocketClient(){
  initPromoptMess="Conncting ....";
  endPromoptMess="End connecting.";
  errorMess="";
  hostName="";
  localName="";
  sourceName="";
  applName="";
  remotePort=0;
  localPort=0;
  interval=0;
  this.getLocalName();
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setApplName(String param){
  if(param.equals(""))
  {
   errorMess="ApplName is empty";
  }
  applName=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setHostName(String param){
  if(param.equals(""))
  {
   errorMess="HostName is empty";
  }
  hostName=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setLocalName(String param){
  if(param.equals(""))
  {
   errorMess="LocalName is empty";
  }
  localName=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setSourceName(String param){
  if(param.equals(""))
  {
   errorMess="SourceName is empty";
  }
  sourceName=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setRemotePort(int param){
  remotePort=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setLocalPort(int param){
  localPort=param;
 }
 //***************************************
 // Setter/Getter Methods               //
 //***************************************
 public void setInterval(int param){
  interval=param;
 }
 //***************************************
 // Getter Methods                      //
 //***************************************
 public void getLocalName(){
  try{
   hostAddress=InetAddress.getLocalHost();
  }catch(Exception e){
   errorMess="Getting localost name is wrong.";
  }
 }
 //***************************************
 // Getting ErrorMessage Method                      //
 //***************************************
 public String getErrorMess(){
  return errorMess;
 }
 //***************************************
 // Connect to the UA server            //
 //***************************************
 public boolean connect(){
  try{
   String setSourceName="//SETSOURCENAME="+sourceName+" "+"/n"+"/n";
   String setApplName=applName+"/n";
   sock = new Socket(hostName, remotePort,hostAddress,localPort);
   os = sock.getOutputStream();
            os.write(setSourceName.getBytes());
            os.write(setApplName.getBytes());
   return true;
  }catch(Exception e){
   errorMess=errorMess+e.getMessage();
   return false;
  }
 }
 //***************************************
 // Send data to the UA server          //
 //***************************************
 public boolean send(String param){
  try{
   if(os==null)
    return false;
   os.write(param.getBytes());
   return true;
  }catch(Exception e){
   errorMess=errorMess+e.getMessage();
   return false;
  }
 }
 public boolean isActive(){
  if(sock.isConnected()) return true;
  else return false;
 }
 //***************************************
 // Close connection with the UA server //
 //***************************************
 public boolean close(){
  try{
   os.flush();
   os.close();
   sock.close();
   return true;
  }catch(Exception e){
   errorMess=errorMess+e.getMessage();
   return false;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值