Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 20 February 2016

An overview of the Message Oriented Middleware with JMS

Message oriented middle-ware allows software components that have been developed independently on different networking platforms to interact with one another. Applications in a distributed system across network node use the API to communicate without the worrying about the underlying details of the operating system. The API sits above the middle-ware layer in a distributed system. The Message Oriented Middle-ware (MOM) allows distributed applications to communicate and exchange data by sending and receiving messages.

Message queues are also referred to as Message-Oriented Middle-ware. This is a class of commercial middle-ware with key implementations including IBM's WebSphere MQ and Oracle's Stream Advanced Queuing. Main aim off these products is to achieve Enterprise Application Integration, this is achieved by loose coupling and message queues. These systems are used in commercial transaction processing systems.

In the programming model for these queues. we take an approach where we send messages to a queue and other processes. The following generally support the following;

  • A blocking receive, this blocks until the appropriate message is received.
  • A non-blocking receive, will check the status of the queue and return a message if one is available.
  • A notify operation, causes an event to issue a notification that corresponds to the associated queue.

The queuing policy is first-in-first-out (FIFO), other queue implementations also support the concept of priority, with higher-priority messages being delivered first.

The Java Messaging Service is a standardized method for a distributed Java program to communicate indirectly.

A JMS broker provides clients with connectivity, it stores messages and deliver functions. A message in JMS is an object that contains the required heading fields, optional properties and data payload being transferred between JMS clients. The destinations are maintained by the message broker. This can be either queues or topics.

A queue offers point to point model, only one consumer gets the message and messages have to be
sent in specific order as in earlier part of where queue messages can’t be delivered out of order. A
queue only guarantees that each message is processed only once. Like in the lab when the producer
submits a message to the queue and we can browse the queue and decide which messages we want
to receive and which we want to purge or delete.

A topic offers publish and subscribe model where multiple clients can subscribe to a message. In
topics there is no guarantee that each message is processed only once and the subscriber needs to
be active when message is produced unless subscription was durable.

The JMS client is essentially a Java program that produces and consumes messages, a JMS producer is a program that creates and produces messages and a JMS consumer is program that receives and consumes messages.
  • A JMS provider is any system that implements the JMS specification.
  • A JMS message is simply an object that is used to communicate information between JMS clients.
  • A JMS destination is an object supporting indirect communication in JMS. This is either a topic or queue.
The first step that is required to interact with the JMS provider is to initiate a connection between a client and provider program. This connection is channel between the client and provider in form of a TCP/IP socket.

Twp types of connections can be established a Topic Connection or a Queue Connection.

Connections can be used to create sessions, a session is a series of operations involving the creation, production and consumption of messages related to a task.

A Topic Connection can support one or more topic connections and a Queue Connection can support one or more queue connections. However we cannot combine different type of sessions together in a connection.

The session is the central component to the operation of JMS. A session consists of a series of operations involving the creation, production and consumption of messages related to a task.

A message consists of thee parts, a header, a set of properties and a body of the message.

Code examples can be found in the link below:
https://github.com/JunSoftware109/Java_Exercises/tree/master/Lab%204%20MOM/exploring-jms

For detailed information check out : http://docs.oracle.com/javaee/6/tutorial/doc/bnceh.html

Sunday, 23 August 2015

Simple guide to TCP and UDP sockets

Introduction

Today we will analyze and build some simple TCP and UDP client and server based applications using Java in Netbeans.

Processes communicate with each other by exchanging messages across a computer network. The process that initiates this process are called the client and the one that waits for the response is called the server. One of the most basic conditions for programming in a distributed system is the possibility to transfer data from one process to another. In this situation the process that initiates the communication is labelled as the client and the one that waits to be contacted is the labelled as the server. Computers that are connected to a TCP/IP network are identified by their IP address and port number. The port number is managed by the operating system and is allocated to processes required to use the network.


A port number is a 16-bit unsigned integer from the range of 0 to 65535. Sometimes we refer to a port number simply as a port. A computer runs many processes, which commmunicate with other processes running on remote computers.When the transport layer recievives an incoming packet from the Internet layer, it needs to know which process on that computer should this packet be delivered to. A port number is a logical number that is used by the transport layer to recognize a destination process for a packet on a computer.

Each incoming packet to the transport layer has a protocol; for example the TCP protocol handler in the transport layer handles TCP packet and the UDP protocol handler in the transport layer handles a UDP packet.
Some well known Ports are: 0 - 1023
Registered Ports: 1024 - 49151
Dynamic or Private Ports: 49152 - 65535

echo: Port 7
FTP: Port 21
Telnet: Port 23
SMTP: 25
POP3: 110
NNTP: 119


UDP and TCP use socket abstraction for inter-process communication. A process sends messages and receives messages from the underlying network through its socket. The sending client must identify the receiving server. This requires two pieces of information, an IP address to identify the destination host, and a unique port number to identify the receiving process running in that host.

Following is an example of how a TCP Client/Server Application in Java is performed; first a client reads a line from standard input keyboard and sends the line out on its socket to the server. The server uses its listening socket to listen for a connection to be made. When a connection is made the server creates a connection socket between the client and server. The server then reads the client’s line from its connection socket. The server converts the line to uppercase. The server sends the modified line out on its connection socket to the client. The client reads the modified line from its socket and prints the line on its standard output monitor.

Socket Programming with TCP

The API (application programming interface) to the TCP protocol provides the abstraction of a stream of bytes to which data may be written and from which data may be read. There are two kindsm of sockets, a Connection-Oriented Socket and a Connectionless Socket.

A connection-oriented socket is also called a steam socket. A connectionless socket is also called a datagram socket.

Transmission Control Protocol (TCP), is used in the transport layer and is one of the most widely used protocols to provide connection-oriented sockets. The application hands over data to a TCP socket and then the TCP takes care of streaming the data to the destination host. TCP takes care of ordering, assembly, fragmentation, lost data and data detection.

User Datagram Protocol (UDP), is used in the transport layer, it is the most widely used protocol that provides a connectionless socket. It is unreliable but is much faster than TCP. It allows us to send limited sized data.

Deciding to use which protocol depends on how the application will be used. If data integrity is important than we will have to use TCP. If speed is a priority then we should UDP. For example for file transfer we may use TCP but for a video call we may use UDP.

In order to react to a client’s initial contact, a server program must be running as a process, and it must have a socket listening for a connection. The client program creates a stream socket bound to any port, specifying the IP address of the server host and the port number of the server process. The client then initiates a three-way handshake and establishes a TCP connection with the server.

During the three-way handshake (which is transparent to the client and server programs), the server hears the connection request on its listening socket, a ServerSocket object (named listenSocket in the example below). It reacts by accepting the connect setup, i.e. it invokes the listening socket’s accept() method, which creates a new connection socket, this time a Socket object, (named connectionSocket in the example below), dedicated to this particular client


From the application’s perspective, the TCP connection is a direct virtual pipe between the client’s socket and the server’s connection socket. Thus, TCP provides a reliable byte-stream service between client and server processes, in which each byte is delivered in the order sent. Client and server processes can each both send and receive bytes through the same socket. This is illustrated in Fig 1 below. Because sockets play such a central role in client/server applications, client/server development is also referred to as socket programming.

To be able to exchange data over sockets Java uses streams. A stream is a sequence of characters that flow into or out of a process. Each stream is either an input stream for the process or an output stream for the process. If it is an input stream, then it is attached to some input source, such as standard input (the keyboard) or a socket into which data flows from the Internet. If it is an output stream, then it is attached to some output source, such as standard output (the monitor) or a socket out of which data flows into the Internet. Streams transfer byte arrays of data across the network.

Client Socket, Server Listening socket, and Server Connection Socket


Example TCP Client/Server Application in Java
The following client/server application is used to demonstrate socket programming for TCP (and for UDP in a later section):
1.     A client reads a line from standard input (keyboard) and sends the line out on its socket to the server.
2.     The server uses its listening socket to listen for a connection to be made.
3.     When a connection is made the server creates a connection socket between the client and server
4.     The server reads the client’s line from its connection socket.
5.     The server converts the line to uppercase.
6.     The server sends the modified line out on its connection socket to the client.
7.     The client reads the modified line from its socket and prints the line on its standard output (monitor).

The client program is called TCPClient.java, and the server program is TCPServer.java. They are both presented below with extensive commenting. The two programs are first compiled on their respective hosts. Then the server program is executed to create a server process at the server host, which waits to be contacted. When the client program is executed, a process is created at the client, and this process immediately contacts the server and establishes a TCP connection with it. The user at the client may then use the application to send a line to the server which converts it to uppercase and returns this capitalized version to the client.

The java.io package provides classes for io (input/output) support and the java.net package provides classes for network support. These two packages must be imported in our client and server programs.


Code for the client side of the application: TCPClient.java:

import java.io.*; import java.net.*; public class TCPClient { public static void main(String argv[]) throws Exception { System.out.println("Please enter a sentence for the TCP server to capitalise:"); //Create an input stream and attach it to the keyboard InputStream inFromUser = System.in; //Create a byte array to hold the user's input byte inFromUserByteArray[] = new byte[100]; //Read the user's input from the input stream into the byte array inFromUser.read(inFromUserByteArray); //Create a socket object & initiate the TCP connection between client & server Socket clientSocket = new Socket("hostname", 6789); //Create an output stream and attach it to the socket object OutputStream outToServer = clientSocket.getOutputStream(); //Write the data in the byte array to the output stream outToServer.write(inFromUserByteArray); //Create an input stream to read in the server's reply from the socket InputStream inFromServer = clientSocket.getInputStream(); //Create a byte array to hold the server's repsonse byte inFromServerByteArray[] = new byte[100]; //Read the server's response in the input stream into the byte array inFromServer.read(inFromServerByteArray); //Create a string using the bytes read into the byte array String modifiedSentence = new String(inFromServerByteArray); //Print the server's reply on the terminal System.out.println("Reply from TCP server is : " + modifiedSentence); //Close the socket clientSocket.close(); } }  









Code for the server side of the application: TCPServer.java:

import java.io.*; import java.net.*; public class TCPServer { public static void main(String argv[]) throws Exception { //Create a listening socket object to listen for a client connection ServerSocket listenSocket = new ServerSocket(6789); while (true) { System.out.println(“TCP Server is waiting for a client to connect”); //When a connection is requested accept it and provide a connection socket for communication with the client Socket connectionSocket = listenSocket.accept(); //Attach an input stream to the connection socket InputStream inFromClient = connectionSocket.getInputStream(); //create a byte array to hold data from the input stream byte inFromClientByteArray[] = new byte[100]; //Read the data from the input stream into the byte array inFromClient.read(inFromClientByteArray); //Create a string from the bytes in the byte array String clientSentence = new String(inFromClientByteArray); System.out.println(“Recieved from client: + clientSentence); //Capitalise the string String capitalisiedSentence = clientSentence.toUpperCase(); //Create an output stream OutputStream outToClient = connectionSocket.getOutputStream(); //Using the output stream send the capiatalised sentence to the client outToClient.write(capitalisiedSentence.getBytes()); } } }
Note that the server’s connection socket has the same port number as the client’s socket. When the connection socket is created (by calling the listening socket’s accept() method) a direct virtual pipe between clientSocket at the client and connectionSocket at the server is established. The client and server can now exchange bytes, and all bytes sent arrive at the other side in order. With connectionSocket established, the server can continue to listen for requests from other clients for the application using listenSocket (though the server code above would need to be modified with threads to do so).

1.              Test this application
The code for the TCP client and server classes is presented. Create the appropriate files, and then compile and run the two programs in the correct order. Use the application by typing a sentence using the keyboard (standard input) at the client.
Note that in the client program, the name “hostname” must be replaced with the IP address of the server in order for the application to work. The host name localhost or the IP address 127.0.0.1 can be used if the client and server processes are run on the same machine.
2.              Test the application with a remote host
Close the previously running client and server processes. Now run the client program on one machine and the server process on another machine. In order to do this you will have to modify the client code to include the IP address of the server machine (run the DOS command ipconfig in order to find the IP address of a PC).


Socket Programming with UDP
When two processes communicate over TCP, they simply insert bytes in the pipe (stream) between them. No destination address is required for the bytes because the pipe is logically connected to the destination. The pipe provides a reliable byte stream channel – the sequence of bytes received is exactly the same (in the correct order) as the one sent.
Similarly to TCP, UDP allows two or more processes running on different hosts to communicate. As UDP is a connectionless service, no handshaking phase and no pipe is established. As there is no pipe, the destination address must be attached to each batch of bytes. The destination address consists of an IP address and port number, and these bytes along with the data bytes form a packet or datagram. UDP provides an unreliable message-oriented transport service in that it makes a best effort to deliver packets, but makes no guarantees.
The code for a UDP based application differs significantly from TCP:
  • There is no initial handshaking, so there is no need for a listening socket
  • Streams are not attached to sockets
  • The sending hosts create packets by attaching the IP destination address and port number to each batch of bytes sent
  • The receiving process must unravel each received packet to obtain the packet’s data bytes.
Example UDP Client/Server Application in Java
The previous client/server application is used again to demonstrate socket programming for UDP.
Code for the client side of the application: UDPClient.java:


  import java.io.*;
  import java.net.*;
  public class UDPClient {
     public static void main(String args[]) throws Exception {
       System.out.println("Please enter the sentence that the server should capitalise: ");

       InputStream inFromUser = System.in;
       byte sendDataByteArray[] = new byte[1024];
       inFromUser.read(sendDataByteArray);

       InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
       DatagramPacket sendPacket =
          new DatagramPacket(sendDataByteArray, sendDataByteArray.length,
             IPAddress, 4444);
       DatagramSocket clientSocket = new DatagramSocket();
       clientSocket.send(sendPacket);

       byte[] receiveData = new byte[1024];
       DatagramPacket receivePacket =
          new DatagramPacket(receiveData, receiveData.length);
       clientSocket.receive(receivePacket);
       String modifiedSentence =
          new String(receivePacket.getData());
       System.out.println("Recieved from server: " + modifiedSentence);
       clientSocket.close();
    }
 }
Code for the server side of the application: UDPServer.java:

 import java.io.*;

 import java.net.*;

  public class UDPServer {

     public static void main(String args[]) throws Exception {

        DatagramSocket serverSocket = new DatagramSocket(4444);

        while (true) {

            System.out.println(UDP Server is up and running......);

           byte[] receiveData = new byte[1024];

           DatagramPacket receivePacket =

             new DatagramPacket(receiveData, receiveData.length);

          serverSocket.receive(receivePacket);

          String clientSentence = new String (receivePacket.getData());

           System.out.println(UDP Client sent :  + clientSentence);

          InetAddress IPAddress = receivePacket.getAddress();

          int port = receivePacket.getPort();

 

          String capitalisiedSentence = clientSentence.toUpperCase();

          byte[] sendData = new byte[1024];

          sendData = capitalisiedSentence.getBytes();

          DatagramPacket sendPacket =

             new DatagramPacket(sendData, sendData.length,

                IPAddress, port);

          serverSocket.send(sendPacket);

       }

    }

 }

Tuesday, 28 July 2015

An introduction to programming, for the absolute beginner

What is Java? 

In the most simple of terms, Java is a general purpose programming language. Initial version of Java was released by Sun Microsystems in 1995. The characteristics behind Java's huge popularity lay behind its simplicity, wide variety of usages and robustness. Java can be used to develop programs that can be used in different environments. You can write programs that can be used in a client-server environment. One of the most popular usages of Java in its early days was in the development of applets. An applet is a Java program that is embedded within a webpage.

Programming can be defined as a sequence of instructions that perform a specific task on a computer.  Well defined notations are required for this process and these set of notations are a called programming language. A programming language is a tool used by an engineer to solve problems. This can be from acquiring statistical data through complex algorithms or designing the logic for a circuit.
 

So how do we tell a computer what to do? As humans we mostly communicate through a spoken language be it English, French or Japanese. However this is not the only means of communicating we also use gestures without saying anything to convey a message.


                                                          https://netbeans.org/images_www/collateral/80/banner_182X150_80FCS.png   

Netbeans is a popular IDE used to write Java programs and also supports a host of other languages.
You can even try it online at http://www.tutorialspoint.com/compile_java_online.php

To have successful communication we have to both be using the same language. If I were to speak Chinese and you French we would need a translator to understand each other. Computers understand instructions in the binary format, this consists of a sequence of 1's and 0's. The following is 'hello' in binary:

      (H)              (E)               (L)                (L)                 (O)
01101000     01100101     01101100     01101100      01101111

This is what is known as machine language. Programs that are written in machine language are machine-dependent. Each computer has its own different instructions where 0011 can be instruction to add two numbers and on another computer something else. Programs written in machine code are difficult, perhaps one could say even impossible to read or write.

Machine language is used to program hardware ranging from consumer electronics like disc drivers to video game consoles like the PlayStation 1.

Below is some code that handled input on a controller for the PlayStation 1.


int move_user_char( unsigned char* pad )
{
 int n;
 int bcount;
 long range;
 long vx=0, vz=0;
 static long ofx=0, ofy=0;


 /* Turning left/right */
 n = 0;
 if(pad[2]<=0x60 ) n = -(0x60-pad[2]);
 if(pad[2]>=0xa0 ) n = -(0xa0-pad[2]);
 PVect.vy = (PVect.vy+n/2)&4095;


 /* Moving forth and back */
 n = 0;
 if(pad[3]<=0x60 ) n = (0x60-pad[3]);
 if(pad[3]>=0xa0 ) n = (0xa0-pad[3]);
 if(n!=0) {
  vz += (rcos( PVect.vy )*n/128/4);
  vx += (rsin( PVect.vy )*n/128/4);
 }


High and low-level languages? 

Machine languages and assembly languages are also known as low-level languages as these are close to the heart of machine, where we can control and adjust each instruction. So low-level details of the computer CPU, memory peripherals and which registers to use, are essential to understanding low-level languages like assembly language. With machine languages we do not have an abstraction of the lower level irrelevant details of a computer.


In comparison higher-level languages provide a means of abstraction where the programmer does not have to worry about the internal specifications of the machine in order to create programs efficiently. The need for this abstraction gave rise to programming languages like C, C++ Java, FORTRAN (formula translator, developed by IBM) and COBOL (common business-oriented language). These higher level languages make use of English words, mathematical notations and concepts the are intuitive to our understanding. The program that is written in a high level language is called the source code.

Simple instructions can be read and understood easily for example to divide two numbers we could have some as code as follows: int x = 10 / 5;

We can see that it is easy and self-explanatory but this also means that the computer does not understand these instructions without a translator. So there is a requirement for the computer to translate these high-level instructions to machine code.

Below is some simple java code that that prints a hello world message.

1
2
3
4
5
6
7
public class Main {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    } 
} 
  
 
Minecraft is a popular game written in Java


Code Compilation

Compilation of code or compiling a program is overly simplified phrase that is often used by programmers and developers alike. This phrase means translating a written program in a high-level language to a lower-level programming language. The code that is complied by a compiler is called compiled code. Another method that is used is by using an interpreter. An interpreter does not translate the whole code rather it reads one instruction written in a high-level language at a time, we can sometimes describe an interpreter as a simulation. 

In a programming language such as Java compiled code is translated into byte-code. An interpreter call the Java Virtual Machine (JVM) is used for this process in the Java platform, it interprets the byte-code in a .class file, this is a binary format. The procedure of bringing byte-code for a class definition into the memory of a running JVM is called classloading. The JVM has classloaders which take the .class files and them loads them into memory. The classloader servers as an abstraction, classes are only loaded on-demand when the JVM needs the class definition.
 
Most Java Virtual Machines use just-in-time compilers (JIT), which compiles an entire Java program into machine language. Ahead-of-time (AOT)  is also sometimes used this is a compiler which is used to compile a program in a intermediate language to machine language.

Other compiled languages such as C++ or Objective-C have to be compiled to run with a defined architecture and operating system, however this is not the case with Java. With Java one is able to run on any JVM regardless of the platform.

Programming Paradigm

An important aspect of modern software development is the idea of Object Oriented Programming. OOP is a paradigm, a theory, an idea, an approach to providing a solution to real world problems. We use models supported by that language, which then enables us to create a solution that is called the program. In fact one may often here that "Java is a object-oriented programming language", but what this really means is that provides features and constructs that support that object-oriented paradigm.

"A programming paradigm is a way of conceptualizing what it means to perform computation, and how tasks are carried out on a computer should be structured and organized" - Robert W. Floyd 1978

The above quote by Floyd, captures the definition of paradigm in the programming context. There are many different paradigms including Procedural, Functional, Logic and Object-oriented.  



If you enjoyed reading this blog, please leave a like, comment or share. 

Until next time goodbye for now.