Outline • Communications in Distributed Systems • Socket Programming 5/29/2016

advertisement
Outline
• Communications in Distributed Systems
• Socket Programming
5/29/2016
COP5611
1
Distributed Systems
• A distributed system is a collection of
independent computers that appears to its
users as a single coherent system
– Independent computers mean that they do not
share memory or clock
– The computers communicate with each other by
exchanging messages over a communication
network
5/29/2016
COP5611
2
Distributed Systems – cont.
5/29/2016
COP5611
3
Necessity of Protocols
• Due to the absence of shared memory, all
communication in distributed systems is
based on exchanging messages at low level
– When process A wants to communicate with
process B, it builds a message in its own space
and then executes a system call to send it to B
– To prevent chaos, A and B must agree on the
meaning of the bits being sent
5/29/2016
COP5611
4
Layered Protocols
• Layers, interfaces, and protocols in the OSI model.
5/29/2016
COP5611
5
Layered Protocols – cont.
• A typical message as it appears on the network.
5/29/2016
COP5611
6
Physical Layer
• It is concerned how to transmit 0’s and 1’s
– There are technical issues involved such as
unidirectional or bidirectional
• These issues are not interesting to us
5/29/2016
COP5611
7
Data Link Layer
• Discussion between a receiver and a sender in the data link layer.
5/29/2016
COP5611
8
Network Layer
• The primary task of a network layer is routing
• The most widely used network protocol is the
connection-less IP (Internet Protocol)
– Each IP packet is routed to its destination
independent of all others
• A connection-oriented protocol is gaining
popularity
– Virtual channel in ATM networks
5/29/2016
COP5611
9
Transport Layer
• This layer is the last part of a basic network protocol
stack
– In other words, this layer can be used by application
developers
• An important aspect of this layer is to provide end-toend communication
– The Internet transport protocol is called TCP (Transmission
Control Protocol)
– The Internet protocol also supports a connectionless transport
protocol called UDP (Universal Datagram Protocol)
5/29/2016
COP5611
10
Client-Server TCP
a) Normal
operation of TCP.
2-4
b) Transactional TCP.
5/29/2016
COP5611
11
Higher Level Protocols
• In the OSI model, there are three additional
layers above the transport layer
– In practice, only the application layer is ever used
– In the Internet protocol suite, everything above
the transport layer is grouped together
– In the face middleware systems, it has two layers
above the transport layer
5/29/2016
COP5611
12
Application Protocols
• File Transfer Protocol (FTP)
– It defines a protocol for transferring files
between a client and a server
• HyperText Transfer Protocol (HTTP)
– Designed to remotely manage and handle the
transfer of web pages
– Mainly used by web servers and browsers
– It is also used in other applications such as Java’s
RMI (Remote Method Invocations)
5/29/2016
COP5611
13
HTTP Methods
• Operations supported by HTTP.
Operation
Description
Head
Request to return the header of a document
Get
Request to return a document to the client
Put
Request to store a document
Post
Provide data that is to be added to a document (collection)
Delete
Request to delete a document
5/29/2016
COP5611
14
Application Protocols – cont.
• HTTPS
– HTTP protocol running on
Transport Layer Security (TLS),
originally known as Secure Socket
Layer
– TLS is an application-independent
security protocol that is logically
on top of the transport layer
• TLS implementations are usually
based on TCP
5/29/2016
COP5611
15
Middleware Protocols
• An adapted reference model for networked communication.
5/29/2016
COP5611
16
Sockets
• Socket primitives for TCP/IP.
5/29/2016
Primitive
Meaning
Socket
Create a new communication endpoint
Bind
Attach a local address to a socket
Listen
Announce willingness to accept connections
Accept
Block caller until a connection request arrives
Connect
Actively attempt to establish a connection
Send
Send some data over the connection
Receive
Receive some data over the connection
Close
Release the connection
COP5611
17
Sockets – cont.
• Connection-oriented communication pattern using sockets.
5/29/2016
COP5611
18
Socket Programming
• Socket Programming
– Based on TCP/UDP.
• TCP Transmission control protocol.
– connection-oriented, reliable, full duplex, byte
stream service
– Interface: socket, bind, listen, accept, connect,
read, write, close.
5/29/2016
COP5611
19
An analogy
– Socket: telephone
– Bind: assign telephone number to a telephone
– Listen: turn on the ringer so that you can hear the
phone call
– Connect: dial a phone number
– Accept: answer the phone
– Read/write: talking
– Close: Hang-up
5/29/2016
COP5611
20
The Basics
• To send:
– Socket, connect write
• To receive:
– Socket, bind, listen, accept read
• TCP endpoint:
– IP address + port number
5/29/2016
COP5611
21
Basic TCP Sockets
#include <sys/socket.h>
int socket(int family, int type, int protocol);
– Family: AF_INET (PF_INET).
– Type: SOCK_STREAM (TCP) SOCK_DGRAM
(UDP)
– Protocol: = 0
– Return descriptor, -1 on error.
5/29/2016
COP5611
22
Connect
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr*
servaddr, socklen_t addrlen);
Servaddr: socket address structure (ip address and port)
5/29/2016
COP5611
23
• Socket Address structure:
struct in_addr {
in_addr_t s_addr;
}
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t
sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
}
• Always use sockaddr_in type for manipulation and convert it
to sockaddr. See example1.c.
5/29/2016
COP5611
24
Bind
• Client does not have to bind, system assigns a
dynamic port number.
– #include <sys/socket.h>
– int bind(int sockfd, const struct sockaddr
&myaddr, socklen_t addlen);
5/29/2016
COP5611
25
Myaddr
• Myaddr
– (address, port) = (INADDR_ANY, 0)
•
system assigns addr and port.
– = (INADDR_ANY, !0)
• system selects addr, user selects port
– =(Local IP address, 0)
• user selects addr, system selects port
– =(Local IP address,!0)
• user selects both addr and port
• See example2.c
5/29/2016
COP5611
26
Listen
• Convert a socket into a passive socket
– #include <sys/socket.h>
– int listen(int sockfd, int backlog)
– Backlog: number of connections that the kernel
should queue for the socket.
5/29/2016
COP5611
27
Accept
• Accept
– Blocking by default
– #include <sys/socket.h>
– int accept (int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);
– Return client’s address in cliaddr
– See example2.c
5/29/2016
COP5611
28
• What happen when we run
example2.c as server on diablo and
example1.c as client on linprog?
–Sockaddr_in revisit
• sin_port and sin_addr must be
in network byte order.
–Check example3.c, what is
the difference between
diablo and quake?
5/29/2016
COP5611
29
Functions to Convert Byte Orders
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
uint16_t ntohs(uint16_t net16bitvalue);
Uint32_t ntohl(uint32_t net32bitvalue);
5/29/2016
COP5611
30
Some Useful Functions
• Some byte manipulation functions:
#include <strings.h>
Void *memset(void *dst, int c, size_t len);
Void *memcpy(void *dst, void *src, size_t nbytes);
Void *memcmp(const void *ptr1, const void *ptr2,
size_t nbytes);
• Address conversion functions
• inet_aton/inet_addr/inet_ntoa
5/29/2016
COP5611
31
Download