|
解决方案详细情况请参考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;
}
}
} |
|
|
|
|
|
|