我有 XML-RPC 服务器,我相信它工作正常。我可以使用 telnet 连接到服务器,也可以使用 Web 浏览器,地址是 (dracek:8080) dracek=计算机名称。当我尝试使用我的客户端连接时,我收到此错误:
org.apache.xmlrpc.XmlRpcException:无法读取服务器的响应:连接被拒绝:连接
服务器应该记录“客户端已连接。”,但没有任何反应。
服务器代码:
公共类主要{
private static int port = 8080;
private static InetAddress ipAddress = null;
public static void main(String[] args) {
try
{
ipAddress = InetAddress.getLocalHost();
}
catch (UnknownHostException e)
{
System.out.println("UnknownHostException");
System.exit(0);
}
System.out.println("Starting server.");
try {
WebServer webServer = new WebServer(port, ipAddress);
// get XML-RPC Server
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("DBClass", DBClass.class);
// mapping
xmlRpcServer.setHandlerMapping(phm);
// configuration
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer
.getConfig();
serverConfig.setEnabledForExceptions(true);
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
// start server
webServer.start();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("Server succesfully started. IP:" + ipAddress + " Port:" + port + ".");
}
数据库类:
public void testConnection(){
System.out.println("Client connected.");
}
客户端代码:
private static InetAddress ipAddress = null;
private static int port = 8080;
private static String confFile = "";
private static XmlRpcClientConfigImpl config;
public static void main(String[] args) {
System.out.println("Starting client.");
if (args.length == 3) {
try {
ipAddress = InetAddress.getByName(args[0]);
port = Integer.parseInt(args[1]);
confFile = args[2];
}
catch (NumberFormatException e) {
System.out.println("Port number is not valid!");
System.exit(0);
}
catch (UnknownHostException e)
{
System.out.println("UnknownHostException");
System.exit(0);
}
}
else
{
System.out.println("Some parameter is missing!");
System.out.println("Valid parameters are: IP(0) Port(1) Config_file_name(2).");
}
Client.config = new XmlRpcClientConfigImpl();
try
{
Client.config.setServerURL(new URL("http://" + ipAddress + ":" + port)); // nastaveni
}
catch(MalformedURLException me)
{
System.out.println("Server URL is not valid!");
System.exit(0);
}
Client.config.setEnabledForExtensions(true);
Client.config.setEnabledForExceptions(true);
Client.config.setConnectionTimeout(60 * 1000);
Client.config.setReplyTimeout(60 * 1000);
XmlRpcClient client = new XmlRpcClient();
client.setTransportFactory(new XmlRpcSunHttpTransportFactory(client));
client.setConfig(Client.config);
System.out.println("Connnected to " + ipAddress + " : " + port + ".");
Tools tools = new Tools(client);
tools.testConnection();
}
工具类:
public class Tools {
private XmlRpcClient client;
public Tools(XmlRpcClient client)
{
this.client = client;
}
public void testConnection() {
Object[] params = new Object[] {};
try
{
this.client.execute("DBClass.testConnection", params);
}
catch (XmlRpcException e)
{
System.out.println("Error occured when testing connection!");
System.out.println(e.toString());
}
}
我使用命令运行客户端:
java -Djava.security.policy=..\Client\src\client\Client.policy -jar ..\Client\dist\Client.jar dracek 8080 ..\data\commands.txt
致以最诚挚的谢意