Java XML RPC Client and server

4.3k views Asked by At

I am trying to communicate client and server in one project where my client and server both started in main() of my project having two different threads but when client try to call the answer_is function of server side it will show the below exception. When I run the client and server combined in one project I got the error

xception in thread "Thread-2" java.lang.InstantiationError: org.apache.xmlrpc.XmlRpcRequest at org.apache.xmlrpc.XmlRpcRequestProcessor.decodeRequest(XmlRpcRequestProcessor.java:82) at org.apache.xmlrpc.XmlRpcWorker.execute(XmlRpcWorker.java:143) at org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:139) at org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:125) at org.apache.xmlrpc.WebServer$Connection.run(WebServer.java:761) at org.apache.xmlrpc.WebServer$Runner.run(WebServer.java:642) at java.lang.Thread.run(Thread.java:745) Exception in thread "Thread-3" java.lang.InstantiationError: org.apache.xmlrpc.XmlRpcRequest at org.apache.xmlrpc.XmlRpcRequestProcessor.decodeRequest(XmlRpcRequestProcessor.java:82) at org.apache.xmlrpc.XmlRpcWorker.execute(XmlRpcWorker.java:143) at org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:139) at org.apache.xmlrpc.XmlRpcServer.execute(XmlRpcServer.java:125) at org.apache.xmlrpc.WebServer$Connection.run(WebServer.java:761) at org.apache.xmlrpc.WebServer$Runner.run(WebServer.java:642) at java.lang.Thread.run(Thread.java:745) JavaClient: org.apache.xmlrpc.XmlRpcException: Failed to create input stream: Unexpected end of file from server

Here is my code for Project 1 having client and server

Main Class

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);
        }
    }

}

ClientSide

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);
      }
    }
}

Server Side

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);
      } 
}

But when i wrote server side in another project and run it , it will work fine so plz tell me why client and server not run in same project

2

There are 2 answers

1
Arpit Aggarwal On

"Unexpected end of file from Server" implies that the server accepted and closed the connection without sending a response. It's possible that the system is too busy to handle the request, or that there's a bug that randomly drops connections.

In your ServerclientTest class, you are invoking: ServerThread serverthread = new ServerThread(); Thread t = new Thread(serverthread);

without implementing Runnable. Try changing the class signature as below:

public class ServerThread implements Runnable

0
Nitesh Prabhat On
 //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);
        }

    }
}