Module 1.1- Basic Concepts

Document Sample
Module 1.1- Basic Concepts Powered By Docstoc
Lớp Cao học CNTT
(Bổ sung Java)

Trần Đình Quế
Module BS2


     Clients - Servers and Peers – Peers (P2P)
     History of TCP/IP && Internet
     Some examples of Net Programming
     Java – WHY?       Module 1: Basic concepts   3
    Client – Server and Peer to Peer Models

   client/server programming has become very
   widely used in recent years.
   A server: Program provides a service of some
   =/= machine (host machine): the server is
   running the host machine
   Clients that initiate a dialogue with the server.
   Clients are also programs and are not human
   clients!        Module 1: Basic concepts    4
                  C-S & P2P

   Web servers: Common services provided by
   servers via Web pages
   Web browsers: The corresponding client
   programs such as Netscape Communicator or
   Microsoft Explorer
   peer-to-peer (P2P): programs on users’
   machines that are able to communicate
   directly with each other
   World Wide Web simply does not allow clients
   to communicate directly with each other.     Module 1: Basic concepts   5
                  Ports and Sockets

ports and sockets: NOT hardware but they are closely
associated with the hardware communication links
between computers within a network,
ports and sockets: abstract concepts that allow the
programmer to make use of those communication
A port is a logical connection to a computer (as
opposed to a physical connection) is identified by a
number in the range 1-65535.       Module 1: Basic concepts   6
                  Ports and Sockets

 Each port may be dedicated to a particular
 Port numbers in the range 1-1023 are normally for
 the use of specified standard services, 'well-known'
 services. For example, port 80 is normally used by
 Web servers.
 Application programs using ports for non-standard
 services should avoid using port numbers 1-1023.
 For each port supplying a service, there is a server
 program waiting for any requests. All such programs
 run together in parallel on the host machine.       Module 1: Basic concepts    7
                  Ports and Sockets

   The host machine examines the port number and
   passes the client’s transmission to the appropriate
   server program for processing.
   There are likely to be multiple clients wanting the
   same service at the same time.
   Example: Multiple browsers (quite possibly
   thousands of them) wanting Web pages from the
   same server. Then the server needs some way of
   distinguishing between various clients and keeping
   their dialogues separate from each other. This is
   achieved via the use of sockets.        Module 1: Basic concepts     8
                  Ports and Sockets

   A socket is an abstract concept and not an element
   of computer hardware. It is used to indicate one of
   the two end-points of a communication link between
   two processes.
   When a client wishes to make connection to a
   server, it will create a socket at its end of the
   communication link.
   Upon receiving the client's initial request (on a
   particular port number), the server will create a new
   socket at its end for communication with this
   particular client.        Module 1: Basic concepts     9
                  The Internet and IP Addresses

   An internet (lower-case 'i') is a collection of
   computer networks that allows any computer on
   any of the associated networks to communicate
   with any other computer located on any of the
   other associated networks.
   The protocol used for such communication is
   called the Internet Protocol (IP).
   The Internet (upper-case 'I') is the world's largest
   IP-based network. Each computer on the Internet
   has a unique IP address, the current version of
   which is IPv4 (Internet Protocol version 4).              Module 1: Basic concepts   10
                  The Internet and IP Addresses

   IPv4: machine addresses in what is called quad
   notation. This is made up of four eight-bit numbers
   32-bit (i.e., numbers in the decimal range 0-255),
   separated by dots. For example,
   would be one such address.
   Due to a growing shortage of IPv4 addresses, IPv4
   must be replaced with IPv6, the draft standard for
   which was published on the 10th of August, 1998.
   IPv6 uses 128-bit addresses, which provide more
   addresses. Many common Internet applications
   already work with IPv6 and it is expected that IPv6
   will gradually replace IPv4              Module 1: Basic concepts   11
                  Net Programming

Explosion in the growth and use of the Internet
NEED a programming language with features
designed specifically for network programming.
Java provides these features and does so in a
platform-independent manner, which is vital for a
heterogeneous network such as the Internet.
Java is sometimes referred to as 'the language of the
Internet' and it is the use of Java in this context that
has had a major influence on the popularisation of
the language. For many programmers, the need to
program for the Internet is one of the main reasons,
if not the reason, for learning to program in Java.       Module 1: Basic concepts      12
                  Net Programming       Module 1: Basic concepts   13
              Internet Services, URLs and DNS

 When a service provided by a server, there must be
 some established protocol governing the
 Communication that takes place between server
 and client.
 Each end of the dialogue must know what
 may/must be sent to the other, the format in which
 it should be sent, the sequence in which it must be
 sent (if sequence matters) and, for 'open-ended'
 dialogues, how the dialogue is to be terminated.           Module 1: Basic concepts   14
                   Services && Protocols

For the standard
services, such
protocols are made
available in public
documents, by the
Internet Engineering
Task Force (IETF)
or the World Wide
Web Consortium
(W3C).          Module 1: Basic concepts   15

  A URL (Uniform Resource Locator) is a unique
  identifier for any resource located on the Internet.
  It has the following structure in BNF notation
  ame>[#<section>]]      Module 1: Basic concepts    16
TCP/IP - Internet
  An Overview of TCP/IP Protocols
  and the Internet, Gary C. Kessler
  16 January 2007

  A communication protocol is a description of the rules
  computers must follow to communicate with each other.
  The Internet communication protocol defines the rules
  for computer communication over the Internet.                Module 1: Basic concepts   17
The Evolution of TCP/IP (and the Internet)

  Your Browser and Your Server Use TCP/IP: Internet
  browsers and Internet servers use TCP/IP to connect to
  the Internet. Your browser uses TCP/IP to access
  Internet servers, and servers use TCP/IP to send HTML
  back to your browser.
  Your E-Mail Uses TCP/IP: Your e-mail program uses
  TCP/IP to connect to the Internet for sending and
  receiving e-mails.
  Your Internet Address is TCP/IP: Your Internet address
  "" is a part of the standard TCP/IP
  protocol. (And so is your domain name
  "")        Module 1: Basic concepts      18
 The Evolution of TCP/IP (and the Internet)
TCP/IP ( Transmission Control Protocol / Internet
Protocol) is the communication protocol for
communication between computers connected to the
Internet. The standard for devices (like computers)
connecting to the Internet, and how data should be
transmitted between them. Inside TCP/IP
Hiding inside the TCP/IP standard: a number of
protocols for handling data communication:
TCP (Transmission Control Protocol) communication between
UDP (User Datagram Protocol) simple communication between
IP (Internet Protocol) communication between computers
ICMP (Internet Control Message Protocol) for errors and statistics
DHCP (Dynamic Host Configuration Protocol) for dynamic
addressing           Module 1: Basic concepts           19
The Evolution of TCP/IP (and the Internet)   Module 1: Basic concepts   20
The Evolution of TCP/IP (and the Internet)   Module 1: Basic concepts   21
IPv4 address 11000000101010000000001100011000
To convert an IPv4 address from binary notation to dotted
decimal notation:
- Segment it into 8-bit blocks:
        11000000 10101000 00000011 00011000
- Convert each block to decimal: 192 168 3 24
- Separate the blocks with periods:
When referring to an IPv4 address, use the notation w.x.y.z.          Module 1: Basic concepts        22   Module 1: Basic concepts   23
The Evolution of TCP/IP (and the Internet)

  The fundamental technology that makes the
  Internet work is called packet switching (1960s,
  packet switching was discovered)
  In 1961, Leonard Kleinrock of MIT published the
  first paper on packet switching theory (and the first
  book on the subject in 1964).
  In 1962, Paul Baran of the Rand Corporation
  described a robust, efficient, store-and-forward
  data network in a report for the U.S. Air Force.        Module 1: Basic concepts      24
The Evolution of TCP/IP (and the Internet)

  1962: Donald Davies and Roger Scantlebury
  suggested a similar idea from work at the National
  Physical Laboratory (NPL) in the U.K.
  The research at MIT (1961-1967), RAND (1962-
  1965), and NPL (1964-1967) occurred
  independently and the principal researchers did
  not all meet together until the Association for
  Computing Machinery (ACM) meeting in 1967.
  The term packet was adopted from the work at
  NPL.       Module 1: Basic concepts    25
                  TCP/IP history

  The modern Internet began as a U.S. Department
  of Defense (DoD) funded experiment to
  interconnect DoD-funded research sites in the
  The 1967 ACM meeting was also where the initial
  design for the so-called ARPANET — named for
  the DoD's Advanced Research Projects Agency
  (ARPA) — was first published by Larry Roberts.
  In December 1968, ARPA awarded a contract to
  Bolt Beranek and Newman (BBN) to design and
  deploy a packet switching network with a
  proposed line speed of 50 kbps.
                     Module 1: Basic concepts   26
                  TCP/IP history

  In September 1969, the first node of the ARPANET
  was installed at the University of California at Los
  Angeles (UCLA), followed monthly with nodes at
  Stanford Research Institute (SRI), the University of
  California at Santa Barbara (UCSB), and the
  University of Utah.
  With four nodes by the end of 1969, the ARPANET
  spanned the continental U.S. by 1971 and had
  connections to Europe by 1973.       Module 1: Basic concepts     27
                  TCP/IP – Internet history

  The initial host-to-host communications protocol
  introduced in the ARPANET was called the
  Network Control Protocol (NCP).
  However, NCP proved to be incapable of keeping
  up with the growing network traffic load. In 1974, a
  new, more robust suite of communications
  protocols was proposed and implemented
  throughout the ARPANET, based upon the
  Transmission Control Protocol (TCP) for end-to-
  end network communication.           Module 1: Basic concepts   28
                  TCP/IP – Internet history
But it seemed like overkill for the intermediate
gateways – routers, to needlessly have to deal with
an end-to-end protocol
1978 a new design split responsibilities between a
pair of protocols; the new Internet Protocol (IP) for
routing packets and device-to-device communication
and TCP for reliable, end-to-end host
Since TCP and IP were originally envisioned
functionally as a single protocol, the protocol suite,
which actually refers to a large collection of
protocols and applications, is usually referred to
simply as TCP/IP.           Module 1: Basic concepts   29
                  TCP/IP – Internet history

  The original versions of both TCP and IP that are
  in common use today were written in September
  1981, although both have had several
  modifications applied to them IPv6 (specification
  was released in December 1995).
  In 1983, the DoD mandated that all of their
  computer systems would use the TCP/IP protocol
  suite for long-haul communications, further
  enhancing the scope and importance of the
  ARPANET.           Module 1: Basic concepts   30
                  TCP/IP – Internet history

  1983, the ARPANET was split into two components.
  One component, still called ARPANET, was used to
  interconnect research/development and academic
  sites; the other, called MILNET, was used to carry
  military traffic and became part of the Defense Data
  1983 a huge boost in the popularity of TCP/IP with
  its inclusion in the communications kernel for the
  University of California’s UNIX implementation,
  4.2BSD (Berkeley Software Distribution) UNIX.           Module 1: Basic concepts   31
  1986, the National Science Foundation (NSF) built a
  backbone network to interconnect four NSF-funded
  regional supercomputer centers and the National
  Center for Atmospheric Research (NCAR)
  This network, dubbed the NSFNET, was originally
  intended as a backbone for other networks, not as
  an interconnection mechanism for individual
  systems. Furthermore, the "Appropriate Use Policy"
  defined by the NSF limited traffic to non-commercial
  use. The NSFNET continued to grow and provide
  connectivity between both NSF-funded and non-
  NSF regional networks, eventually becoming the
  backbone that we know today as the Internet.       Module 1: Basic concepts    32
Although early NSFNET applications were largely
multiprotocol in nature, TCP/IP was employed for
interconnectivity (with the ultimate goal of migration to
Open Systems Interconnection).
NSFNET originally comprised 56-kbps links and was
completely upgraded to T1 (1.544 Mbps) links in 1989.
Migration to a "professionally-managed" network was
supervised by a consortium comprising Merit (a Michigan
state regional network headquartered at the University of
Michigan), IBM, and MCI. Advanced Network & Services,
Inc. (ANS), a non-profit company formed by IBM and MCI,
was responsible for managing the NSFNET and
supervising the transition of the NSFNET backbone to T3
(44.736 Mbps) rates by the end of 1991.       Module 1: Basic concepts      33

  1993, the NSF decided that it did not want to be in
  the business of running and funding networks, but
  wanted instead to go back to the funding of research
  in the areas of supercomputing and high-speed
  There was increased pressure to commercialize the
  Internet; in 1989, a trial gateway connected MCI,
  CompuServe, and Internet mail services, and
  commercial users were now finding out about all of
  the capabilities of the Internet that once belonged
  exclusively to academic and hard-core users!       Module 1: Basic concepts    34

  In 1991, the Commercial Internet Exchange (CIX)
  Association was formed by General Atomics,
  Performance Systems International (PSI), and
  UUNET Technologies to promote and provide a
  commercial Internet backbone service.
  Nevertheless, there remained intense pressure from
  non-NSF ISPs to open the network to all users.      Module 1: Basic concepts   35
                  TCP/IP   Module 1: Basic concepts   36

Application          HTTP FTP Telnet Finger SSH DNS             DNS SNMP RIP
                       POP3/IMAP SMTP Gopher BGP                RADIUS Archie     Ping
     Layer             Time/NTP Whois TACACS+ SSL               Traceroute tftp

 Transport                         TCP                                  UDP       ICMP   OSPF

   Internet                                            IP                                       ARP

   Network                  Ethernet/802.3 Token Ring (802.5) SNAP/802.2 X.25 FDDI ISDN
                                Frame Relay SMDS ATM Wireless (WAP, CDPD, 802.11)
  Interface                  Fibre Channel DDS/DS0/T-carrier/E-carrier SONET/SDH DWDM
     Layer                       PPP HDLC SLIP/CSLIP xDSL Cable Modem (DOCSIS)                           Module 1: Basic concepts                                 37

  The Internet is a packet-switched network (PSN).
  PSN: Messages between computers on the
  Internet are broken up into blocks of information
  called packets
  Each packet being handled separately and
  possibly traveling by a completely different route.
  IP is concerned with the routing of these packets
  through an internet.       Module 1: Basic concepts     38

 TCP layer involves breaking up the data
 packets into TCP segments and adding
 sequence numbers and checksums.
 IP layer involves placing the TCP segments
 into IP packets called datagrams and adding
 the routing details. The host-to-network layer
 then converts the digital data into an
 analogue form suitable for transmission over
 the carrier wire, sends the data and converts
 it back into digital form at the receiving end.     Module 1: Basic concepts   39
                  TCP/IP   Module 1: Basic concepts   40

      Another network model that is often referred to is
      the seven-layer Open Systems Interconnection
      (OSI) model. However, this model is an
      unnecessarily complex one for our purposes and
      is better suited to non-TCP/IP networks anyway.          Module 1: Basic concepts     41

 Internet applications use TCP as their transport
 TCP is a relatively slow transport mechanism. For
 many applications (e.g., file transfer), this does not
 really matter greatly.
 However, TCP is not suitable for applications such
 as the playing of audio and video while the
 associated files are being downloaded, via what is
 called streaming. One of the most popular
 streaming technologies is called RealAudio.
 RealAudio does not use TCP. This and other such
 applications use User Datagram Protocol (UDP).
                       Module 1: Basic concepts      42

UDP is an unreliable protocol, since:
- it doesn't guarantee that each packet will arrive;
- it doesn't guarantee that packets will be in the right
- it doesn't re-send a packet if it is missing or there is
some other error
- it doesn't assemble packets into the correct order.
However, it is significantly faster than TCP.
Applications such as the streaming of audio or video,
losing a few bits of data is much better than waiting for
re-transmission of the missing data.        Module 1: Basic concepts      43

 HOW those protocols may be used and the
 techniques implemented in Java?
 Core package contains a number of very
 useful classes that allow programmers to carry out
 network programming very easily.
 Package, introduced in J2SE 1.4, contains
 factory classes for creating sockets in an
 implementation-independent fashion.
 Using classes from these packages, the network
 programmer can communicate with any server on
 the Internet or implement his/her own Internet      Module 1: Basic concepts    44
                  InetAddress Class is called InetAddress, which handles
 Internet addresses both as host names and as IP
 Static method getByName of this class uses DNS
 (Domain Name System) to return the Internet
 address of a specified host name as an
 InetAddress object.
 In order to display the IP address from this object,
 we can simply use method println        Module 1: Basic concepts     45
import java.util.*;

public class IPFinder {
public static void main(String[] args) {
String host;
Scanner input = new Scanner(;
System.out.print("\n\nEnter host name: ");
host =;
InetAddress address = InetAddress.getByName(host);
System.out.println("IP address: “ + address.toString());
catch (UnknownHostException uhEx) {
System.out.println("Could not find " + host);
}                 Module 1: Basic concepts   46
    IP Address


public class MyLocalIPAddress {
  public static void main(String[] args) {
InetAddress address = InetAddress.getLocalHost();
 catch (UnknownHostException uhEx){
 System.out.println("Could not find local address!");
}                 Module 1: Basic concepts   47
                  Using Sockets

      Different processes (programs) can
      communicate with each other across
      networks by means of sockets.
      Java implements both TCP/IP sockets and
      datagram sockets (UDP sockets).      Module 1: Basic concepts   48
                  TCP Sockets

 A communication link created via TCP/IP sockets
 is a connection-orientated link.
 Connection between server and client remains
 open throughout the duration of the dialogue
 between the two and is only broken (under normal
 circumstances) when one end of the dialogue
 formally terminates the exchanges
 Since there are two separate types of process
 involved (client and server), we shall examine them
 separately      Module 1: Basic concepts     49
                   Set up server Process

1.    Create a ServerSocket object.
2.    Put the server into a waiting state.
3.    Set up input and output streams.
4.    Send and receive data
5.    Close the connection (after completion of the
      dialogue).          Module 1: Basic concepts    50
                  SERVER Process
1. Create a ServerSocket object.
   The ServerSocket constructor requires a port number
   (1024-65535, for non-reserved ones) as an argument.
   For example:
   ServerSocket servSock = new ServerSocket(1234);
   In this example, the server will await ('listen for') a
   connection from a client on port 1234.
2. Put the server into a waiting state.
   The server waits indefinitely ('blocks') for a client to
   connect. It does this by calling method accept of class
   ServerSocket, which returns a Socket object when
   aconnection is made. For example:
   Socket link = servSock.accept();           Module 1: Basic concepts            51
3. Set up input and output streams.
   Scanner input = new Scanner(link.getInputStream());
   PrintWriter output = new PrintWriter(link.getOutputStream(),true);
   Methods getInputStream, getOutputStream: get references to
   streams associated with the socket returned in step 2.
   These streams will be used for communication with the client.
   Wraping a Scanner object around the InputStream object
   returned by method getInputStream, in order to obtain string-
   orientated input (just as we would do with input from the
   standard input stream, Wraping a PrintWriter
   object around the OutputStream object returned by method
   getOutputStream. Supplying the PrintWriter constructor with a
   second argument of true will cause the output buffer to be
   flushed for every call of println           Module 1: Basic concepts          52
4. Send and receive data.
  Having set up our Scanner and PrintWriter objects,
  sending and receiving data is very straightforward. We
  simply use method nextLine for receiving data and
  method println for sending data, just as we might do for
  console I/O. For example:
  output.println("Awaiting data...");
  String input = input.nextLine();
5. Close the connection (after completion of the
  This is achieved via method close of class Socket. For
  link.close();          Module 1: Basic concepts           53
                  Set up client Process

 Setting up the corresponding client
 involves four steps...

 1.       Establish a connection to the server.
 2.       Set up input and output streams.
 3.       Send and receive data.
 4.       Close the connection.            Module 1: Basic concepts   54
1. Establish a connection to the server.
  We create a Socket object, supplying its constructor
  with the following two arguments:
  - The server's IP address (of type InetAddress);
  - The appropriate port number for the service.
  (The port number for server and client programs
  must be the same)
 For simplicity's sake, we shall place client and server on the
 same host, method getLocalHost of class InetAddress. For
 Socket link = newSocket(InetAddress.getLocalHost(),1234);         Module 1: Basic concepts         55
2. Set up input and output streams.
Similar to the server streams set up (by
calling methods getInputStream && getOutputStream
of the Socket object that was created in step 2).
3. Send and receive data.
The Scanner object at the client will receive messages sent
by the PrintWriter object at the server while the PrintWriter
object at the client will send messages that are received by
the Scanner object at the server end (using methods nextLine
and println respectively).
4. Close the connection.
This is exactly the same as for the server process
(using method close of class Socket).           Module 1: Basic concepts        56

      In this simple example, the server will accept
      messages from the client and will keep count of
      those messages, echoing back each
      (numbered) message.
      The main protocol for this service is that client
      and server must alternate between sending and
      receiving (with the client initiating the process
      with its opening message, of course).          Module 1: Basic concepts     57

 TCPEchoServer   Module 1: Basic concepts   58
                  Example: Loading web with GUI

      A program (like browser) that can load web page
      to your computer              Module 1: Basic concepts   59   Module 1: Basic concepts   60

      JAVA again ☺
      Hello program (Compile, Interpret)
      Object – class
      Java classes
      Package - class         Module 1: Basic concepts   61

Shared By: