IPT Chapter 1 - Network Programming & Integrative Coding
IPT Chapter 1 - Network Programming & Integrative Coding
Technologies
Mettu University, Department of Information
Technology
4th Year
Chapter 1 - Network Programming & Integrative Coding
Prepared by: Sridhar.U
Network Programming
Network:
– A network is a collection of computers and other
devices that can send data to and receive data from
each other.
– A network is often connected by wires.
– However, wireless networks transmit data through
infrared light and microwaves.
Node:
– Each machine on a network is called a node.
– Most nodes are computers, but printers, routers,
bridges, gateways etc.. can also be nodes.
– Nodes that are fully functional computers are also
called hosts.
Network Programming cont..
Packet:
– All modern computer networks are packet-switched
networks: data traveling on the network is broken
into chunks called packets and each packet is handled
separately.
– Each packet contains information about who sent it
and where it's going.
Protocol:
– A protocol is a precise set of rules defining how
computers communicate: the format of addresses, how
data is split into packets
IP:
– IP was designed to allow multiple routes between any
two points and to route packets of data around
damaged routers.
Network Programming cont..
TCP:
– Since there are multiple routes between two points,
and the packets that make up a particular data
stream.
– Furthermore, they may not arrive in the order they
were sent, if they even arrive at all.
UDP:
– UDP is an unreliable protocol that does not guarantee
that packets will arrive at their destination or that
they will arrive in the same order they were sent.
Internet:
– largest IP-based network for connecting machines
together.
– Java: easy-to-use, cross-platform model for network
communications.
Network Programming cont..
• Ports:
– Each computer with an IP address has several thousand
logical ports.
– Each port is identified by a number between 1 and 65,535.
Each port can be allocated to a particular service.
– Port numbers 1 through 255 are reserved by IP for well-known
services
– If you connect to port 80 of a host, for instance, you may
expect to find an HTTP server.
What is a Socket?
– Sockets are a means of using IP to communicate between
machines, so sockets allow Java to interoperate with legacy
systems by simply talking to existing servers using their
pre-defined protocol.
– Internet protocol: User Datagram Protocol (UDP) and
Transmission Control Protocol (TCP).
Network Programming cont..
• Internet Addresses or IP Addresses
– Every network node has an address, a series of bytes
that uniquely identify it.
– Internet addresses are manipulated in Java by the use of
the InetAddress class. InetAddress takes care of the
Domain Name System (DNS) look-up and reverse look-up;
– IP addresses can be specified by either the host name or
the raw IP address.InetAddress provides methods to
getByName(), getAllByName(), getLocalHost(),
getAddress(), etc.
– IP addresses are a 32-bit number, often represented as a
"quad" of four 8-bit numbers separated by periods.
– They are organized into classes (A, B, C, D, and E). For
example 126.255.255.255 Client/Server Computing- Java
language communicate with remote file system
UDP - How UDP clients and UDP servers
communicate over sockets
Creating UDP Servers:
To create a server with UDP, do the following:
1. Create a DatagramSocket attached to a port.
int port = 1234;
DatagramSocket socket = new DatagramSocket(port);
2. Allocate space to hold the incoming packet, and create an instance of
DatagramPacket to hold the incoming data.
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
3. Block until a packet is received, then extract the information you need from the
packet.
// Block on receive()
socket.receive(packet);
// Extract the packet data
byte[] data = packet.getData();
// Find out where packet came from
// so we can reply to the same host/port
InetAddress remoteHost = packet.getAddress();
int remotePort = packet.getPort();
UDP - How UDP clients and UDP servers
communicate over sockets
Creating UDP Clients
1. First allocate space to hold the data we are sending and create an
instance of DatagramPacket to hold the data.
byte[] buffer = new byte[1024];
int port = 1234;
InetAddress host = InetAddress.getByName(“mywebsite.com");
DatagramPacket packet = new DatagramPacket(buffer,
buffer.length, host, port);
2. Create a DatagramSocket and send the packet using this socket.
DatagramSocket socket = new DatagramSocket();//free local port to use
socket.send(packet);
// Find out where we are sending from
InetAddress localHostname = socket.getLocalAddress();
int localPort = socket.getLocalPort();
How TCP clients and TCP servers
communicate over sockets
Creating TCP Servers:
To create a TCP server, do the following:
1. Create a ServerSocket attached to a port number.
ServerSocket server = new ServerSocket(port);
2. Wait for connections from clients requesting connections to that port.
// Block on accept()
Socket channel = server.accept();
You'll get a Socket object as a result of the connection.
3. Get input and output streams associated with the socket.
out = new PrintWriter (channel.getOutputStream());
out.println("Hey! I heard you over this socket!");
reader = new InputStreamReader (channel.getInputStream());
in = new BufferedReader (reader);
Now you can read and write to the socket, thus, communicating with
the client.
String data = in.readLine();
How TCP clients and TCP servers
communicate over sockets
Creating TCP Clients:
To create a TCP client, do the following:
1. Create a Socket object attached to a remote host, port.
Socket client = new Socket(host, port);
When the constructor returns, you have a connection.
2. Get input and output streams associated with the socket.
out = new PrintWriter (client.getOutputStream());
out.println(“Hi welcome");
reader = new InputStreamReader (client.getInputStream());
in = new BufferedReader (reader);
Now you can read and write to the socket, thus, communicating with
the server.
String data = in.readLine();
DESIGN PATTERNS
Design Patterns
Design pattern:
– A Lower level framework for structuring an
application than architectures (Sometimes, called
micro-architecture).
– Reusable collaborations that solve sub problems
within an application.
Why Design Patterns?
– Design patterns support object-oriented reuse at a
high level of abstraction
– Design patterns provide a “framework” that guides and
constrains object-oriented implementation
Design Pattern Description Template
Organizing Design Pattern
The Gang of Four (GoF) Design Patterns book describes
twenty-three
patterns arranged into three groups.
The groups help classify how the patterns are used.
1. Creational patterns : used to help make a system
independent of how its objects are created, composed
and represented.
2. Structural patterns are concerned with how classes and
objects are organized and composed to build larger
structures.
3. Behavioral patterns are used to deal with assignment of
responsibilities to objects and communication between
objects.
Example for Design Pattern
Creational Patterns
– Abstract Factory - create instances of other objects
Eg:-creating GUI components for different GUI toolkits
– Factory Method -common interface for creating subclasses
– Singleton -create only one instance of a class
Structural Patterns
– Decorator - add more responsibilities to an object dynamically
• Eg:- adding scrolling to a text view
– Facade- higher level unified interface to a set of objects in a
subsystem
– Proxy- interface layer between objects
Behavioral Patterns
– Iterator- a means to access all the elements of objects
sequentially
– Momento- capture and save the current state of an object
– Observer- when any numbers of objects (the Observers) need to be
notified automatically
Interfaces
• Application Programming Interfaces
– Are sets of requirements that govern how one application can
talk to another
– applications to share data and take actions on one another's
behalf without requiring developers to share all of their
software's code
– define exactly how a program will interact with the rest of
the software world—saving time, resources
– Eg:- System-level APIs- cut and paste LibreOffice document
into an Excel spreadsheet
– Eg:-FacebookAPIs- Facebook users sign into many apps and Web
sites using their Facebook ID
– Eg:-Web APIs – games let players chat, post high scores and
invite friends to play via Face book, right there in the
middle of a game
Inheritance
Inheritance
– derive a new class based on an existing class, with
modifications or extensions
– A subclass inherits all the variables and methods from its
super classes, including its immediate parent as well as all
the ancestors
– avoid duplication and reduce redundancy
Types of Inheritance
– Simple , Multilevel, Multiple, hierarchical and Hybrid
Inheritance and Abstract class
– Abstract Method:- a method with only signature (i.e., the
method name, the list of arguments and the return type)
without implementation (i.e., the method’s body).
– use the keyword abstract to declare an abstract method
Interface cont..
Abstract Class
– A class containing one or more abstract methods is called an
abstract class.
– must be declared with a class-modifier abstract
– provides a template for further development
Notes:
– An abstract method cannot be declared final, as final method
cannot be overridden.
– An abstract method must be overridden in a descendent before
it can be used.
– An abstract method cannot be private (which generates a
compilation error, because private method is not visible to
the subclass and thus cannot be overridden.
– In Java, define a subclass using the keyword "extends",
e.g.,
class MyApplet extends java.applet.Applet {.....}
class Cylinder extends Circle {......}
Versioning and Version Control
– Version control enables multiple people to simultaneously
work on a single project.
– Each person edits his or her own copy of the files and
chooses when to share those changes with the rest of the
team.
– temporary or partial edits by one person do not interfere
with another person's work.
– enables one person to use multiple computers to work on a
project
– integrates work done simultaneously by different team
members
– In rare cases, when two people make conflicting edits to the
same line of a file, then the version control system
requests human assistance in deciding what to do
– Version control gives access to historical versions of the
project
Versioning and Version Control cont..
– If make a mistake, roll back to a previous version.
reproduce and understand a bug report on a past version of
your software.
– undo specific edits without losing all the work that was
done in the meanwhile.
– For any part of a file, determine when, why, and by whom it
was ever edited.
– Version control uses a repository (a database of changes)
and a working copy (checkout) where you do your work
– working copy is your personal copy of all the files in the
project.
– edits to this copy, without affecting your teammates. commit
your changes to a repository
– repository is database of all the edits to, and/or
historical versions (snapshots) of, your project
– update your working copy to incorporate any new edits or
versions
Versioning and Version Control cont..
– Two varieties of version control: centralized (one
repository) and distributed (multiple repositories)
– Some popular version control systems are Mercurial
(distributed), Git (distributed), and Subversion
(centralized).
– The main difference between centralized and distributed
version control is the number of repositories.
– In centralized version control, there is just one
repository, and in distributed version control, there are
multiple repositories.
THANK YOU