Java XML RPC客户端和服务器

时间:2015-06-25 10:22:16

标签: java c++ xml-rpc

我正在尝试在一个项目中与客户端和服务器进行通信,其中我的客户端和服务器都在我的项目的main()中启动,具有两个不同的线程但是当客户端尝试调用服务器端的answer_is函数时,它将显示以下例外。当我在一个项目中运行客户端和服务器时,我收到了错误

  

线程“Thread-2”中的xception java.lang.InstantiationError:   org.apache.xmlrpc.XmlRpcRequest         在org.apache.xmlrpc.XmlRpcRequestProcessor.decodeRequest(XmlRpcRequestProcessor.java:82)         在org.apache.xmlrpc.XmlRpcWorker.execute(XmlRpcWorker.java:143)         在org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:139)         在org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:125)         在org.apache.xmlrpc.WebServer $ Connection.run(WebServer.java:761)         在org.apache.xmlrpc.WebServer $ Runner.run(WebServer.java:642)         在java.lang.Thread.run(Thread.java:745)       线程“Thread-3”中的异常java.lang.InstantiationError:org.apache.xmlrpc.XmlRpcRequest         在org.apache.xmlrpc.XmlRpcRequestProcessor.decodeRequest(XmlRpcRequestProcessor.java:82)         在org.apache.xmlrpc.XmlRpcWorker.execute(XmlRpcWorker.java:143)         在org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:139)         在org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:125)         在org.apache.xmlrpc.WebServer $ Connection.run(WebServer.java:761)         在org.apache.xmlrpc.WebServer $ Runner.run(WebServer.java:642)         在java.lang.Thread.run(Thread.java:745)       JavaClient:org.apache.xmlrpc.XmlRpcException:无法创建输入流:来自服务器的文件意外结束

以下是我的具有客户端和服务器的项目1的代码

主要类

package serverclienttest;

/**
 *
 * @author root
 */
public class ServerclientTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            ServerThread serverthread =  new ServerThread();
            Thread t = new Thread(serverthread);
            t.start();
            ClientThread clientthread = new ClientThread();
            Thread t1 = new Thread(clientthread);
            t1.start();

        } catch (Exception exception) {
            System.err.println("WebClientServer: " + exception);
        }
    }

}

客户机侧

package serverclienttest;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;

import java.util.Vector;

import java.util.*;

import java.io.*;
import java.net.*;

import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcClient;
import java.util.*;
import java.io.*;

/**
 *
 * @author root
 */
public class ClientThread implements Runnable{
    public void run()
    {
         try {
        // XmlRpcClient server = new XmlRpcClient("http://localhost/RPC2"); 
          XmlRpcClient client = new XmlRpcClient();
             XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("http://localhost:5300/RPC2"));
            config.setEnabledForExtensions(true);
         //Vector params = new Vector();
       /*  Hashtable params = new Hashtable();
       params.put(1, 1);
         params.put(2, 2);*/
         Object[] testclass = new Object[]{1,2};
         client.setConfig(config);
         int result = (Integer) client.execute("sample.sum", testclass);
         System.out.print("Client Executed");
         int sum = ((Integer) result).intValue();
         System.out.println("The sum is: "+ sum);

      } catch (Exception exception) {
         System.err.println("JavaClient: " + exception);
      }
    }
}

服务器端

public class ServerThread {   
    public ServerThread() {
        System.out.println("Handler registered as answer_is");
   }
    public Integer sum(int x, int y){

      return new Integer(x+y);
   }
   public void run()
    {
       try {
       System.out.println("Attempting to start XML-RPC Server...");
          WebServer server = new WebServer(5300);
          server.addHandler("sample", new ServerThread());
         server.start();
          System.out.println("Started successfully.");
         System.out.println("Accepting requests. (Halt program to stop.)");
        }
        catch (Exception exception){
         System.err.println("JavaServer: " + exception);
      } 
}

但是当我在另一个项目中编写服务器端并运行它时,它会正常工作,所以请告诉我为什么客户端和服务器不能在同一个项目中运行

2 个答案:

答案 0 :(得分:0)

"来自Server" 的文件意外结束意味着服务器接受并关闭连接而不发送响应。系统可能太忙而无法处理请求,或者存在随机丢弃连接的错误。

在您的ServerclientTest课程中,您正在调用: ServerThread serverthread = new ServerThread(); Thread t = new Thread(serverthread);

未实施Runnable。尝试更改类签名,如下所示:

public class ServerThread implements Runnable

答案 1 :(得分:0)

 //Server Side This will work for me
import java.io.FileWriter;
import java.io.IOException;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;


/**
 *
 * @author root
 */
 public class ServerThread implements Runnable {
  private static final String fileName = "/tmp/sample.txt";

  /** Default port. */
  private static final int defaultPort = 7777;

  /** Password. */
  private static final String password = "2isAnOddPrime";

  /** Handler name. */
  private static final String handlerName = "sample";

  public Integer sum(int x, int y) {

        return new Integer(x + y);
    }   
  public ServerThread()
  {

  }

  public ServerThread(int port) throws InterruptedException, XmlRpcException, IOException {

         System.out.println("Handler registered as answer_is");
     //   Thread.sleep(5000);
      WebServer webServer = new WebServer(port);
      XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
      PropertyHandlerMapping phm = new PropertyHandlerMapping();
      phm.addHandler(handlerName,ServerThread.class);
      xmlRpcServer.setHandlerMapping(phm);
      XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)xmlRpcServer.getConfig();
      serverConfig.setEnabledForExtensions(true);
      serverConfig.setContentLengthOptional(false);
      webServer.start();
      System.out.println("Done.\nListening on port: " + port);

      xmlRpcServer.setHandlerMapping(phm);
    }



   private void writeToFile(long prime)
  {
    try
    {
      FileWriter out = new FileWriter(fileName);
      out.write(prime + "");
      out.close();
    }
    catch (Exception e)
    {
      System.err.println("Could not write " + prime + " to file " + fileName + "\nReason: " + e.getMessage());
    }
  }

    public void run() {
       try {
            int port = 5300;
            new ServerThread(port);
        } catch (Exception exception) {
            System.err.println("JavaServer: " + exception);
        }

    }
}