Вы находитесь на странице: 1из 25

HET306 UNIX for Telecommunications

Writing Networked Applications Under Unix

The Network model


User Application Application Layer Presentation Layer Session Layer Transport Layer Network Layer Link Layer Physical Layer Actual Network User Application Application Layer Presentation Layer Session Layer Transport Layer Network Layer Link Layer Physical Layer

HET306 Slide Set 5 Writing Networked Applications under Unix

Network Encapsulation
Physical Link Network Transport Application

HET306 Slide Set 5 Writing Networked Applications under Unix

The Network model


Physical and Link Layers implemented in hardware and as Device Drivers Network (IP) Layer implemented within kernel Transport (TCP/UDP) Layer implemented within kernel An API must exist to allow applications (user space) to interact with the Transport Layer (kernel space)

HET306 Slide Set 5 Writing Networked Applications under Unix

OS Provided API Services


Name Resolution
IP Addresses are 32 bit binary resources DNS (Domain Naming System) allows human readable names DNS Servers resolve Names into IP Addresses Management of name resolution managed by OS

Transport Layer Connection Services IP Layer (Raw Network) Services


Allow generation of IP packets to transmit directly

HET306 Slide Set 5 Writing Networked Applications under Unix

TCP Basics
Transport Layer Protocol
Packets delivered in order
Datagrams contain sequence numbers

Guaranteed error-free delivery of data


Positive acknowledgement with retransmission No acknowledgement of datagrams with errors Use of a sliding transmission window Source will continue to transmit until all datagrams acknowledged

Protocol backs-off data rate when congestion is detected


Flow control procedures to manage throughput based on available bandwidth
HET306 Slide Set 5 Writing Networked Applications under Unix

TCP Application Interface


What does this mean at the Application Layer Once the TCP Handshake has established a connection
Can treat the connection as a pipe Data input at one end is received at the other end in the same order OS manages the intricacies of retransmission and buffering of datagrams

Sending data to a TCP socket is (almost) the equivalent of writing to a file Reading data from a TCP socket is (almost) the equivalent of reading from a file
HET306 Slide Set 5 Writing Networked Applications under Unix

UDP Basics
Like TCP uses source/destination ports to identify applications using the connection Provides a means for error detection based on a checksum Reduced protocol overheads more bandwidth efficient Immediate data transfer from source No
Guarantee of delivery Flow Control
HET306 Slide Set 5 Writing Networked Applications under Unix

TCP vs UDP
Uses for TCP
We want to transmit a stream of data reliably to its destination Overall transmission rate is unimportant Data being transmitted is of a stream nature

Uses for UDP


We want to send a burst of data quickly We dont want overheads of guaranteed transmission of data We dont want the protocol to back-off the transmission rate Stream set-up not required
Data update to central server Single packet requestresponse scenario

Typically used in non timecritical applications and in most client-server applications


WWW servers Database servers Email transfer

Typically used in real-time services


Online games Voice-over-IP (VoIP) Streaming multi-media

HET306 Slide Set 5 Writing Networked Applications under Unix

Application Layer Protocols


Application Layer Protocols sit on top of Transport Layer Protocols
http, smtp, ssh, etc.

Most services on Unix systems are programs that implement


An underlying server The corresponding Application Layer Protocol Interface to the network using the OS provided Transport Layer Interface

HET306 Slide Set 5 Writing Networked Applications under Unix

Sockets API
Access to the underlying OS provided API is accomplished using the Berkeley Sockets API Developed by Berkeley University
Many implementations available but all conform to this API standard (excepting Microsoft )

Allows network code to be cross-platform

HET306 Slide Set 5 Writing Networked Applications under Unix

Basic Sockets Functions


The following functions are introduced:
Socket: Create a socket in the system table Bind: Attach the socket to a particular port number Listen: Tell the system to begin listening for incoming calls on that socket Accept: Blocks the thread until an incoming call can be accepted, a new socket is created to connect to the call and the existing socket can continue listening Connect: Attempt to establish a connection to a socket Send: Send data over a connected socket Receive: Retrieve data from a connected socket Close: Close and release the socket connection

HET306 Slide Set 5 Writing Networked Applications under Unix

Sockets API
socket()
Allocates system resources for, and returns a handle (file descriptor) to the newly created socket
Descriptor then used in all future function calls on this socket Parameters include socket and network type

bind()
Attaches the socket to a TCP/UDP IP Address/Port Number pair
Can bind to specified or wildcard IP Address Can bind to specific or system selected port number

listen()
OS begins listening on the socket for incoming connections
Only valid for TCP sockets usually server side The OS perform the entire TCP connection handshake HET306 Slide Set 5 Writing Networked Applications under Unix

Sockets API
accept()
Accept any connections waiting on a listening socket
The TCP handshake has already been completed

Returns a socket descriptor to a newly created socket:


Original socket is still listening for new connections New socket is bound to same IP Address/Port Number pair as listening socket

connect()

Parameters include IP Address/Port Number pair of the remote socket Blocks until the connection is made Sends data to the remote end of the socket
TCP Sockets socket must be connected to a remote end UDP Sockets specify destination socket details for each datagram HET306 Slide Set 5 Writing Networked Applications under Unix

send()

Sockets API
receive()
Receives data from a remote socket
TCP Sockets socket must be connected to a remote end UDP Sockets function returns source socket details for each datagram received

close()
Closes any connections Releases any allocated resources

HET306 Slide Set 5 Writing Networked Applications under Unix

Sockets API Usage (TCP)


socket() socket() bind() listen() connect() send()/recv() close()
HET306 Slide Set 5 Writing Networked Applications under Unix

accept()

close()

Sockets API Usage (UDP)


socket() socket() send()/recv() close() close() bind()

HET306 Slide Set 5 Writing Networked Applications under Unix

Other Sockets Functions


gethostbyname(), gethostbyaddr()
Get details on a host given some information Many of these functions are non-reentrant need to be protected when called from concurrent threads

select()
Blocks on a list of sockets until one of those sockets would be unblocked

sendto(), recvfrom()
Send a UDP datagram on an unconnected socket

inet_xxxx()
Convert an IP address from one form to another

See man for more information


HET306 Slide Set 5 Writing Networked Applications under Unix

Blocking/Non-Blocking Sockets
Sockets are typically blocking
Call to send() will block until the kernel accepts data and queues it to send Call to recv() will block until data arrives over the network and has been processed by kernel

Can create sockets with socket options


Non-blocking sockets

Non-blocking sockets
Will return a special error code when the normal call would block Allows single threaded code to manage multiple concurrent input sources
HET306 Slide Set 5 Writing Networked Applications under Unix

Network Application Development


Synchronous
Events occur a pre-defined times Regular transmission of data over network Examples include VoIP, video streaming

Asynchronous
Events occur at random intervals Server response to user-initiated client actions Examples include most types of server based systems

Both types of systems still have issues of managing response times and use of system resources

HET306 Slide Set 5 Writing Networked Applications under Unix

Network Application Development


Most networked applications are not single-threaded
Multiple threads of execution allow dynamic responses across the entire system More typical in server systems which must respond to potentially hundreds/thousands of clients

Example implementations
One process/thread for each connected client One thread to manage input while another manages output This idea often results in simpler coding of servers where each client can be treated independently

HET306 Slide Set 5 Writing Networked Applications under Unix

Processes fork()
Traditional way of spawning a child process
fork()

When fork() is called


Unix makes an exact copy of the current process space creating a second process with exactly the same state

Original Process
fork() returns process ID of spawned child

Child Process
fork() returns 0

Common amongst all Unix systems


Platform/Unix version independent Used in many server products (eg. Apache)
HET306 Slide Set 5 Writing Networked Applications under Unix

fork() Example
Create a listening socket for a server Block/wait for an incoming connection Call fork() Parent
Needs to close the new connection still open in child Go back to waiting for next connection Handles client connection Closes connection when finished Terminates process
create_socket() listen() for(;;) { client_socket = accept(); if (int iPID = fork()) { // result is non zero parent close(client_socket); store iPID in list of PIDs to wait for on program termination } else { // result is zero child handle_client(client_socket); // finished with client, kill child close(client_socket); exit(0); } }

Child

HET306 Slide Set 5 Writing Networked Applications under Unix

Lightweight Threads
fork() uses system resources
A completely new process Entire process space needs to be copied Cant share variables

Lightweight threads
Creates a new process New process state (program counter, registers) Same memory pages

Threads
Shared memory Faster Easier inter-thread communication Multi-thread programming issues
HET306 Slide Set 5 Writing Networked Applications under Unix

Lightweight Threads
Not standardised amongst different Unix platforms Code portability requires development of Unix independent API on top of platform specific threads API Many newer (no history) applications are starting to be developed using threads

HET306 Slide Set 5 Writing Networked Applications under Unix

Вам также может понравиться