扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
解决方案详细情况请参考 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;
}
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者