Remoting (English)

Přejít na: navigace, hledání

Often it is necessary to communicate among systems located on different computers. You may need to communicate with systems implemented with different programming languages and running on different platform - still you need to be able to access them remotely.

Communication Types

  • Terms synchronicity
    • Synchronous - the caller stops and waits until it gets the answer
    • Asynchronous - the calling party continues to work while waiting for the answer
  • Terms secure message delivery
    • Transient
    • Persistent

On the Internet the normal HTTP traffic is transient - if the target server is not running or is unavailable, the communication will simply fail. For some applications, especially the so-called enterprise applications, this is unacceptable. To have a persistent communication we can use specialized systems creating queues. For example, IBM MQSeries, that exists in the JavaEE universal interface called JMS - Java Message Service.

The most proper type of communication depends on the type of application.

Network Communication for application developers

There is a specific set of protocols that are used nowadays over the Internet Internet protocols, other protocols (eg, Infiniband) are only for specialized applications. Internet Protocols not exactly layers, see ISO / OSI model.


Data communication over the Internet is through the Internet Protocol (IP ) sending datagrams from one IP to the other.

Currently there are two versions of IP, ​'IPv4' uses 32-bit addresses written in the form of four decimal numbers separated by dots, such as and IPv6 that uses 128-bit addresses as eight groups of four hexadecimal digits separated by colons , such as 2001:718:801:130:216:3eff:fe02:403. IPv4 addresses are exhausted [ third February 2011 ]. An IP address can be modeled by a Java class

IP addresses are difficult to remember, so the service DNS (Domain Name System) provides the translation to names such as the addresses shown above that include the name

The IP communication protocol is unreliable: datagrams may be lost or arrive in a different order than they were sent.


Reliable data delivery in the correct order is ensured by the TCP (Transmission Control Protocol) protocol that delivers the packets between pairs of points specified IP address and port number e.g. from to .

TCP uses confirmation packets after a certain number of sent packets. Therefore, TCP cannot utilize the full bandwidth of the network , and does not guarantee a delivery time. Therefore, TCP is not suitable for delivering real-time data such as voice data.

The program interface is called TCP [ socket] and uses the client-server model . TCP server listens on one or more IP addresses on a particular port. The connection is initiated by the client, that connects to the server. Normally, the server responds by transferring connection to another port and the main port is still waiting for an incoming connection .

Server side, to use TCP we can use the class and the client-side class {

There are many applications built on top of TCP. There is a set of well known port, e.g. 80 for HTTP, 443 for SSL, 25 for SMTP, etc...

TCP Server in Java

 * Simple one-thread TCP server in Java.
public class TcpServer {
    public static final int SERVER_PORT = 65432;
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
        while (true) { //démon nikdy neumírá
            System.out.println("Čekám na spojení na "+serverSocket.getInetAddress().getHostAddress()+":"+serverSocket.getLocalPort());
            //čeká na příchozí spojení
            Socket socket = serverSocket.accept();
            System.out.println("Připojil se klient z "+socket.getInetAddress()+":"+socket.getPort());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"),true);
            out.println("Server Vás vítá !");
            String line = null;
            //obsluha dokud klient neuzavře spojení, nebo nepožádá o uzavření slovem "exit"
            while((line=in.readLine())!=null) {
                System.out.println("Přišel řádek '"+line+"'");
                if("konec".equals(line)) {
                    out.println("Děkujeme, přijďte zas.");
                out.println("Zvětšeno: " + line.toUpperCase());
            System.out.println("Ukončuji spojení s "+socket.getInetAddress()+":"+socket.getPort());

TCP Client in Java

 * Simple TCP client. Uses HTTP protocol.
public class TcpClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("", 80);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"), false);
        out.println("GET / HTTP/1.0");
        String line = null;
        while ((line = in.readLine()) != null) {

NAT and Firewalls

Communication via TCP/IP connection between two machines may not always be possible.

One problem is due to the so-called firewalls that for safety reasons block traffic unless it is explicitly allowed. Typically it is allowed to operate outside of the local network by using the protocols HTTP and SMTP for web and mail servers.

The second problem is the so-called NAT (Network Address Translation) when the computer does not have a publicly accessible IP address but private address ranges like 192.168.X.x,172.16.X.x to 172.31.x.x and 10.x.x.x. NAT is performed on equipment that connects the local network to the Internet, typically the home WiFi router or a corporate firewall . NAT replaces the private IP datagrams with the addresses for public and vice versa.


Communication via TCP/IP is not encrypted, it can easily eavesdropped or forged. Security must be addressed at the application layer.

Security must be addressed in all applications communicating over a public network, because the state of the Internet today resembles the Middle Ages - roads are full of highwaymen and cities around the walls (firewalls).

Usually the solution is SSL/TLS (Secure Sockets Layer / Transport Layer Security), in which the communication is encrypted with a symmetric cipher, and to establish a connection to the server or both ends, the sides must be identified by their X509 certificates signed by trusted certification authorities.

In Java, SSL/TLS is represented in the package​​, especially by the class and certificates are in the package, especially the class with for the link to a trusted certification authority.

Other useful classes are javax.crypto.Mac for MAC (Message Authentication Code) and for hashing functions such as MD5 and SHA.

Remote Calls - RPC and RMI

RPC (Remote Procedure Call) and RMI (Remote Method Invocation ) are ways for developers to call code on a remote machine. The difference is in the object orientation aspect - RPC assumes only the existence of remote procedures whereas RMI presupposes distant objects remotely to be called with methods. In both cases, the programmer calls the remote code as as if it were local.

Example implementations of RPC in Java can be done with SOAP, XML-RPC and Hessian. These solutions enable communication systems created in any language.

Examples of implementations of distributed object systems are: CORBA, DCOM, Java RMI.

Systems with that enable communication between systems written in different programming languages ​​need to have an intermediate definition of the funtion calls:

  • CORBA has called IDL - Interface Definition Language
  • SOAP has the WSDL - Web Service Definition Language.

Then there are tools to generate interfaces from the description of the so-called client stubs , that is the code for the selected programming language callable locally. The code ensures

  • Marshalling/serialization of names and parameters of the called function or method from the client program memory in a form that is portable across the network
  • Communication with the remote server
  • Demarshaling/deserialize returning values ​​back to the client

Java RMI

In the core libraries of the Java SE (starting from version 1.3) there is an integrated solution called Java RMI . See Java Remote Method Invocation.

It is a mix of the two technologies: the original RMI, that is tied to Java, and uses a special binary transmission format, and CORBA (Common Object Request Broker Architecture) , which - at least in theory - allows remote calls between objects and different programming languages.

Main advantage of Java RMI is its speed. The main disadvantage is the dependence on the format and serialization format. Class files vary depending on the version of Java, and therefore only work with clients and servers running the same JVM.

Java RMI allows to pass a class to a remote method as a parameter that the remote system does not know (dynamic code loading). The remote system can itself downloads the byte code and can start using it:

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
 * Rozhraní pro objekt na serveru.
public interface Compute extends Remote {
    <T> T executeTask(Task<T> t) throws RemoteException;
 * Rozhraní pro parametr metody na serveru.
public interface Task<T> {
    T execute();
 * Implementace na serveru.
public class ComputeEngine implements Compute {
    public <T> T executeTask(Task<T> t) {
        return t.execute();
    public static void main(String[] args) {
        Compute engine = new ComputeEngine();
        Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
        LocateRegistry.getRegistry().rebind("Compute", stub);      
 * Implementace úlohy poskytnutá klientem.
public class Pi implements Task<BigDecimal>, Serializable {
    public BigDecimal execute() {
      // implementace výpočtu čísla Pí
 * Klient.
public class ComputePi {
    public static void main(String args[]) {
        Pi task = new Pi();
        Compute comp = (Compute) LocateRegistry.getRegistry("").lookup("Compute");
        BigDecimal pi = comp.executeTask(task);

Data Communication


XML (eXtended Markup Language) is a standardized markup language. Its main advantage is the existence of many associated standards e.g. XML Schema, that allow to prescribe data formats, or XML Signatures for digital signature.


JSON (JavaScript Object Notation) is a data format that is written as JavaScript objects, e.g.:

 "jméno" : "Jan",
 "příjmení": "Novák",
 "adresa": {
     "ulice": "Severní 1",
     "obec":   "Horní Dolní"
 "věk": 110,
 "mrtev": true,
 "odpracované roky": [1950,1951,1952,1953,1954],
 "oblíbené knihy": [ {"název":"Vinnetou","autor":"Karel May"}, {"název":"Babička","autor":"Božena Němcová"} ]  

The advantage is that browsers equipped with JavaScript (which is all of them) do not need to perform any data parsing. It is more compact than XML, which was originally designed for documents, not data. The implementation of parsers and generators are available for all languages​​.


Binary formats have the advantage of quick parsing, but the disadvantage of poor scalability in newer versions.

Java RMI uses a binary format.

Using the HTTP protocol for communication

Transmiting messages can be done directly on TCP/IP. For safety reasons, the majority of firewalls block the incoming connection over ports different than 80 (HTTP) and 443 HTTP over SSL).

The URL GET method can be as follows:

        InputStream inputStream = new URL("").openStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
        String line;
        while((line=in.readLine())!=null) {

If necessary, you can even do POST, or work with passwords and certificates.

For more complex usage with HTTP, it is recommended to use a library, like the Apache Http Components Client.


By using XML-RPC we can define in XML the signature of the call, e.g.:

<?xml version="1.0"?>

There are implementations for many languages, see XML-RPC Home Page. In Java, for example, Apache XML-RPC you can write an application such as:

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  config.setServerURL(new URL(""));
  XmlRpcClient client = new XmlRpcClient();
  Object[] params = new Object[]{new Integer(33), new Integer(9)};
  Integer result = (Integer) client.execute("Calculator.add", params);


The equivalent of using XML-RPC messages in JSON format.

in: {"id":"-7784003634935858885","jsonrpc":"2.0","method":"mojeMetoda","params":[1 , "řetězec", true, {"i":99,"s":"kůň","b":false,"f":0.2098023224,"list":["D","E","F"],"map":{"březen":"March"}}] }
out: {"jsonrpc":"2.0","id":"-7784003634935858885","result":{"i":10,"s":"příliš žlutoučký kůň","b":true}}


Large companies offering so-called enterprise solutions to RPC offer the so-called Web Services, with the usage of the communication protocol SOAP (once meaning Simple Object Access Protocol) with a service interface described with the WSDL language (Web Service Description Language).

Compared to XML-RPC it has several advantages:

  • the interface is described in a machine-processable format WSDL, so there are tools to generate Java classes from WSDL
  • Allows the return of so-called faults, like exceptions for non-standard conditions
  • There are related standards such as WS-Addressing or WS-Security for special requirements

For more details: tutoriál Web Services

In Java there are many examples of implementations:


Although remote procedure calls can be confortable for programmers, very often require an exact match of client and server versions, and as well this approach scales poorly . Systems that want to have an interface to use Internet-services (eg Google Calendar , Amazon, etc. ) choosed a REST interface , which is an abbreviation of REpresentational State Transfer, ( see [1] ) .

REST is an architectural style of applications, it simply uses HTTP with his methods GET,PUT,POST and DELETE for editing data located on certain URLs.

For data transfer you can use any format: very popular are XML and JSON .

For the implementation in Java you can use any library that supports HTTP. Quite useful can be to use Spring 3.x RestTemplate (javadoc pro RestTemplate). This library can convert the most common types of response ( XML , JSON , plain text, ... ) to the respective objects .

   RestTemplate rt = new RestTemplate();
   JsonNode l = rt.getForObject("{a}/{b}", JsonNode.class,"hodnotaA","hodnotaB");


A model based on REST can be advantageously used in the creation of applications running in the Web browser, called RIA - Rich Internet Applications. One key technology is AJAX (Asynchronous JavaScript And XML) that very often does not use XML, rather just JSON for data transfer. Another common technology is based on the Flash browser plugin called Flex.

When creating AJAX applications you use typically libraries that support JavaScript objects, such as prototype.js, jQuery,, Dojo and DWR.

Making AJAX applications requires knowledge of the JavaScript programming language, knowledge in the creation of ActionScript that is a strongly typed JavaScript. There are standards to help: [ECMAScript].


JavaScript has almost nothing in common with Java, except that both the syntax was taken from the C language. Java is a statically and strongly typed language. JavaScript is a dynamic, weakly typed language.

In JavaScript there are objects, but they are not related to classes. A JavaScript object is an associative array (like java.util.Map in Java), which can dynamically add additional attributes and methods. So the object is created as follows:

<script type="text/javascript">
    var objekt = { };

    objekt.a = 3;                                                   //real number
    objekt.b = "blabla";                                            //string
    objekt["c"] = true;                                             //boolean
    objekt.d = [ 2, "ble", false, null, { } ];                      //field
    objekt.metoda = function(neco) { return neco + (this.a + 1); }; //method

    alert("vysledek= "+objekt.metoda("hu")); //zobrazí "hu4"

JavaScript is functional because you can call functions without an associated object/class. Contains also called closure in which local variables exist outside of the local context, if used in the function passed as a parameter. for example:

   //funkce vyššího řádu, bere jinou funkci jako argument
   function ukazka(fce) {
        return fce(5);

    for(var i=0; i<3; i++) {
        var u = ukazka( function(x) { return x+i;} ); //předána anonymní funkce používající lokální proměnnou i
        alert('i='+i+' u='+u);

Since version 6.0, JavaSE in the JRE Java Scripting API ia packaged with a JavaScript implementation.


For security reasons, web browsers apply the so-called same origin policy, which does not allow a Web site to access with Javascript other pages different from the original source identified by the triple (protocol, DNS name of the machine, port).

If you need to work around this limitation, you can use techniques JSONP or CORS.

JSON uses the HTML tag <script> that may refer to a document from another server. CORS is a specification that defines that browsers respond to HTTP headers Access-Control-Allow-Origin:.

Google Web Toolkit

GWT is a tool designed by Google to develop AJAX applications in Java.

Source code in Java is cross-compiled to JavaScript, which then runs in a browser. Most classes of packages java.lang and java.utils have been ported to JavaScript so that you can use them.

From the server, the application takes only data using RPC/AJAX calls.