Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

 

            kcc.edu.np  
 
 
 
 
 
 
 
 
 
 

NETWORK  PROGRAMMING  
BCA  -­‐  VI  SEMESTER  
 
 
 
 
 
 
 
 
 
 
 
 
iSaroz    
sarojpandey.com.np    

  sarojpandey.com.np  -­‐  1  
              kcc.edu.np  
Network  Programming  -­‐  BCA  352  CS  -­‐  Year:  III  Semester:  II  
 

Full  Marks:  100  [Internal:  40  &  External:  60]  


Objectives:  
At  the  end  of  this  course,  the  students  would  be  able  to  design  and  implement  network  client  server  
applications.  
 

Contents:  
1.  Introduction  to  Network  Programming:  [5  Hrs.]  
Introduction  to  Computer  Network:  Client  /Server  Model,  Protocol  Suite  (ISO/OSI,  TCP/IP),  Unix  
Standards  (POSIX,  Open  Group,  IETF),  Network  Utilities  (telnet,  netstat,  ifconfig,  ping,  ftp)  
 

Introduction  to  Programming:  Programming  Standards,  Wrapper  functions,  Header  files,  


libraries  and  program  documentation.  
2.  Elementary  Operating  System  Calls:  [5  Hrs.]  
Elementary  Operating  System  Calls:  fork,  exec  and  its  family,  waitpid,  wait,  pipes,  fifo,  signals  
(SIGCHLD,  SIGINT,  SIGIO),  read,  write,  readn,  writen.  
3.  TCP/UDP  Transport  Layer  Protocols:  [4  Hrs.]  
TCP  (Transmission  Control  Protocol):  features,  connection  establishment  and  termination,  
states  in  communication  (LISTEN,  TIME_WAIT,  ESTABLISHED,  BLOCKED)  
 

UDP  (User  Datagram  Protocol):  features,  uses,  comparison  with  TCP.  TCP  and  UDP  Buffer  sizes  
and  limitations.  
4.  Elementary  Socket  Calls:  [4  Hrs.]  
Socket  Address  Structure:  for  IPV4,  IPV6,  UNIX  domain  socket  and  Generic  socket  address  
structure,  Value-­‐result  argument.  Byte  Ordering  and  manipulating  function:  htonl,  htons,  ntohl,  
ntohs,  inet_addr,  inet_aton,  inet_ntoa,  inet_pton.  
5.  TCP  Socket  Calls:  [4  Hrs.]  
Socket,  function,  connect  function,  bind  function,  listen  function,  accept  function,  close  function,  
getsock_name  and  get_peer_name  function  (Example:  echo  server  and  client).  
6.  I/O  multiplexing  withSelect  &  Poll:  [3  Hrs.]  
I/O  models:  Blocking  I/O,  non-­‐blocking  I/O,  I/O  multiplexing,  signal  driven  I/O  (SIGIO)  and  
Asynchronous  I/O  model.  
 

Introductions  to  select  function,  shutdown  function  and  poll  function.  


7.  Socket  Options:  [2  Hrs.]  
Getsockopt  and  setsockopt  functions,  Generic  IPV$  socket  options,  TCP  socket  options.  
   
  sarojpandey.com.np  -­‐  2  
              kcc.edu.np  
8.  UDP  Sockets  Calls  for  data  transmission  and  connection:  [3  Hrs.]  
Socket  calls  recv_from  and  send_to  functions,  UDP  client  with  connect,  Determine  outgoing  
interface  with  UDP.  Protocol  Independent  Services.  
9.  Name  and  Address  Conversion:  [2  Hrs.]  
Basic  Name  and  Address  Conversion  Function:  Review  of  Domain  name  system,  gethostbyname,  
gethostbyaddr,  uname  function,  getservbyname  and  getservbyport  function.  
 

Other  I/O  functions:  recv,  send,  readv,  writev,  recvmsgsndmsg,  socket  timeouts  and  data  
handling.  
10.  Unix  Domain  Protocol:  [3  Hrs.]  
Unix  Domain  socket  address  structure,  socket  pair  function,  socket  functions,  UDP  Stream  
Client/Server,  Unix  domain  datagram,  passing  descriptor.  
11.Client  and  Server  Design  Alternative:  [2  Hrs.]  
TCP  Client  Alternative,  Iteration  Server,  Concurrent  server,  Networked  servers.  
12.  Daemon  processes,  inetd  super  servers:  [2  Hrs.]  
Sysloged  (syslog  function),  daemon_init  function,  inetd  daemon.  
13.Broadcast  and  Multicast:  [3  Hrs.]  
Introduction,  Broadcast  and  Multicast  addresses,  comparison  between  broadcast,  unicast  and  
Multicast  socket  options.  
14.  IP  Layers  and  Raw  Socket:  [3  Hrs.]  
Introduction,  Raw  socket  creation,  Input  and  Output  (ping  example).  
 
Lab  Exercise:  Laboratories  of  elementary  or  system  calls,  TCP  and  UDP  socket  including  select  and  poll,  
socket  options  and  name  conversion  strictly  using  C/C++  and  Linux.  
 
Class  project:  Project  class  includes  the  project  covering  the  advanced  topics  in  Windows  /  Linux  /  
Unix  Environment  using  Java/C/C++.  
 
Textbooks:  
1.  Stevens  W.R.,  "Unix  Network  Programming",  Vol-­‐1.  
2.  Stevens  W.R.,  "Unix  Network  Programming",  Vol-­‐II  
3.  Doglous  E.  Comer,  "Internetworking  with  TCP/IP",  Vol  -­‐  III  
   

  sarojpandey.com.np  -­‐  3  
              kcc.edu.np  
CHAPTER  01:  Introduction  to  Network  Programming  
Introduction  to  Computer  Network  
A  computer   network,   or   simply   a   network,   is   a   collection   of  computers  and   other   hardware  
interconnected   by   communication   channels   that   allow   sharing   of   resources   and   information.  
Where   at   least   one   process   in   one   device   is   able   to   send/receive   data   to/from   at   least   one  
process   residing   in   a   remote   device,   then   the   two   devices   are   said   to   be   in   a   network.   A   network  
is  a  group  of  devices  connected  to  each  other.  Networks  may  be  classified  into  a  wide  variety  of  
characteristics,   such   as   the   medium   used   to   transport   the   data,   communication   protocol   used,  
scale,  topology,  benefit,  and  organizational  scope.  
 
Client  /Server  Model  
The  term  client-­‐server  refers  to  a  popular  model  for  computer  networking  that  utilizes  client  and  
server  devices  each  designed  for  specific  purposes.  The  client-­‐server  model  can  be  used  on  the  
Internet  as  well  as  local  area  networks  (LANs).  Examples  of  client-­‐server  systems  on  the  Internet  
include  Web  browsers  and  Web  servers,  FTP  clients  and  servers,  and  the  DNS.  
 

The  model  assigns  one  of  two  roles  to  the  computers  in  a  network:  Client  or  server.  A  server  is  a  
computer   system   that   selectively   shares   its  resources;   a  client  is   a   computer   or  computer  
program  that   initiates   contact   with   a   server   in   order   to   make   use   of   a   resource.  Data,  
CPUs,  printers,  and  data  storage  devices  are  some  examples  of  resources.  
 

This  sharing  of  computer  resources  is  called  time-­‐sharing,  because  it  allows  multiple  people  to  
use   a   computer   (in   this   case,   the   server)   at   the   same   time.   Because   a   computer   does   a   limited  
amount   of   work   at   any   moment,   a   time-­‐sharing   system   must   quickly   prioritize   its  tasks   to  
accommodate  the  clients.  

Clients   and   servers   exchange   messages   in   a  request-­‐response  messaging   pattern:   The   client  
sends  a  request,  and  the  server  returns  a  response.  To  communicate,  the  computers  must  have  a  
common  language,  and  they  must  follow  rules  so  that  both  the  client  and  the  server  know  what  
to  expect.  The  language  and  rules  of  communication  are  defined  in  a  communications  protocol.  
All  client-­‐server  protocols  operate  in  the  application  layer.  
   

Whether  a  computer  is  a  client,  a  server,  or  both,  it  can  serve  multiple  functions.  For  example,  a  
single  computer  can  run  web  server  and  file  server  software  at  the  same  time  to  serve  different  
data   to   clients   making   different   kinds   of   requests.   Client   software   can   also   communicate   with  

  sarojpandey.com.np  -­‐  4  
              kcc.edu.np  
server   software   on   the   same   computer.  Communication   between   servers,   such   as   to   synchronize  
data,  is  sometimes  called  inter-­‐server  or  server-­‐to-­‐server  communication.  

Role  of  Clients  

• Establish  a  connection  with  the  server,  given  its  address.  

• After  connection  establishment,  receive  commands  from  the  user,  perform  necessary  translation  
as  per  the  protocol  and  send  them  to  the  server.  

• Receive  messages  from  the  server,  translate  them  to  human  readable  form  and  display  them.  

This  can  be  summarized  as:  

     get  the  server's  address  

       form  a  working  address  that  can  be  used  to  talk  over  Internet.  

       connect  to  the  server  

       while  (not  finished)  do:  

           wait  until  there's  either  information  from  the  server,  or  from  the  

                   user.  

           If  (information  from  server)  do  

               parse  information  

               show  to  user,  update  local  state  information,  etc.  

           else  {we've  got  a  user  command}  

               parse  command  

               send  to  server,  or  deal  with  locally.  

       done  

Role  of  Servers  

• Accept  connection  request  from  a  client.  

• Receive  request  from  the  client  and  return  results.  

• Close  connection  when  done.  

  sarojpandey.com.np  -­‐  5  
              kcc.edu.np  
this  forms  the  main  loop  a  Multi-­‐Client  Server  performs:    

     bind  a  port  on  the  computer,  so  Clients  will  be  able  to  connect  

       forever  do:  

           listen  on  the  port  for  connection  requests.  

           accept  an  incoming  connection  request  

           if  (this  is  an  authorized  Client)  

               while  (connection  still  alive)  do:  

                   receive  request  from  client  

                   handle  request  

                   send  results  of  request,  or  error  messages  

               done  

           else  

               abort  the  connection  

       done  

Multi  Client  Servers  


These  are  servers  that  talk  to  a  several  Clients  at  the  same  time.  They  need  to  be  able  to:  
• Accept  new  connection  requests  from  Clients.  
• Receive  requests  from  any  Client  and  return  results.  
• Close  any  connection  that  the  client  wants  to  end.  
this  forms  the  main  loop  a  Multi-­‐Client  Server  performs:    
 
   bind  a  port  on  the  computer,  so  Clients  will  be  able  to  connect  
   listen  on  the  port  for  connection  requests.  
   forever  do:  
       wait  for  either  new  connection  requests,  or  requests  from  existing  
                       Clients.  
       if  (this  is  a  new  connection  request)  
           accept  connection  

  sarojpandey.com.np  -­‐  6  
              kcc.edu.np  
           if  (this  is  an  un-­‐authorized  Client)  
               close  the  connection  
       else  if  (this  is  a  connection  close  request)  
           close  the  connection  
       else  {  this  is  a  request  from  an  existing  Client  connection}  
           receive  request  from  client  
           handle  request  
           send  results  of  request,  or  error  messages  
   done  
 
Client   Server  

• Process  that  request  for  a  service   • Process  that  accepts  the  client  
request,  handles  them  and  provides  
the  service  
• Initiates  the  connection   • Do  not  initiate  the  connection,  rather  
establishment   responds  to  the  connection  request  
from  the  clients.  
• Runs  for  comparatively  short  time   • Runs  for  longer  time;  some  server  
never  terminate  
 
Protocol  Suite  (ISO/OSI,  TCP/IP)    
ISO/OSI  REFERENCE  MODEL  
Transferring   of   data   between   different   communications   devices   is   divided   into   logical   layers   called   OSI  
layers;   these   layers   are   seven   in   number.   OSI   model   stands   for  Open   Systems   Interconnection,  Xerox  
Corporation   and   Intel   designed   it   in   1984   and   later   ISO   (International   Standard   Organization)  
standardized  it.  OSI  is  logical  framework  for  standards  for  computer  networks  and  data  communication.  
OSI  model  is  used  as  set  standards  for  computer  networking  worldwide.  Major  networking  protocols  are  
specifically  designed  keeping  OSI  model  in  view.  
 
Their  principles   are  as  follows:  
1.     A  layer  should  be  created  where  a  different   level  of  abstraction   is  needed.  
2.     Each  layer  should  perform  a  well-­‐defined  function.  
3.     The  function  of  each  layer  should  be  defined  by  internationally  standardized   protocols.  
4.     The   layer   boundaries   should   be   chosen   to   reduce   the   information   flow   across   the  
interfaces.  

  sarojpandey.com.np  -­‐  7  
              kcc.edu.np  
5.     The   number   of   layers   should   be   large   enough   that   some   distinct   functions   not   be  
thrown   t o g e t h e r   i n   t h e   s a m e   l a y e r   o f   necessity   a n d   s m a l l   e n o u g h   t h a t   t h e  
architecture  does  not  become   bulky.  
 
The  OSI’s  7-­‐layer  reference  model  is  an  attempt  to  abstract  features  common  to  all  approaches  to  data  
communications,  and  organize  them  into  layers  or  modules  such  that  each  layer  only  worries  about  the  
layer  directly  above  it  and  the  one  directly  below  it.  Those  7  layers  are  as  follows:    
 
1. Physical  layer  

2. Data  Link  layer  

3. Network  layer  

4. Transport  layer  

5. Session  layer  

6. Presentation  layer  

7. Application  layer  

  sarojpandey.com.np  -­‐  8  
              kcc.edu.np  
These   layers   helps   dividing   different   tasks   of   communication   and   transferring   data   within   the   network   into  
subtasks,   that’s   how   one   completes   cycle   of   communications   between   network(s)   is   completed.   Each   layer  
completes  its  tasks  independently.  These  layers  are  also  divided  into  upper  and  lower  layers.  
 
Physical  Layer:  Layer  1  
As   the   name   suggests,   Physical   layer   handles   all   sorts   of   physical   aspects   including,   networking   cables,  
networking  cards/adaptors  and  other  physical  devices  require  for  networking.  It  defines  bit  steam  on  physical  
media.  It  is  responsible  for  providing  interfaces  between  networks  and  network  communication  devices.  Physical  
layer  also  handles  mechanical,  electrical  and  optical  aspects  on  the  network.   It  is  concerned  w i t h  transmitting  
r a w  b its  o ver  a  communication   channel.  
 
Data  Link  Layer:  Layer  2  
Communication  links  and  its  procedures  are  handled  by  data  link  layers.  The  main  function  of  the  data  -­‐link  layer  
is   to   take   a   raw   transmission   facility   and   transform   it   into   a   line   that   appears   free   of   undetected   transmission  
errors  to  the  network  layer.  It  is  done  by  breaking  the  input  data  into  data  frames  (typically  a  few  hundred  or  a  
few  thousand  bytes),  transmit  the  frames  subsequently  and  process  the  acknowledgement  frames  sent  back  by  
the  receiver.  
The  physical  layer  merely  accepts  and  transmits  a  stream  of  bits  without  any  concern  with  meaning  or  structure.  
It   is   up   to   the   data   link   layer   to   create   and   recognize   frame   boundaries.   This   can   be   done   by   attaching   special   bit  
patterns  the  beginning  and  end  of  the  frame.  

A  noise  burst  on  the  line  can  destroy  a  frame  completely.  In  such  cases,  the  data  link  layer  software  on  the  source  
machine   can   retransmit   the   frame.   A   duplicate   frame   could   be   sent   if   the   acknowledgement   frame   from   the  
receiver   back   to   the   sender   were   lost.   It   is   up   to   this   layer   to   solve   the   problems   caused   by   damaged,   lost   and  
duplicate  frames.  Another  issue  that  arises  in  the  data  link  layer  is  how  to  keep  a  fast  transmitter  from  drawing  a  
slow   receiver   in   data.   Some   traffic   regulation   mechanism   must   be   employed   to   let   the   transmitter   known   how  
much  buffer  space  the  receiver  has  at  the  moment.  

Network  Layer:  Layer  3  


Network  layer  is  of  great  importance  when  it  comes  to  talk  about  communication  between  networking  devices.  
This  layer  helps  in  determining  the  data  being  transferred  between  different  network  devices.    
 
It   is   related   with   controlling   the   operation   of   the   subnet.   A   key   design   issue   is   to   determine   how   packets  
are   routed   from   source   to   destination.  If   too   many   packets   are   present   in   the   subnet   at   the   time,   they   will  
get  in  each  other’s   way  similar   to  bottlenecks.   It  is  the   duty  of  the  network   layer  to  control   such  congestion.  
 
Many  problems  arise  when  a  packets  travel  form  one  network  to  another  up  to  its  destination.   The  
addressing   used   by   the   second   network   may   differ   from   the   first   one;   the   packet  size  may   be   large  

  sarojpandey.com.np  -­‐  9  
              kcc.edu.np  
enough   for   the   second   network,   protocols   may   differ   and   so   on.   Hence   network   layer   has   to  
overcome   all  these  problems   to  allow  heterogeneous   networks   to   be   interconnected.  
 
Network  layer  also  translate  logical  address  into  actual  physical  address  for  example,  converting  computer  name  
in  to  MAC  address.  The  layer  is  held  responsible  for  controlling  the  core  processes  of  networking,  for  example,  
routing,  managing  network  and  its  problem,  addressing  etc.  Router/Switches  are  the  hardware  which  work  on  
network  layers,  if  sending  devices  are  unable  to  break  down  data  into  packets  similar  to  receiving  devices  then  
network  layer  comes  in  and  breakdown  the  data  into  smaller  unites  and  then  re-­‐assemble  the  data  at  receiving  
end.   It   handles   the   core   processes   of   networking,   network   layer   routes   data   in   form   of   packets   according   to  
network   address.   It   is   responsible   to   send   all   the   data   to   specific   destination   with   in   the   network.   Protocols   used  
are  RIP,  ARP,  and  ICMP  etc.  
 
Transport  Layer:  Layer  4  
Transport   layer   handles   the   delivery   of   data   packets   at   their   respective   ends   and   destinations.   It   is   also  
responsible   for   identifying   errors   and   duplication   of   data   through   out   with   in   the   network.   Once   data   is  
transferred  to  their  respective  ends  successfully  it  provides  acknowledgement  about  it  and  resends  the  data  on  
receiving   errors   during   transferring.   Transport   layer   keep   the   connection   alive   during   the   data   transmission   and  
also  handles  error  during  the  course  of  transferring  of  data  within  the  network.    
It’s   basic   function   is   to   accept   data   from   the   session   layer,   split   it   up   into   smaller   units   if   need   be,   pass  
these   to   the   network   layer   and   ensure   that   the   pieces   all   arrive   correctly  at  the  o t h e r  e n d .  Under  normal  
conditions,  the  transport  layer  creates  a  distinct   network   connection  for   each   transport   connection  required  
by   the   session   layer.   If   the   transport   connection   requires   a   high   throughput,   the   transport   layer   might  
create   multiple   network   connections,   dividing   the   data   among   the   network   connections   to   improve  
throughput.   But   if   it   sounds   expensive,   the   transport   layer   might   multiplex   several   transport   connections  
on   the   same   network   to   reduce   c o s t .   Overall,   t h e   tr a n s p o r t   l a y e r   i s   required   to   make   t h e  
m u ltip lex in g   transparent   to   the   session   layer.   The   transport   layer   also   determines   what   type   of   service   to   be  
provided   to   the  session  layer,  and   ultimately   the   users   of  the   network.   The   most   popular   type   of  transport  connections  
is  an   error  -­‐free  -­‐p oint-­‐t o-­‐point   channels   that   delivers   message   or   bytes   in   the  order   in   which   they   were  sent.  
 
Session  Layer:  Layer  5  
Session   layer   is   designed   to   manage   established   sessions   between   two   different   nodes   (users)   within  
the   network.   Session   layer   is   also   responsible   to   establish   connection   then   identifies   who   and   how  
much   data   is   to   be   sent   how   much   time   is   required   for   it.   Session   layers   keeps   the   connection   alive,  
incase  if  it  is  disconnection  during  the  transfer,  it  reconnects  and  continues  the  session.    
 
It   allows   users   on   different   machines   to   establish   sessions   between  them.   A   session   allows   ordinary  
data   transport   like   transport   layer   do,   but   also   provides   enhanced   services   useful   in   some  

  sarojpandey.com.np  -­‐  10  


              kcc.edu.np  
applications.   A   session   might   be   sued   to   allow   a   user   to   log   into   remote   timesharing   system   or   to  
transfer  a  file  between  two  machines.  
 
Presentation  Layer:  Layer  6  
Presentation   layers   works   as   interfacing   layer   between   different   formats.   It   present   data   into   similar   format   and  
update   the   difference   of   formats   between   two   different   systems.   It   also   provide   interface   between   application  
data  to  network  data  format.  It  is  used  for  protocol  conversions,  encryptions,  decryptions  and  data  compressions.  
Cryptography  is  done  with  in  presentation  layer.  
 
Application  Layer:  Layer  7  
This  is  upfront  layer  that  helps  in  interfacing  for  communication  and  data  transfer  with  in  the  network.  It  also  
provides  support  services  like  transfer  services,  handles  network  access,  e-­‐mailing,  error  management  and  user  
application  support.  
 
The   next   function   of   this   layer   is   file   transfer.   Different   file   systems   have   different   file   naming  
conventions,   different   ways   of   representing   text   lines   and   so   on.   Transferring   a   file   between   two   different  
systems  requires  handling  these  and  other  incompatibilities.  Thus  electronic  mail,  remote   job  entry,   directory  
look  up,  etc.  belongs   to  the  application   layer.  
 
 
The TCP/IP Reference Model
ARPANET   was   a   research   network   sponsored   by   US   Department   of   Defense.     It   eventually   connected  
hundreds  of  universities  and  government  installations  through  leased  telephone  lines.  When  satellite  and  
radio  networks  were  added  later,  the  existing  protocols  had  trouble  internetworking  with  them.  So  new  
reference   architecture   was   needed,   which   would   be   able   to   connect   multiple   networks   together   in   a  
seamless  way.  This  was  known  as  TCP/IP  Reference  Model.  

 
The  TCP/IP  reference  model.  

  sarojpandey.com.np  -­‐  11  


              kcc.edu.np  
1.     The   Internet   Layer:   The   Internet   Layer   holds   the   whole   architecture   together.   It   is   a   packet-­‐
switching  network   based   on  a  connectionless   inter   network   layer.   Its  function   is  to  permit   hosts   to  
inject   packets   into   the   network   and   let   them   to   travel   independently   to   their   destinations.   The  
Internet   Layer   d e f i n e s   a n   o ffic ia l   packet   format   and   protocol   called   Internet   Protocol   (IP).  
Thus   this   layer’s   function   is   to  deliver   IP   packets   to   their  proper  destination.  
 
2.     The   Transport  Layer:  The  Transport  Layer  allows  peer  entities  on  the  source  and   destination   hosts  
to   carry   on   a   conversation.  Here,   two   end-­‐t o-­‐end   protocols  have   been  defined.   The   first   one  TCP   is  
a   reliable   connection-­‐oriented   protocol   that   allows   a   byte   stream   originating   on   one   machine   to   be  
delivered   on   any   other   machine   in   the   Internet   without   error.   It   fragments   the   incoming   byte  
stream   into   discrete   messages   and   passes   each   one   on   to   the   Internet   Layer.   At   the   destination,  
the   receiving   TCP/IP   process   reassembles   the   received   messages   into   the   output   stream.  TCP/IP  also  
handles   the   flow   control.  The  second  is  UDP  (User  Datagram  Protocol),  an  unreliable  connectionless  
protocol   for   applications   that   do   not   want   TCP’S   sequencing   or   flow  control.   It   is   widely   used  for  
one-­‐shot,   client-­‐server   type   request-­‐reply   queries   and   applications   in   which   prompt   delivery   is  
important   than  accurate   delivery   like  transmitting   speech  or  voice.  
 
3.     The   Application  L ayer:     The   TCP/IP   model   does   not   have   session   or   presentation   layers.   The  
Application   layer   is   directly   on   top   of   transport   layer.   It   contains   all   the   higher-­‐level   protocols.   The  
early   one   included   v i r t u a l   te r m in a l   (Telnet),   f i l e   transfer   (FTP)   and   electronic   mail   (SMTIP).  
Telnet   protocol   allows   a   user   on   one   machine   to   log   into   a   distant   machine   and   work   there.   FTP  
provides   a   way   to   move   data   efficiently   from   one   machine   to   another.   Email   was   originally   just   a  
kind   of   file   transfer,  but   later,   a  specialized   protocol  was   developed  for   it.  There   some   other   new  
protocols   like   Domain   Name   Service   for   mapping   host   names   to   their   network   address   or   vice-­‐
versa,   NNTP   for  moving  news   articles   around   and  HTTP   for  fetching   pages   on  the  World   Wide  Web,  
etc.  
 
4.     The   Host-­‐to-­‐Network   Layer:   The   TCP/IP   reference   model   does   not   really   say   much   about   what  
happen   below   the   Internet   Layer,   except   to   port   out   that   the   host   has   to   connect   to   the   network  
using   same   protocol   so   it   can   send   IP   packets   over   it.   This   protocol   is   not   defined   and   varies   from  
host  to  host  and  network   to  network.  

A  Comparison  of  the  OSI  and  TCP  Reference  Models  

[ASSIGNMENT  01]  

  sarojpandey.com.np  -­‐  12  


              kcc.edu.np  
TCP vs UDP

There are two types of Internet Protocol (IP) traffic. They are TCP or Transmission Control Protocol and UDP or
User Datagram Protocol. TCP is connection oriented – once a connection is established, data can be sent
bidirectional. UDP is a simpler, connectionless Internet protocol. Multiple messages are sent as packets in chunks
using UDP.

Comparison chart
  TCP   UDP  

Acronym  for:   Transmission  Control  Protocol   User  Datagram  Protocol  or  Universal  
Datagram  Protocol  

Function:   As  a  message  makes  its  way  across   UDP  is  also  a  protocol  used  in  message  
the  internet  from  one  computer  to   transport  or  transfer.  This  is  not  
another.  This  is  connection  based.   connection  based  which  means  that  one  
program  can  send  a  load  of  packets  to  
another  and  that  would  be  the  end  of  the  
relationship.  

Usage:   TCP  is  used  in  case  of  non-­‐time   UDP  is  used  for  games  or  applications  that  

critical  applications.   require  fast  transmission  of  data.  UDP's  


stateless  nature  is  also  useful  for  servers  that  
answer  small  queries  from  huge  numbers  of  
clients.  

Examples:   HTTP,  HTTPs,  FTP,  SMTP  Telnet  etc.   DNS,  DHCP,  TFTP,  SNMP,  RIP,  VOIP  etc.  

Ordering  of  data   TCP  rearranges  data  packets  in  the   UDP  has  no  inherent  order  as  all  packets  are  

packets:   order  specified.   independent  of  each  other.  If  ordering  is  
required,  it  has  to  be  managed  by  the  
application  layer.  

Speed  of  transfer:   The  speed  for  TCP  is  slower  than  UDP.   UDP  is  faster  because  there  is  no  error-­‐
checking  for  packets.  

Reliability:   There  is  absolute  guarantee  that  the   There  is  no  guarantee  that  the  messages  
data  transferred  remains  intact  and   or  packets  sent  would  reach  at  all.  
arrives  in  the  same  order  in  which  it  
was  sent.  

  sarojpandey.com.np  -­‐  13  


              kcc.edu.np  

  TCP   UDP  

Header  Size:   TCP  header  size  is  20  bytes   UDP  Header  size  is  8  bytes.  

Common  Header   Source  port,  Destination  port,  Check   Source  port,  Destination  port,  Check  Sum  
Fields:   Sum  

Streaming  of  data:   Data  is  read  as  a  byte  stream,  no   Packets  are  sent  individually  and  are  
distinguishing  indications  are   checked  for  integrity  only  if  they  arrive.  
transmitted  to  signal  message   Packets  have  definite  boundaries,  which  
(segment)  boundaries.   are  honored  upon  receipt,  meaning  a  
read  operation  at  the  receiver  socket  will  
yield  an  entire  message  as  it  was  
originally  sent.  

Weight:   TCP  requires  three  packets  to  set  up   UDP  is  lightweight.  There  is  no  ordering  
a  socket  connection,  before  any  user   of  messages,  no  tracking  connections,  etc.  
data  can  be  sent.  TCP  handles   It  is  a  small  transport  layer  designed  on  
reliability  and  congestion  control.   top  of  IP.  

Data  Flow  Control:   TCP  does  Flow  Control.  TCP   UDP  does  not  have  an  option  for  flow  
requires  three  packets  to  set  up  a   control  
socket  connection,  before  any  user  
data  can  be  sent.  TCP  handles  
reliability  and  congestion  control.  

Error  Checking:   TCP  does  error  checking   UDP  does  error  checking,  but  no  recovery  
options.  

Fields:   1.  Sequence  Number,  2.  AcK   1.  Length,  2.  Source  port,  3.  Destination  
number,  3.  Data  offset,  4.  Reserved,   port,  4.  Check  Sum  
5.  Control  bit,  6.  Window,  7.  Urgent  
Pointer  8.  Options,  9.  Padding,  10.  
Check  Sum,  11.  Source  port,  12.  
Destination  port  

Acknowledgement:   Acknowledgement  segments   No  Acknowledgment  

   
  sarojpandey.com.np  -­‐  14  
              kcc.edu.np  
Differences  in  Data  Transfer  Features  
TCP  ensures  a   reliable   and   ordered   delivery   of   a   stream   of   bytes   from   user   to  server  or   vice  
versa.  UDP  is  not  dedicated  to  end  to  end  connections  and  communication  does  not  check  readiness  of  
receiver.  
Reliability  
TCP  is  more  reliable  since  it  manages  message  acknowledgment  and  retransmissions  in  case  of  
lost   parts.   Thus   there   is   absolutely   no   missing   data.  UDP  does   not   ensure   that   communication  
has   reached   receiver   since   concepts   of   acknowledgment,   time   out   and   retransmission   are   not  
present.  

Ordering  
TCP  transmissions   are   sent   in   a   sequence   and   they   are   received   in   the   same   sequence.   In   the  
event   of   data   segments   arriving   in   wrong   order,   TCP   reorders   and   delivers   application.   In   the  
case   of  UDP,   sent   message   sequence   may   not   be   maintained   when   it   reaches   receiving  
application.   There   is   absolutely   no   way   of   predicting   the   order   in   which   message   will   be  
received.  

Connection  
TCP  is  a  heavy  weight  connection  requiring  three  packets  for  a  socket  connection  and  handles  
congestion   control   and   reliability.  UDP  is   a   lightweight   transport   layer   designed   atop   an   IP.  
There  are  no  tracking  connections  or  ordering  of  messages.  

Method  of  transfer  


TCP  reads   data   as   a   byte   stream   and   message   is   transmitted   to   segment  
boundaries.  UDP  messages   are   packets   which   are   sent   individually   and   on   arrival   are   checked  
for  their  integrity.  Packets  have  defined  boundaries  while  data  stream  has  none.  

Differences  in  how  TCP  and  UDP  work  


A   TCP   connection   is   established   via   a  three   way   handshake,   which   is   a   process   of   initiating   and  
acknowledging   a   connection.   Once   the   connection   is   established   data   transfer   can   begin.   After  
transmission,  the  connection  is  terminated  by  closing  of  all  established  virtual  circuits.  
 
UDP   uses   a   simple   transmission   model   without   implicit   hand-­‐shaking   dialogues   for   guaranteeing  
reliability,   ordering,   or   data   integrity.   Thus,   UDP   provides   an   unreliable   service   and   datagrams   may  
arrive  out  of  order,  appear  duplicated,  or  go  missing  without  notice.  UDP  assumes  that  error  checking  
and  correction  is  either  not  necessary  or  performed  in  the  application,  avoiding  the  overhead  of  such  

  sarojpandey.com.np  -­‐  15  


              kcc.edu.np  
processing   at   the   network   interface   level.   Unlike   TCP,   UDP   is   compatible   with   packet   broadcasts  
(sending  to  all  on  local  network)  and  multicasting  (send  to  all  subscribers).  

Different  Applications  of  TCP  and  UDP  


Web  browsing,  email  and  file  transfer  are  common  applications  that  make  use  of  TCP.  TCP  is  used  to  
control   segment   size,   rate   of   data   exchange,   flow   control   and   network   congestion.   TCP   is   preferred  
where   error   correction   facilities   are   required   at   network   interface   level.   UDP   is   largely   used   by   time  
sensitive  applications  as  well  as  by  servers  that  answer  small  queries  from  huge  number  of  clients.  UDP  
is   compatible   with   packet   broadcast   -­‐   sending   to   all   on   a   network   and   multicasting   –   sending   to   all  
subscribers.   UDP   is   commonly   used   in   Domain   Name   System,   Voice   over   IP,   Trivial   File   Transfer  
Protocol  and  online  games.  

 
Unix  Standards  (POSIX,  Open  Group,  IETF)  
POSIX  
POSIX,  an  acronym  for  "Portable  Operating  System  Interface",  is  a  family  of  standards  specified  by  the  
IEEE   for   maintaining   compatibility   between   operating   systems.   POSIX   defines   the   application  
programming   interface   (API),   along   with   command   line   shells   and   utility   interfaces,   for   software  
compatibility  with  variants  of  Unix  and  other  operating  systems.  
 
POSIX   defines   a   standard   operating   system   interface   and   environment,   including   a   command  
interpreter  (or  “shell”),  and  common  utility  programs  to  support  applications  portability  at  the  source  
code   level.   It   is   intended   to   be   used   by   both   application   developers   and   system   implementers   and  
comprises  four  major  components  (each  in  an  associated  volume):  
 

• General  terms,  concepts,  and  interfaces  common  to  all  volumes  of  this  standard,  including  utility  
conventions  and  C-­‐language  header  definitions,  are  included  in  the  Base  Definitions  volume.  
• Definitions  for  system  service  functions  and  subroutines,  language-­‐specific  system  services  for  
the  C  programming  language,  function  issues,  including  portability,  error  handling,  and  error  
recovery,  are  included  in  the  System  Interfaces  volume.  
• Definitions  for  a  standard  source  code-­‐level  interface  to  command  interpretation  services  (a  
“shell”)  and  common  utility  programs  for  application  programs  are  included  in  the  Shell  and  
Utilities  volume.  

  sarojpandey.com.np  -­‐  16  


              kcc.edu.np  
• Extended  rationale  that  did  not  fit  well  into  the  rest  of  the  document  structure,  which  contains  
historical  information  concerning  the  contents  of  POSIX.1-­‐2008  and  why  features  were  included  
or  discarded  by  the  standard  developers,  is  included  in  the  Rationale  (Informative)  volume.  
 
The  following  areas  are  outside  the  scope  of  POSIX:  
• Graphics  interfaces  
• Database  management  system  interfaces  
• Record  I/O  considerations  
• Object  or  binary  code  portability  
• System  configuration  and  resource  availability  
 
POSIX   describes   the   external   characteristics   and   facilities   that   are   of   importance   to   application  
developers,   rather   than   the   internal   construction   techniques   employed   to   achieve   these   capabilities.  
Special   emphasis   is   placed   on   those   functions   and   facilities   that   are   needed   in   a   wide   variety   of  
commercial  applications.  

The   POSIX   specifications   for  Unix-­‐like  operating   system  environments   originally   consisted   of   a   single  
document   for   the   core-­‐programming   interface,   but   eventually   grew   to   19   separate   documents   (E.g.  
POSIX.1,   POSIX.2   etc.).   The   standardized   user  command   line  and  scripting   interface  were   based   on  
the  Korn   shell.   Many   user-­‐level   programs,   services,   and   utilities   including  awk,  echo,  ed  were   also  
standardized,   along   with   required   program-­‐level   services   including   basic  I/O  (file,  terminal,  
and  network)   services.   POSIX   also   defines   a   standard  threading  library   API   which   is   supported   by   most  
modern  operating  systems.  

As  of  2009,  POSIX  documentation  is  divided  in  two  parts:  

• POSIX.1-­‐2008:   POSIX   Base   Definitions,   System   Interfaces,   and   Commands   and   Utilities   (which  
include   POSIX.1,   extensions   for   POSIX.1,   Real-­‐time   Services,   Threads   Interface,   Real-­‐time  
Extensions,   Security   Interface,   Network   File   Access   and   Network   Process-­‐to-­‐Process  
Communications,   User   Portability   Extensions,   Corrections   and   Extensions,   Protection   and   Control  
Utilities  and  Batch  System  Utilities)  
• POSIX   Conformance   Testing:   A   test   suite   for   POSIX   accompanies   the   standard:  PCTS  or   the  POSIX  
Conformance  Test  Suite.  

[The  development  of  the  POSIX  standard  takes  place  in  the  Austin  Group,  a  joint  working  group  linking  the  Open  Group  and  the  
ISO  organization.]  

  sarojpandey.com.np  -­‐  17  


              kcc.edu.np  

POSIX.1  

• POSIX.1,  Core  Services  (incorporates  Standard  ANSI  C)  (IEEE  Std.  1003.1-­‐1988)  
• Process  Creation  and  Control  
• Signals  
• Floating  Point  Exceptions  
• Segmentation  /  Memory  Violations  
• Illegal  Instructions  
• Bus  Errors  
• Timers  
• File  and  Directory  Operations  
• Pipes  
• C  Library  (Standard  C)  
• I/O  Port  Interface  and  Control  
 
POSIX.1b  

• POSIX.1b,  Real-­‐time  extensions  (IEEE  Std  1003.1b-­‐1993)  


• Priority  Scheduling  
• Real-­‐Time  Signals  
• Clocks  and  Timers  
• Semaphores  
• Message  Passing  
• Shared  Memory  
• Asynch  and  Synch  I/O  
Memory  Locking  Interface  

 
POSIX.1c  

• POSIX.1c,  Threads  extensions  (IEEE  Std  1003.1c-­‐1995)  


• Thread  Creation,  Control,  and  Cleanup  
• Thread  Scheduling  
• Thread  Synchronization  
• Signal  Handling  
 
POSIX.2  

• POSIX.2,  Shell  and  Utilities  (IEEE  Std  1003.2-­‐1992)  


• Command  Interpreter  
• Utility  Programs  

  sarojpandey.com.np  -­‐  18  


              kcc.edu.np  
After  1997,  the  Austin  Group  developed  the  POSIX  revisions.  The  specifications  are  known  under  the  name  Single  UNIX  
Specification,  before  they  become  a  POSIX  standard  when  formally  approved  by  the  ISO.  

POSIX.1-­‐2001  

POSIX.1-­‐2001  or  IEEE  Std  1003.1-­‐2001  equates  to  the  Single  UNIX  Specification  version  3  

This  standard  consisted  of:  

• the  Base  Definitions,  Issue  6,  


• the  System  Interfaces  and  Headers,  Issue  6,  
• the  Commands  and  Utilities,  Issue  6.  

POSIX.1-­‐2001  

IEEE   Std   1003.1-­‐2004   involved   a   minor   update   of   POSIX.1-­‐2001.   It   incorporated   two  


technical  corrigenda.  Its  contents  are  available  on  the  web.  

POSIX.1-­‐2008  

As  of  2009  POSIX.1-­‐2008  or  IEEE  Std  1003.1-­‐2008  represents  the  current  version.  A  free  online  copy  is  
available.  

This  standard  consists  of:  

• the  Base  Definitions,  Issue  7,  


• the  System  Interfaces  and  Headers,  Issue  7,  
• the  Commands  and  Utilities,  Issue  7.  
 
OPEN  GROUP  
The  Open  Group  is  a  global  consortium  that  enables  the  achievement  of  business  objectives  through  IT  standards.  
With  more  than  400  member  organizations,  they  have  a  diverse  membership  that  spans  all  sectors  of  the  IT  
community  —  customers,  systems  and  solutions  suppliers,  tool  vendors,  integrators  and  consultants,  as  well  as  
academics  and  researchers  to:  
• Capture,  understand  and  address  current  and  emerging  requirements,  and  establish  policies  and  share  best  
practices  
• Facilitate  interoperability,  develop  consensus,  and  evolve  and  integrate  specifications  and  open  source  
technologies  
• Offer  a  comprehensive  set  of  services  to  enhance  the  operational  efficiency  of  consortia  
• Operate  the  industry’s  premier  certification  service  
The   Open   Group   is   a   vendor   and   technology-­‐neutral   industry   consortium,   currently   with   over   four  
hundred  member  organizations.  It  was  formed  in  1996  when  X/Open  merged  with  the  Open  Software  
  sarojpandey.com.np  -­‐  19  
              kcc.edu.np  
Foundation.   Services   provided   include   strategy,   management,   innovation   and   research,   standards,  
certification,  and  test  development.  The  Open  Group  is  most  famous  as  the  certifying  body  for  the  UNIX  
trademark   and   its   publication   of   the   Single   UNIX   Specification   technical   standard,   which   extends   the  
POSIX  standards  and  is  the  official  definition  of  a  UNIX  system.    
 
The  Open  Group  also  develops  and  manages  the  Open  Group  Architecture  Framework  standard,  which  
is  an  industry  standard  enterprise  architecture  framework.  The  Open  Group  members  include  a  range  
of  IT  buyers  and  vendors  as  well  as  government  agencies,  for  example,  Fujitsu,  Oracle,  Hitachi,  HP,  IBM,  
,  NEC,  US  Department  of  Defense,  NASA  and  others.  
 
• The  Open  Group  published  x/open  portability  guide  in  1989.  
• Next  issue  was  published  in  1992  followed  by  version  2  in  1994.  
• In  1997,  version  2  of  the  single  Unix  specification  was  announced.  Products  conforming  to  this  
specification  are  called  UNIX  98.  The  number  of  interfaces  required  by  UNIX  98  increases  from  
1170   to   1434,   although   for   a   workstation,   this   jumps   to   3030   because   it   includes   the   CDF  
(Common  Desktop  Environment).  
 
IETF  
IETF  is  short  for  Internet  Engineering  Task  Force,  the  main  standards  organization  for  the  Internet.  The  
IETF   is   a   large   open   international   community   of   network   designers,   operators,   vendors,   and  
researchers  concerned  with  the  evolution  of  the  Internet  architecture  and  the  smooth  operation  of  the  
Internet.  It  is  open  to  any  interested  individual.  
 

The  Internet  Engineering  Task  Force  (IETF)  develops  and  promotes  Internet  standards,  cooperating  
closely   with   the  W3C  and  ISO/IEC   standards   bodies   and   dealing   in   particular   with   standards   of  
the  Internet  protocol  suite  (TCP/IP).  It  is  an  open  standards  organization,  with  no  formal  membership  
or  membership  requirements.  

All  participants  and  managers  are  volunteers,  though  their  employers  or  sponsors  usually  fund  their  work;  for  instance,  the  
current  chairperson  is  funded  by  VeriSign  and  the  U.S.  government's  National  Security  Agency.  

 
Network  Utilities  (telnet,  netstat,  ifconfig,  ping,  ftp)  
• telnet  
A   terminal   emulation   program   for   TCP/IP   networks   such   as   the   Internet.   The   Telnet   program  
runs   on   your   computer   and   connects   your   PC   to   a   server   on   the   network.   You   can   then   enter  

  sarojpandey.com.np  -­‐  20  


              kcc.edu.np  
commands  through  the  Telnet  program  and  they  will  be  executed  as  if  you  were  entering  them  
directly   on   the   server   console.   This   enables   you   to   control   the   server   and   communicate   with  
other  servers  on  the  network.  To  start  a  Telnet  session,  you  must  log  in  to  a  server  by  entering  a  
valid  username  and  password.  Telnet  is  a  common  way  to  remotely  control  Web  servers.  

telnet  [-­‐8]  [-­‐E]  [-­‐L]  [-­‐c]  [-­‐d]  [-­‐r]  [  -­‐e  escape_char  ]  [  -­‐l  user  ]  [-­‐n  file  ]  [  host  [  port  ]  ]  

-­‐8   Specifies  an  8-­‐bit  data  path.  Negotiating  the  TELNET  BINARY  option  is  attempted  for  both  input  
and  output.  

-­‐E   Stops  any  character  from  being  recognized  as  an  escape  character.  

-­‐L   Specifies  an  8-­‐bit  data  path  on  output.  This  causes  the  BINARY  option  to  be  negotiated  on  output.  

-­‐c   Disables  the  reading  of  the  user's  telnetrc  file.  

-­‐d   Sets  the  initial  value  of  the  debug  toggle  to  TRUE.  

-­‐r   Specifies   a   user   interface   similar   to   rlogin.   In   this   mode,   the   escape   character   is   set   to   the  
tilde   (~)   character,   unless   modified   by   the   -­‐e   option.   The   rlogin   escape   character   is   only  
recognized   when   it   is   preceded   by   a   carriage   return.   In   this   mode,   the   telnet   escape  
character,   normally   '^]',   must   still   precede   a   telnet   command.   The   rlogin   escape   character  
can   also   be   followed   by   '.\r'   or   '^Z',   and,   like  rlogin,   closes   or   suspends   the   connection,  
respectively.  This  option  is  an  uncommitted  inter-­‐  face  and  may  change  in  the  future.  

-­‐e  escape_char   Sets  the  initial  escape  character  to  escape_char.  escape_char  may  also  be  a  two  character  sequence  
consisting  of  '^'  followed  by  one  character.  If  the  second  character  is  '?',  the  DEL  character  is  
selected.  Otherwise,  the  second  character  is  converted  to  a  control  character  and  used  as  the  
escape  character.  If  the  escape  character  is  the  null  string  (that  is,  -­‐e  ''),  it  is  disabled.  

-­‐l  user   When  connecting  to  a  remote  system  that  understands  the  ENVIRON  option,  then  user  will  be  sent  
to  the  remote  system  as  the  value  for  the  ENVIRON  variable  USER.  

-­‐n  file   Opens  trace  file  for  recording  trace  information.  

 
• netstat  
Print  network  connections,  routing  tables,  interface  statistics,  masquerade  connections,  and  
multicast  memberships.  
netstat  [-­‐a]  [-­‐b]  [-­‐e]  [-­‐f]  [-­‐n]  [-­‐o]  [-­‐p  protocol]  [-­‐r]  [-­‐s]  [-­‐t]  [-­‐x]  [-­‐y]  [time_interval]  [/?]  

  sarojpandey.com.np  -­‐  21  


              kcc.edu.np  
-­‐a  =  This  switch  displays  active  TCP  connections,  TCP  connections  with  the  listening  state,  as  well  
as  UDP  ports  that  are  being  listened  to.  

-­‐b  =  This  netstat  switch  is  very  similar  to  the  -­‐o  switch  listed  below,  but  instead  of  displaying  the  
PID,  will  display  the  process's  actual  file  name.  Using  -­‐b  over  -­‐o  might  seem  like  it's  saving  you  a  
step  or  two  but  using  it  can  sometimes  greatly  extend  the  time  it  takes  netstat  to  fully  execute.  

-­‐e  =   Use   this   switch   with   the   netstat   command   to   show   statistics   about   your   network   connection.  
This   data   includes   bytes,   unicast   packets,   non-­‐unicast   packets,   discards,   errors,   and   unknown  
protocols  received  and  sent  since  the  connection  was  established.  

-­‐f  =   The  -­‐f  switch   will   force   the   netstat   command   to   display   the  Fully   Qualified   Domain  
Name(FQDN)  for  each  foreign  IP  addresses  when  possible.  
 
-­‐n  =  Use  the  -­‐n  switch  to  prevent  netstat  from  attempting  to  determine  host  names  for  foreign  IP  
addresses.   Depending   on   your   current   network   connections,   using   this   switch   could   considerably  
reduce  the  time  it  takes  for  netstat  to  fully  execute.  
…  …  …  
• ifconfig  
ifconfig   (short   for   interface   configuration)   is   a   system   administration   utility   in   Unix-­‐like  
operating   systems   to   configure,   control,   and   query   TCP/IP   network   interface   parameters   from   a  
command  line  interface  (CLI)  or  in  system  configuration  scripts.  
ifconfig  [interface]  
ifconfig  interface  [aftype]  options  |  address  ...  
 
Options  
interface  
The  name  of  the  interface.  This  is  usually  a  driver  name  followed  by  a  unit  number,  for  
example  eth0  for  the  first  Ethernet  interface.  
up  
This  flag  causes  the  interface  to  be  activated.  It  is  implicitly  specified  if  an  address  is  assigned  to  
the  interface.  
down  
This  flag  causes  the  driver  for  this  interface  to  be  shut  down.  
…  …  …    
E.g.  
ifconfig  eth0  :  View  the  network  settings  on  the  first  Ethernet  adapter  installed  in  the  computer.  

  sarojpandey.com.np  -­‐  22  


              kcc.edu.np  
ifconfig  –a  :  Display  info  on  all  network  interfaces  on  server,  active  or  inactive.  
ifconfig  eth0  down  :  If  eth0  exists  would  take  it  down  causing  it  cannot  send  or  receive  any  
information.  
ifconfig  eth0  up  :  If  eth0  exists  and  in  the  down  state  would  return  it  back  to  the  up  state  allowing  
to  to  send  and  receive  information.  
 
ifconfig  eth0  192.168.1.102  netmask  255.255.255.0  broadcast  192.168.1.255  :  Assign  eth0  with  the  
above  values  for  IP,  netmask  and  broadcast  address.  
 

• ping  
Short   for  Packet   InterNet   Groper,  ping  is   a   utility   used   to   verify   if   a   network  data  packet  is  
capable  of  being  distributed  to  an  address  without  errors.  The  ping  utility  is  commonly  used  to  
check  for  network  errors.  It  sends  ICMP  ECHO_REQUEST  packets  to  network  hosts.  

Syntax  

ping  -­‐s  [-­‐d]  [-­‐l]  [-­‐L]  [-­‐n]  [-­‐r]  [-­‐R]  [-­‐v]  [  -­‐i  interface_address  ]  [-­‐I  interval]  [-­‐t  ttl]  host  [packetsize]  [count]  

-­‐d   Set  the  SO_DEBUG  socket  option.  

-­‐l   Loose  source  route.  Use  this  option  in  the  IP  header  to  send  the  
packet  to  the  given  host  and  back  again.  Usually  specified  with  the  -­‐R  
option.  

-­‐L   Turn  off  loopback  of  multicast  packets.  Normally,  if  there  are  
members  in  the  host  group  on  the  out-­‐  going  interface,  a  copy  of  the  
multicast  packets  will  be  delivered  to  the  local  machine.  

-­‐n   Show  network  addresses  as  numbers.  Ping  normally  displays  


addresses  as  host  names.  

-­‐r   Bypass   the   normal   routing   tables   and   send   directly   to   a   host   on   an  
attached   network.   If   the   host   is   not   on   a   directly   attached   network,  
an   error   is   returned.   This   option   can   be   used   to   ping   a   local   host  
through  an  interface  that  has  been  dropped  by  the  router  daemon.  

-­‐R   Record  route.  Sets  the  IP  record  route  option,  which  will  store  the  
route  of  the  packet  inside  the  IP  header.  

-­‐v   Verbose  output.  List  any  ICMP  packets,  other  than  ECHO_RESPONSE,  
that  are  received.  

-­‐i  interface_address   Specify  the  outgoing  interface  address  to  use  for  multicast  packets.  
The  default  interface  address  for  multicast  packets  is  determined  

  sarojpandey.com.np  -­‐  23  


              kcc.edu.np  

from  the  routing  tables.  

-­‐I  interval   Specify  the  interval  between  successive  transmissions.  The  default  is  
one  second.  

-­‐t  ttl   Specify   the   IP   time   to   live   for   unicast   and   multicast   packets.   The  
default  time  to  live  for  multicast  is  one  hop.  

host   The  network  host.  

packetsize   Specified  size  of  packet.  Default  is  64.  

count   Amount  of  times  to  send  the  ping  request.  

Examples  

ping  google.com  
 
• ftp  &  its  commands  
[SELF  STUDY  01.  Ref:  Web  Technology]  
 

Introduction  to  Network  Programming  


Computer   network   programming   involves   writing   computer   programs   that   enable   processes   to  
communicate  with  each  other  across  a  computer  network.  
 
Network  programming  is  client–server  programming    
Inter   process   communication,   even   if   it   is   bidirectional,   cannot   be   implemented   in   a   perfectly  
symmetric  way:  to  establish  a  communication  channel  between  two  processes,  one  process  must  take  
the  initiative,  while  the  other  is  waiting  for  it.  Therefore,  network  programming  unavoidably  assumes  a  
client–server  model:  The  process  initiating  the  communication  is  a  client,  and  the  process  waiting  for  
the   communication   to   be   initiated   is   a   server.   The   client   and   server   processes   together   form   a  
distributed  system.  In  a  peer-­‐to-­‐peer  communication,  the  program  can  act  both  as  a  client  and  a  server.  
 
Network  programming  is  socket  programming  
The   endpoint   in   an   inter   process   communication   is   called   a   socket,   or   a   network   socket   for  
disambiguation.   Since   most   communication   between   computers   is   based   on   the   Internet   Protocol,   an  
almost  equivalent  term  is  Internet  socket.  The  data  transmission  between  two  sockets  is  organized  by  
communications   protocols,   usually   implemented   in   the   operating   system   of   the   participating  
computers.   Application   programs   write   to   and   read   from   these   sockets.   Therefore,   network  
programming  is  essentially  socket  programming.  
  sarojpandey.com.np  -­‐  24  
              kcc.edu.np  
We  write  network  programs  using  APIs.  There  are  2  APIs  for  that.  
• SOCKETS  
• XTI  (X/Open  Transport  Interface)  developed  by  AT  &  T  
 
Programming  Standards  
[SELF  STUDY  02.]  
 
Wrapper  functions  
A   wrapper   function   is   a   function   in   a   computer   program   whose   main   purpose   is   to   call   a   second  
function   with   little   or   no   additional   computation.   This   is   also   known   as   method   delegation.   Wrapper  
functions  can  be  used  for  a  number  of  purposes.  
• Each  and  every  system  call  has  potentiality  to  fail.  
• System  calls  may  fail  due  to  various  reasons  like  resource  limitation,  wrong  parameters  etc.  
• Real  Program  to  be  implemented  can’t  ignore  these  errors.  
• Handling   error   in   the   main   routine   itself   involves   many   nested   if-­‐else   ladders   or   switch  
statements,  making  the  flow  of  the  program  difficult  to  understand.  
• So,   we   define   the   new   function   that   makes   the   system   call,   handles   error   (if   any)   and   returns  
back  the  output  of  the  system  call  (if  success).  This  is  called  wrapper  function.  
• Thus,   by   using   wrapper   function,   we   get   clean   and   understandable   program   code   and   errors   are  
also  handled  properly.  
• To  distinguish  it  from  system  calls,  a  wrapper  function  name  begins  with  a  capital  letter;  while  
system  call  has  a  name  with  all  small  letter.  
• By,   convention,   a   wrapper   function   and   the   system   call   that   it   invokes   have   the   same   name  
except  for  the  initial  letter.  
• Also,  they  have  same  type,  sequence  and  number  of  parameters  as  well  as  same  return  type.  
 
Example  
int main()
{
char *buff[100];
int fd, n;

Mkfifo(“/usr/bin/myinfo.fifo”, S_IRDWR);
fd=open(“/usr/bin/myfifo.fifo”,O_RDONLY);
n=Read(fd, buff, 100);
buff[n]=’\0’;
  sarojpandey.com.np  -­‐  25  
              kcc.edu.np  
printf(“%s”,buff);
return 0;
}
int Mkinfo (const char *pname, mode_t mode)
{
int err;
err=mkfifo(pname,mode);
if(err==-1)
{
switch(errno)
{
case E_ACCESS: printf(“Access Violation.”);
exit(0);

case E_EXIST: printf(“Already Exists.”)


exit(0);
.
.
.
default: perror("Other ERROR”);
exit(0);
}
return 0;
}  
 
Header  files  
A   header   file   is   a   file   that   allows   programmers   to   separate   certain   elements   of   a   program's   source   code  
into   reusable   files.   Header   files   commonly   contain   forward   declarations   of   classes,   subroutines,  
variables,   and   other   identifiers.   Programmers   who   wish   to   declare   standardized   identifiers   in   more  
than   one   source   file   can   place   such   identifiers   in   a   single   header   file,   which   other   code   can   then   include  
whenever  the  header  contents  are  required.  This  is  to  keep  the  interface  in  the  header  separate  from  
the  implementation.  

Header  files  serve  two  purposes:  

• System   header   files   declare   the   interfaces   to   parts   of   the   operating   system.   You   include   them   in   your  
program  to  supply  the  definitions  and  declarations  you  need  to  invoke  system  calls  and  libraries.  

  sarojpandey.com.np  -­‐  26  


              kcc.edu.np  
• Your   own   header   files   contain   declarations   for   interfaces   between   the   source   files   of   your   program.   Each  
time  you  have  a  group  of  related  declarations  and  macro  definitions  all  or  most  of  which  are  needed  in  
several  different  source  files,  it  is  a  good  idea  to  create  a  header  file  for  them.  

Some  Header  Files  Used  in  Network  Programming  

o <sys/types.h>  
o <unistd.h>  
o  <sys/uio.h>  
o  <errno.h>  
o  <sys/ioctl.h>  
o  <fcntl.h>  
o  <sys/socket.h>  
o  <netdb.h>  
o  <netinet/in_systm.h>  
o  <netinet/ip_icmp.h>  
o  <netinet/udp.h>  
o  <netinet/ip.h>  
o  <netinet/in.h>  
o  <arpa/inet.h>  
o  <arpa/nameser.h>  
o  <resolv.h>  
o  <net/if.h>  
o  <strings.h>  

Libraries  and  Program  Documentation  


[ASSIGNMENT  02  +  SELF  STUDY  03]  
 

  sarojpandey.com.np  -­‐  27  

You might also like