Network Programming Lesson 1: Huynh Cong Phap, PHD
Network Programming Lesson 1: Huynh Cong Phap, PHD
hcphap@gmail.com, hcphap@dut.udn.vn
Components
Clients Servers Communication Networks
Server
Client
Clients
Applications that run on computers Rely on servers for
Files Devices Processing power
Clients are Applications
Servers
Computers or processes that manage network resources
Disk drives (file servers) Servers Manage Resources Printers (print servers) Network traffic (network servers)
Communication Networks
Networks Connect Clients and Servers
ClientServer Computing
Process takes place
on the server and on the client Client-Server Computing Optimizes Computing Resources
Servers
Clients
Make requests Format data on the desktop
Application Functions
Software application functions are separated into three distinct parts
Application Components
3 Data Management
2 Client Types
Fat Client
2
1
Application Logic
Presentation
3 Logical Tiers
Thin Client
Database Applications:
Most common use of client-server architectures
Middleware
Software that connects two otherwise separate applications Example: Middleware product linking a database system to a Web server Database Server: Manages Data
Types of Servers
From A to Z Application Servers Audio/Video Servers Chat Servers Fax Servers FTP Servers Groupware Servers IRC Servers List Servers Mail Servers News Servers Proxy Servers Telnet Servers Web Servers Z39.50 Servers
Source: http://webopedia.lycos.com
ADVANTAGES OF CLIENTSERVER
Advantages often cited include:
Centralization - access, resources, and data security are controlled through the server Scalability - any element can be upgraded when needed Flexibility - new technology can be easily integrated into the system Interoperability - all components (clients, network, servers) work together
DISADVANTAGES OF CLIENT-SERVER
Disadvantages often cited include:
Dependability - when the server goes down, operations cease Higher than anticipated costs Can cause network congestion
CLIENT-SERVER ARCHITECTURES
There are basically two types of clientserver architectures
Two tier architectures Three tier architectures
The choice between the two should be made based on combination of:
Schedule for project implementation Expected system changes and enhancements
TWO-TIER ARCHITECTURES
Application components are distributed between the server and client software In addition to part of the application software, the server also stores the data, and all data accesses are through the server. The presentation (to the user) is handled strictly by the client software.
Server
Network
PC
PC
PC
Clients
THREE-TIER ARCHITECTURES
Server
Server
Network
PC
PC
PC
Clients
3-tier architectures attempt to overcome some of the limitations of the 2-tier architecture by separating presentation, processing, and data into 3 separate and distinct entities. The software in the client handles the presentation (to the user) using similar tools as in the 2-tier architecture.
What is Peer-to-Peer?
A model of communication where every node in the network acts alike. As opposed to the Client-Server model, where one node provides services and other nodes use the services.
Scalability
Since every peer is alike, it is possible to add more peers to the system and scale to larger networks.
Programmability
As a corollary of decentralized coordination.
Types of P2P
Pure P2P Hybrid P2P
Napster
Gnutella
KaZaA/Morpheus
Distributed systems
Independent machines work cooperatively without shared memory
They have to talk somehow
Modes of connection
Circuit-switched
dedicated path guaranteed (fixed) bandwidth [almost] constant latency
Packet-switched
shared connection data is broken into chunks called packets each packet contains destination address available bandwidth channel capacity variable latency
For computers:
electrical encoding of data where is the start of the packet? which bits contain the length? is there a checksum? where is it? how is it computed? what is the format of an address? byte ordering
Protocols
Protocols
Exist at different levels
understand format of address and how to compute checksum humans vs. whales
different wavelengths
Layering
To ease software development and maximize flexibility:
Network protocols are generally organized in layers Replace one layer without replacing surrounding layers Higher-level software does not have to know how to format an Ethernet packet or even know that Ethernet is being used
Layering
Most popular model of guiding (not specifying) protocol layers is
2
1
2
1
4
3
Transport
Network Data Link Physical
2
1
4
3
Transport
Network Data Link Physical
2
1
Presentation
Session
Concerned with the meaning of data bits Convert between machine representations
Examples: XDR, ASN.1, MIME, MIDI
4
3
Transport
Network Data Link Physical
2
1
6
5
Presentation
Session
4
3
Transport
Network Data Link Physical
Examples: email (SMTP, POP, IMAP) file transfer (FTP)
directory services (LDAP)
2
1
Adapter
expansion slot (PCI, PC Card, USB dongle) usually integrated onto main board
Media
Wires (or RF, IR) connecting together the devices that make up a LAN
Twisted pair
Most common: STP: shielded twisted pair UTP: unshielded twisted pair (e.g. Telephone cable, Ethernet 10BaseT)
Coaxial cable
Thin (similar to TV cable) Thick (e.g., 10Base5, ThickNet)
Fiber Wireless
Switch
Moves data from input to output port. Analyzes packet to determine destination port and makes a virtual connection between the ports.
Concentrator or repeater
Regenerates data passing through it
Bridge
Connects two LANs or two segments of a LAN Connection at data link layer (layer 2)
Router
Determines the next network point to which a packet should be forwarded Connects different types of local and wide area networks at network layer (layer 3)
Networking Topology
Bus Network
Networking Topology
Tree Network
Networking Topology
Star Network
Networking Topology
Ring Network
Networking Topology
Mesh Network
To offer a service, a server must get a transport address for a particular service
Transport address
Transport provider
Layer of software that accepts a network message and sends it to a remote machine
Two categories: connection-oriented protocols connectionless protocols
Connection-oriented Protocols
1. 2. 3. 4. establish connection [negotiate protocol] exchange data terminate connection
Connection-oriented Protocols
1. 2. 3. 4. establish connection [negotiate protocol] exchange data terminate connection
Connectionless Protocols
- no call setup - send/receive data (each packet addressed) - no termination
Connectionless Protocols
- no call setup - send/receive data drop letter in mailbox (each packet addressed) (each letter addressed) - no termination analogous to mailbox
datagram service
client is not positive whether message arrived at destination no state has to be maintained at client or server cheaper but less reliable than virtual circuit service
Ethernet
Layers 1 & 2 of OSI model
Physical (1)
Cables: 10Base-T, 100Base-T, 1000Base-T, etc.
Ethernet
48-byte ethernet address Variable-length packet
1518-byte MTU
18-byte header, 1500 bytes data
6 bytes
6 bytes
46-1500 bytes
18 bytes + data
IP Internet Protocol
Born in 1969 as a research network of 4 machines Funded by DoDs ARPA
Goal:
build an efficient fault-tolerant network that could connect heterogeneous machines and link separately connected networks.
Internet Protocol
Connectionless protocol designed to handle the interconnection of a large number of loca and wide-area networks that comprise the internet IP can route from one physical network to another
IP Addressing
Each machine on an IP network is assigned a unique 32-bit number for each network interface:
IP address, not machine address
IP Address space
32-bit addresses >4 billion addresses!
Routers would need a table of 4 billion entries
remus.rutgers.edu 128.6.13.3 80 06 0D 03
network #
host #
first 16 bits identify Rutgers external routers need only one entry
route 128.6.*.* to Rutgers
use network number to route packet to correct network use host number to identify specific machine
IP Addressing
Expectation:
class a few big networks and many small ones create different classes of networks use leading bitsbits to identify network leading bits for net # bits for host
A 0 7 (128) 24 (16M) B 10 14 (16K) 16 (64K) C 110 21 (2M) 8 (256) To allow additional networks within an organization: use high bits of host number for a network within a network subnet
IP Addressing
IBM: 9.0.0.0 9.255.255.255
00001001 xxxxxxxx xxxxxxxx xxxxxxxxx
network #
8 bits
host #
24 bits
Subnet within IBM (internal routers only) 00001001 10101010 11 xxxxxx xxxxxxxxx network #
18 bits
host #
14 bits
Every machine connected to the internet needed a worldwide-unique IP address Solutions: CIDR, NAT, IPv6
IP Special Addresses
All bits 0
Valid only as source address all addresses for this machine Not valid over network
All bits 1
Broadcast to all directly connected networks
IPv6:
16-byte (128 bit) addresses 3.6 x 1038 possible addresses 8 x 1028 times more addresses than IPv4 4-bit priority field Flow label (24-bits)
.1
.2
.3
.4
.5
IP driver
receive packet from wire send packet to wire
network driver
IP driver responsibilities
Get operating parameters from device driver
Maximum packet size (MTU) Functions to initialize HW headers Length of HW header
Routing packets
From one physical network to another
Fragmenting packets Send operations from higher-layers Receiving data from device driver Dropping bad/expired data
Network device
Network card examines packets on wire
Compares destination addresses
Device addressing
IP address ethernet address
Address Resolution Protocol (ARP)
1. Check local ARP cache 2. Send broadcast message requesting ethernet address of machine with certain IP address 3. Wait for response (with timeout)
Transport layer uses a port number to identify the application TCP Transmission Control Protocol UDP User Datagram Protocol
IP header
device header
IP header
TCP/UDP header
payload
IP data
vers
hlen
TTL
source IP address
destination IP address options and pad
20 bytes
fragment identification
fragment offset
TCP header
src port dest port
UDP header
src port dest port checksum
ack number
hdr len
20 bytes
seg length
8 bytes
checksum
dest addr
src addr
data
CRC
6 bytes
6 bytes
46-1500 bytes
18 bytes + data
Unreliable delivery
Software to the rescue TCP/IP
Programming Interfaces
Sockets
IP lets us send data between machines
TCP & UDP are transport layer protocols
Contain port number to identify transport endpoint (application)
Sockets
Attempt at generalized IPC model Goals:
communication between processes should not depend on whether they are on the same machine efficiency compatibility support different protocols and naming conventions
Socket
Abstract object from which messages are sent and received
Looks like a file descriptor Application can select particular style of communication
Virtual circuit, datagram, message-based, inorder delivery
Step 1
int s = socket(domain, type, protocol)
AF_INET SOCK_STREAM useful if some SOCK_DGRAM families have
Create a socket
Step 2
int error = bind(s, addr, addrlen)
socket
Address structure struct sockaddr*
Step 3a (server)
Set socket to be able to accept connections
Step 3b (server)
int snew = accept(s, clntaddr, &clntalen)
pointer to address structure length of address structure
Step 3 (client)
int error = connect(s, svraddr, svraddrlen) socket
length of address structure
Connect to server
Step 4
Exchange data
Connection-oriented read/write recv/send (extra flags)
Connectionless sendto, sendmsg recvfrom, recvmsg
Step 5
Close connection
shutdown(s, how)
how:
0: can send but not receive 1: cannot send more data 2: cannot send or receive (=0+1)
Sockets in Java
java.net package
Two major classes:
Socket: client-side ServerSocket: server-side
Step 1a (server)
Create socket and name it
ServerSocket svc = new ServerSocket(port)
Step 1b (server)
Wait for connection from client
Server req = svc.accept()
new socket for client session
Step 1 (client)
Create socket and name it
Socket s = new Socket(address, port);
obtained from: getLocalHost, getByName, or getAllByName
Step 2
Exchange data obtain InputStream/OutputStream from Socket object
BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream())); PrintStream out = new PrintStream(s.getOutputStream());
Step 3
Terminate connection close streams, close socket
in.close(); out.close(); s.close();
The end.