Network programming in Java
From EGEE-see WIki
Network programming in Java
Networking classes included in standard Java are considered one of better and complete solutions amongst modern languages, with simple syntax and semantics, although they are being used for objective which is not too easy to implement: distributed programming.
Originally Java was presented as “the language for the Internet” because the integrated networking support was good and complete. Logically, Java has been continuously developed in the same rhythm as the Internet itself was being more and more complex – at the same time code written in Java remained as much backwards compatible as it was possible. Today a Java network program is written the same way no matter which IP protocol is being used in the production environment, Java adopts. Because of its network core but also because of the excellent support for concurrent programming in time Java became industry standard for developing big distributed enterprise applications.
Communication technologies
Technologies which will be described in this tutorial will be the basic network technologies integrated in Java – socket and RMI programming. Socket programming presents lower level of abstraction, while the RMI presents higher level, which enables remote method calls like they are positioned on the very computer where program executes (Microsoft .Net Remoting technology was implemented afterwards using same premises). Choice seems simple: if a complete control is desired sockets are a usual solution, but if connecting remote systems on the fly without too many low-level protocol considerations is desired, RMI seems to be a better solution.
Socket programming expects that firewall (if any is active at all) allows it to work, otherwise program would not work. Basically, the program needs to be able to occupy the port – that’s being done both by enabling it in firewall and making sure that program’s port is not already occupied by some other process.
One of bad things in RMI is that it data transport is based on serialization (which is Java-proprietary thing) so it shouldn’t be expected that a program written in some other language can be used on other side of the communication channel. It is not unusual that even small differences in version of the Java runtime cause problems in de-serialization which consequences RMI . Therefore, it is a convention and a good practice to use serialVersionUID to go around that problem. Another problem with network data transfer in Java could be firewall because they are usually configured to forbid binary communication which is a first choice when building distributed network applications – although RMI doesn't have that problem because it packs data in HTTP Request & Response messages to solve firewall problems (official RMI specification explains this topic thoroughly).
Some of technologies present today that partially or completely solve problems mentioned above are: Web services (which use SOAP XML – firewall-friendly text format); EJB (JMS or JNDI); Caucho Hessian (special binary format); Caucho Burlap (special XML format); Spring Http Invoker (serialization but using XML); CORBA etc.
We will be focusing here on technologies present in java.net and java.rmi packages which are basic core ingredients of the Java platform, show how to write applications based on classes from these packages, explain problems that may appear and how to solve them.
