Академический Документы
Профессиональный Документы
Культура Документы
Network Encapsulation
Physical Link Network Transport Application
TCP Basics
Transport Layer Protocol
Packets delivered in order
Datagrams contain sequence numbers
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
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 )
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
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
accept()
close()
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
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
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
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
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
Processes fork()
Traditional way of spawning a child process
fork()
Original Process
fork() returns process ID of spawned child
Child Process
fork() returns 0
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
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