Java/Java Network Programming

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Data Types, Variables,and Arrays
Operators
Packages, Classes, and Interfaces
Java Program
Modifiers
Control Statements
Exception Handling
Object Oriented Programming
Wrappers
Strings
Math
Arrays
Random Numbers
Date and Time
Regular Expressions
Java Collections
Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
* TCP Sockets
* UDP Sockets
* Multicasting
RMI
Image, Audio, Video
GUI
Applets
Internationalization
JDBC
Java and XML
What is Network Programming

Network Programming means different to different people. To some programmers networking programming means low-level programming like reading and wirting to network sockets. To others the meaning of network programming is broader and includes the design and programming of distributed applications.

Distributed applications are mainly client server applications that use network server for important services, such as security and database access, are one popular interpretations. The important feature of distributed computing is that it involves two or more computers communicating with each ohter.

What is a Network

A network is a collection of computers and other devices that can move information around from one device to another. Traditionally, network devices were connected together with wires with shared information converted into electrical signals that were passed across the wires. Later, fiber optics cables were used to convert the information into light signals instead of electrical signals and today, some networks use radio transmitters and receivers and do not use cabling at all.

Each device on a network is called a node. These include computers as well as printers, routers, bridges and gateways.

There are lots of different kinds of networks. The most common are LANs(Local Area Networks) and WAN(Wide Area Network). LANs are used to connect computers in a local area such as office, home, shops. WANs connect computers over a wider area, such as across city. Best example for a WAN is Airways Reservation System.

Networks are laid out with some common geometric relations. The most common are star, bus and ring topologies.

Star topology

Connects nodes together is smaller circuits, which are then connected to a circuit that runs through the network.

Image:Star-topology.jpg

Bus topology

Connects all of the nodes to one circuit that runs through the network like a backbone.

Image:Bus-topology.jpg

Ring topology

Connects all of nodes together in one closed circuit

Image:Ring-topology.jpg

Layers of a Network

Image:Network-layer.jpg

Moving information form one computer to another is a complex set of operations. It involves many different standards and protocols. Engineers solved the complex web of protocols and standards by breaking them down into an abstract set of layers, with each layer building on the services provided by the layers beneath it. The layers of a network only speak to the layer immediately above and below them.

The OSI(Open System Interconnect) Reference Model, show above defines seven layers that every network stack should provide. This model is defined by the ISO(International Standards Organization)

Here are some short definitions for these seven layers:

Physical Layer

This layer sits at the lowest level of the OSI model and performs sending and receiving of the raw data through physical mediums like the electrical, optical, and physical components of the network. The physical layer carries the signals for all of the higher layers.

Data Link Layer

The data-link layer provides error-free transfer of data frames from one computer to another through the physical medium. This layer defines the format of the data on the network. The data link layer converts data bits into packets and defines parameters like the largest packet that can be sent. If a packet is corrupted during transit, the data link is responsible for retransmitting the packet.

Network Layer

The Network Layer is responsible for routing those packets received from data link layer. It determines what physical path the data packets should take based on the network conditions, the priority of service, congestions, shortest path and other factors. This layer also breaks packets which are larger than allowed by the underlying network into smaller packets. At the destination point this layer only does the reassembling of those fragmented data packets so that they can be combined into original size.

Transport Layer

The transport layer ensures that packets are delivered in the order in which they are sent and that there is no loss or duplication. It offers a level of abstraction for the higher layers, shielding them from the low-level details of the network, data link and physical layers.

Session Layer

The session layer starts, maintains and terminates sessions across the network. This layer provides security authentication and synchronization. Synchronization starts check points in the data stream thus improving network performance. At a time session fails, data after the most recent checkpoint will be retransmitted.

Presentation Layer

The presentation layer work as the translator of the data for the network. This layer is responsible for the transfer of the data to both the layers that is the next layer above the presentation layer and the one which is below this layer. It performs the character set conversion, encryption and compression of the data.

Application Layer

This layer as defined by its name is responsible for all application services which help to support the user applications. Since it is on the top most hierarchy of network model it works as a user interface for all the applications.

Image:Send-receive.JPG

What are Network Protocols

The term protocol can be understood in the simplest terms as the standards which have to be followed in the entire process and thus the term Networking Protocols defines the standards or the rules which have to be followed at the time of enabling the network connection and the transfer of the Data across the network.

Thus , we can define Networking Protocols as the rules which govern the syntax, semantics and synchronization of communication. Protocols may be implemented by hardware, software, or a combination of both of them .

Some of the network Protocols are listed as under:

1. IP (Internet Protocols)

2. TCP (Transmission Control Protocol)

3. UDP (User Datagram Protocol)

4. Ping (Packet InterNetwork Grouping)

5. FTP (File Transfer Protocol)

6. HTTP (Hypertext Transfer Protocol)

7. NNTP (Network News Transfer protocol)

8. SMTP (Simple Mail Transfer Protocol)

9. POP 3 (Post Office Protocol 3)

10. SNMP (Simple Network Management Protocol)


We will discuss each one of them in brief in the section below to have an understanding about each one of them:

1. IP (Internet Protocols): Internet Protocols are very much responsible for sending or transferring the data from one computer to another on the network. The data which has to travel from one computer to another is divided into a number of divisions which are called as “Packets”. Every packet is first passed through a gateway computer which understands a part of the internet. These packets travel all over the network carrying a unique address of both the sender and the receiver which will help the data packet to finally reach the correct destination Computer. These Unique addresses are called the IP address. Both the computers i.e. the sender and the receiver are called the hosts and they both have a 32 bit logical IP Address.

This protocol is responsible for the successful transfer of data packets from one host to another but it is very much possible the packets do not reach in the same sequence it should reach and to make sure that all the packets reach in the right sequence we have another Protocol which takes care of this. This protocol is called as TCP (Transmission Control Protocol); we will talk about it below in the section.

This protocol does not make any continuous connection between the sender and the receiver and that’s why it is termed as connectionless protocol and each packet of data behaves more like an independent unit of the data which should reach the correct destination.


2. TCP (Transmission Control Protocol): Transmission Transfer Protocol works closely with the internet protocol as it handles the responsibility of the successful delivery of the data packets in the correct sequence which are traveling from the receiver to the sender. The complete message is divided into various segments called the packets which help to transfer the complete data or the message successfully over the network.

This Protocol is responsible for ensuring that a message is divided into the packets whose transmission is controlled by the IP and then they are rearranged back in the same order so as to make a meaningful message reach at the destination which is the other end of the transmission process.

Unlike IP, TCP is called as a connection-oriented protocol because it provides a virtual connection service with the help of sockets and ports. This also means that a connection is established and maintained until the complete message is successfully exchanged between the two hosts.


3. UDP (User Datagram Protocol): User Datagram Protocol provides a very limited and restricted service when the data or the messages are exchanged between the two hosts which implement the Internet Protocol. This protocol sits in the 4th Layer which is the transport Layer. It also uses the internet Protocol to send or transfer a data unit or segment which is called as Datagram from one computer to another.

Since this protocol works like the Internet Protocol it doesn’t have any feature which assures the transfer of the datagram’s in the correct sequence to the destination Computer this protocol is advised to be implemented only when the data size is very small else it can create trouble at the time of transferring a big data or message from one computer to another. It doesn’t provide any error correction or retransmission feature so if any data segment is lost or corrupted in the transmission process the complete data packet is discarded and rejected. So it can work successfully only in case of small independent packets of information.


4. Ping (Packet InterNetwork Grouping): Ping is a very common terminology in the computer world which means that when we ping a host on the network we come to know whether that host is active or inactive or whether there is any delay in the communication between the sender and the receiver.

Ping Packet InterNetwork Grouping is a routing protocol which helps to handle network problems like network congestion. It uses the ICMP, Internet Control Message Protocol messages which try to get the response from the remote host by sending an echo request packet.

One very important point to remember in case of Ping is that it cannot be implemented in Java as Java doesn’t support ICMP.


5. FTP (File Transfer Protocol): File Transfer protocol is a very familiar and old protocol. Rather it can be said to be one of the oldest TCP/IP protocols and its term defines its purpose is to transfer the text and binary files successfully between two hosts on the network.

It is mostly used to send the Web page files from the sender to the computer which acts as a server for everyone on the Internet. And also used for the purpose of downloading the files and other programs to our computer from other servers. We can implement FTP with the help of a very simple command line interface and also with the help of GUI, graphical user Interface. It can use the password controlled access and public access too. When it is password Controlled then it is controlled with the help of account and password and if it is made public then no account or password is required.

6. HTTP (Hypertext Transfer Protocol): When ever we need to transfer the various types of files on the World Wide Web we implement the rules and guidelines provided by Hypertext Transfer Protocol. This protocol works on the top of TCP/IP Protocols which acts as the base of the protocols on the internet.

An HTTP protocol is used for communicating between the Web Browsers and the Web Browsers and it comes into picture immediately when we open any Web Browser.

The basic functionality in this process is that the web browser opens a connection and makes a request to the server, once the server sends back the requested information the connection is closed. Thus it is termed as a stateless protocol.


7. NNTP (Network News Transfer protocol): As the term defines itself this protocol is used by the USENET Internet news System. It is basically implemented by the computer clients and servers which try to manage the notes published on Usenet Newsgroups.

These servers try to efficiently manage the global network of all the collected Usenet newsgroups and include the server at the Internet access provider. An NNTP client is included as part of any Web Browser like Netscape, Internet Explorer etc and we also have the flexibility to use a separate client program called a newsreader.


8. SMTP (Simple Mail Transfer Protocol) SMTP is used to send and receive E-Mails and is a TCP/IP Protocol. It is used in combination either with POP 3 or IMAP which helps the user to save all its messages in a mailbox and then download them alter and this feature is not available with SMTP and thus used in combination with POP 3 or IMAP. SMTP.

Commonly, SMTP is used for sending messages from a client to a server and POP 3 or IMAP are used for receiving the E-Mails. Here the client establishes a connection with the SMTP server through a request or response dialogue and then the client transmits the mail addresses of the recipients for a message. If the given request is processed by the server and it accepts the mail address the message is successfully transmitted to the recipient.


9. POP 3 (Post Office Protocol 3): This protocol is said to be the latest version of a standard protocol which is used to receive the Emails. This is commonly used with the SMTP. SMTP manages sending the Emails and POP 3 serves the purpose of receiving the Emails for the recipients .The recipient can any time check his/her mailbox and download any mail from the mail box It deletes the mail on the server once the mail gets downloaded by the receiver . This standard protocol is built into all most all popular e-mail products, such as Outlook Express and Microsoft Internet Explorer browsers. An alternative protocol is Internet Message Access Protocol; IMAP which functions in a very similar way except that it retains e-mails on the server and it can also be called as a remote file server for organizing them in folders on the server.

10. SNMP (Simple Network Management Protocol): This protocol serves the purpose of controlling all the devices like bridges, routers and hubs etc which are connected to a network. All the resources which are monitored by SNMP are called as “managed object”.

It is also a connectionless protocol and quite simple in its functionality and thus it is recommended for very simple management of network components.

Network Models
The networking strategies of modern world are fully based on two types of models which are:

1. Client /Server Model 2. Peer-to-peer Model.


We will now talk about both of them to have an understanding on each in the section below:

1. Client/Server Model: The relationship between two computer programs in which they carry the relationship of a Client and a server represents the client/Server Model of networking. In this one program which is basically denoted as client, makes a request for any service from the other computer program which is called as server and the server in return to the request made by the client fulfills its request by performing that service. This model makes it very easy for the two computer programs to be connected to each other although they might be placed at two very different far off locations. Both the server and the client run their individual processes and the server processes can be executed either on the client machine or it can be easily executed on the remote machine.

It has successfully made an important place in the networking world and that’s why it is in today’s time is basically implemented in all the major business applications. The most common example where we see this model running very effectively and successfully is the operations which are performed on day to day basis in any Bank. Here all the communication is been done between two computer programs in which one server as a client and the other as a server.

The two very broad areas where we can see the client server running very successfully are Custom Business applications and Internet Applications.

In Java, we have the option that we can choose and decide how to split and distribute the work between the client and the server processes so as to make the entire application very efficient and here we have few choices with respect to the architecture which are :

1. Two-Tier Architecture

2. Three-Tier Architecture

3. Multi –Tier Architecture

Now we will briefly take a look on all the three architectures:

1. Two-Tier Architecture: The two- Tier Architecture as the term illustrates is based on the very simple and common architecture wherein we have a “fat” server and a “ thin” client and all the mojor processes are been brought under the dominance of the server.

The biggest positive aspect of the two-Tier architecture is that it is very simple and easy to use because all the TopLink features which the two-tier architecture provides are present in a single session type and the biggest drawback present in this architecture is that each client of it needs its own individual database session which provides the TopLink support for the two-Tier architecture. Here mainly a Java client connects directly to the database with the help of TopLink.

The Two-tier architecture is not that common as the three-tier architecture model.


2. Three-Tier Architecture: The three Tier architecture as the name defines divides the entire application into three components User Interface Services Tier, Business Rule Services Tier, Data Persistence Services Tier.

In this type of model the client application behaves as a very thin client. It as its term mentions , serves the purpose of providing only the user interface and it has nothing to do with the business logic of the particular application. This responsibility is taken care by the Application Servers which communicates directly with the database servers and since this has been isolated by the user interface and the data persistence, they can easily be put in the business rule components and these components can be installed on any number of application servers which makes it more effective than the two-Tier architecture model.

This mechanism not only makes it more compatible it also increases the performance of the application servers and whenever we do any kind of changes to the business rules we can get it modified on only few application servers instead doing these changes on many workstations thus saving huge amount of time.

This architecture is suggested to be implemented in the case of Web Application. Difference between the two-Tier and Three-Tier architecture: The basic difference between the two-tier and thrtee-tier architecture is that first one is a client/server architecture , in which the client requests to perform a task to the server and gets the response from the server in the form of execution of that task where as a three-tier or a multi tier architecture constitutes of a client, server and database. Where the client sends the request to the server and the server then sends the request to the database and the database sends information/data required back to the server and the server finally sends the information to the client for which it made the request from the server.

3. Multi-Tier architecture: This architecture is also termed as the n-tier architecture. This architecture unlike the three-tier architecture further bifurcates the business rule services into two segments. One is for supporting the user interface and the other to manipulate and play around the data.

It can be categorized into four tiers. First the User Interface Services Tier, Second is the UI-Oriented Services Tier, third the Data-Oriented Business Rule Services tier and the last one the Data Persistence Services Tier.

The basic difference between the three-Tier model and the multi-tier model is that this model further divides the business services tier present in the three-Tier architecture to two more divisions making it more flexible to be implemented. In this model we can have more processes added to the application servers and to increase the performance we can also add few more servers if required.


Multicasting

Multicasting can be defined as the process through which we can send a single message to n number of recipients at one point of time. Thus it saves a lot of time. It is supported through the wireless data networks. The best example to properly understand this concept is teleconferencing and videoconferencing. But this also doesn’t provide any kind of assurance as so deliver the data or message successfully to all the recipients. Many a time people mix multicasting with broadcasting but they vary from each other and the biggest difference between multicasting and broadcasting is that multicasting is used when we have to send a message to a group of selected recipients whereas broadcasting is used when we are sending a message to everyone who is connected to a network. Multicasting plays an effective role in the areas like distance learning, digital video libraries, online collaboration tools, and other types of advanced applications which are important to research and education.


Personal tools