Process Communication COMPUTER NETWORKING Part...
Transcript of Process Communication COMPUTER NETWORKING Part...
1.1 TS Distributed Systems
Process Communication
COMPUTER NETWORKING
Part 2
Thanks to the authors of the textbook [USP] and [KR] for providing the base slides. I made several changes/additions. These slides may incorporate materials kindly provided by Prof. Dakai Zhu.
So I would like to thank him, too. Turgay Korkmaz
Client-server paradigm and Socket Programming
ch 18
1.2 TS Distributed Systems
Computer Networking
Layered Protocols
Grand tour of computer
networking, the Internet
Client-server paradigm,
Socket Programming
1.3 TS Distributed Systems
Objectives
To understand how processes communicate (the
heart of distributed systems)
To understand computer networks and their layers
(part 1)
To understand client-server paradigm and
Use low-level message passing using sockets
1.4 TS Distributed Systems
CLIENT-SERVER
COMMUNICATION MODELS
The client/server model underlies almost every distributed system
(networked applications).
Hence it is important to understand the principles of client/server
communication.
1.5 TS Distributed Systems
Client-server architecture clients:
communicate with server
may have dynamic IP
addresses and port
do not communicate
directly with each other
may be intermittently
connected to the network
From Computer Networking by Kurose and Ross.
Clients and servers
communicate through
Socket, RPC, RMI
servers:
always-on host
permanent IP address,
well-known port
server farms for scaling
1.6 TS Distributed Systems
Request Protocol (R)
If service
does not have output parameters and
does not have a return type
client may not want to wait for server to finish.
request
send(...)
Client Server
receive(...)
exec op;
Continue execution
OPT
1.7 TS Distributed Systems
Request-Reply protocol (RR)
send(...)
receive(...)
Client Server
receive(...)
exec op;
send(...)
request
reply
blocked
To be applied if client expects result from server
Client requests service execution from server through
request message, and
Delivery of service result in reply message
Most client-server interactions are built on
RR protocol
message exchange protocol : format, order, action
1.8 TS Distributed Systems
Request-Reply-Acknowledge Protocol (RRA)
In addition to RR protocol, client sends
acknowledgement after it received reply
Acknowledgement sent asynchronously
send(...)
receive(...)
send (...)
Client Server
receive(...)
exec op;
send(...)
receive(...)
request
reply
history
OPT
1.9 TS Distributed Systems
Issues in Client-Server Communication
Addressing (IP address, port number)
Blocking versus non-blocking
Buffered versus unbuffered
Reliable versus unreliable
Server architecture:
concurrent versus sequential
Scalability
1.10 TS Distributed Systems
Addressing Issues
Question: how does the client
know where the server is located?
Hard-wired address
Machine address and process
address are known a priori
(www.cs.utsa.edu port 80)
DNS converts www… to 32-bit IP
Broadcast-based
Server chooses address from a
sparse address space
Client broadcasts request
Can cache response for future
Locate address via name server
user server
user server
user server NS
OPT
1.11 TS Distributed Systems
Blocking versus Non-blocking
Blocking communication (synchronous)
Sender blocks until message is actually sent
Receiver blocks until message is actually received
Non-blocking communication (asynchronous)
Sender returns immediately
Receiver does not block either
Examples:
OPT
1.12 TS Distributed Systems
Buffering Issues
Unbuffered communication
Server must call receive
before client can call send
Buffered communication
Client send to a mailbox
Server receives from a
mailbox
user server
user server
OPT
1.13 TS Distributed Systems
Reliability
Unreliable channel
Need acknowledgements (ACKs)
Applications handle ACKs
ACKs for both request and reply
Reliable channel
Reply acts as ACK for request
Explicit ACK for response
Reliable communication on unreliable channels
Transport protocol (TCP) handles lost messages
request
ACK
reply
ACK
User
Serv
er
request
reply
ACK
User
Serv
er
OPT
1.14 TS Distributed Systems
Server Architecture
Sequential
Serve one request at a time
Can service multiple requests by employing
events and asynchronous communication
Concurrent
Server spawns a process (or thread) to service each request
Can also use a pre-spawned pool of threads/processes
(apache)
Thus servers could be
Pure-sequential, event-based, thread-based, process-based
Discussion: which architecture is most efficient?
1.15 TS Distributed Systems
Scalability
Question: How can you scale the server capacity?
Buy bigger machine!
Replicate
Distribute data and/or algorithms
Ship code instead of data
Cache
OPT
1.19 TS Distributed Systems
SOCKET PROGRAMMING
Learn how to build client/server applications that
communicate with each other using sockets
From Computer Networking by Kurose and Ross.
1.20 TS Distributed Systems
What is a socket?
Socket API
introduced in BSD4.1 UNIX,
1981
explicitly created, used,
released by apps
client/server paradigm
two types of transport service
via socket API:
UDP: unreliable datagram
TCP: reliable, in-order
byte-stream
a host-local, application-created,
OS-controlled interface (a “door”) that
hides the details of all the layers and
provides transport services to application process so it can send and receive messages
to/from another application process
socket
From Computer Networking by Kurose and Ross.
1.22 TS Distributed Systems
Socket-programming using TCP
process
TCP with buffers, variables
socket
controlled by application developer
controlled by operating
system
process
TCP with buffers, variables
socket
controlled by application developer
controlled by operating system internet
From Computer Networking by Kurose and Ross.
client server
TCP service:
connection-oriented,
reliable,
in-order byte-stream
1.23 TS Distributed Systems
Socket programming with TCP
Client must know
server’s IP and port
Client then
creates a TCP socket
connects it to server by
specifying IP address and
port number of server
process
read/write data
Server process must first be
running
Server creates a TCP welcome
socket and binds it to a known
port and waits for requests
When contacted by client, server
creates new TCP socket for server
process to communicate with client
read/write data
From Computer Networking by Kurose and Ross.
Server can talk with multiple clients TCP socket is identified by 4-tuple:
source IP address source port number dest IP address dest port number
1.24 TS Distributed Systems
Client/server socket interaction: TCP
wait for incoming
connection request connectionSocket =
accept(welcomeSocket)
// create a TCP socket, // bind to port x,
welcomeSocket = socket(…,SOCK_STREAM,0);
bind(welcomeSocket, x);
listen(welcomeSocket); // create socket, // connect to hostid, port x
clientSocket = socket( …, SOCK_STREAM, 0)
connect(clientSocket, hostid, x)
close
connectionSocket
read reply from
clientSocket
close
clientSocket
Server (running on hostid, port x) Client (running on hostname ?, port ?)
write request using
clientSocket read request from
connectionSocket
write reply to
connectionSocket
TCP connection setup
*****
1.25 TS Distributed Systems
Connection-oriented demux: Threaded Web Server
Client IP:B
P1
client IP: A
P1 P2
server IP: C
P4 P3
SP: 9157
DP: 80
S-IP: A
D-IP:C
SP: 9157
DP: 80
D-IP:C
S-IP: B
SP: 5775
DP: 80
D-IP:C
S-IP: B
From Computer Networking by Kurose and Ross.
SP: 9157
DP: 80
S-IP: A
D-IP:C
connect
Server can fork a child process to handle each request from different clients
Too costly, use threads!
1.26 TS Distributed Systems
TCP Socket Primitives
Primitive Function
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
Recv Receive some data over the connection
Close Release the connection
Connection socket
*****
1.29 TS Distributed Systems
Example: C server (TCP)
/* server.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
struct sockaddr_in cad;
int welcomeSocket, connectionSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
char clientSentence[128];
char capitalizedSentence[128];
port = atoi(argv[1]);
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */
sad.sin_port = htons((u_short)port);/* set the port number */
bind(welcomeSocket, (struct sockaddr *)&sad, sizeof(sad));
Create welcoming socket at port &
Bind a local address
Complete example is posted at the class
web site. See the socket programming link
under “Online Materials”
1.30 TS Distributed Systems
Example: C server (TCP), cont /* Specify the maximum number of clients that can be queued */
listen(welcomeSocket, 10)
while(1) {
connectionSocket=accept(welcomeSocket, (struct sockaddr *)&cad, &alen);
n=read(connectionSocket, clientSentence, sizeof(clientSentence));
/* capitalize Sentence and store the result in capitalizedSentence*/
n=write(connectionSocket, capitalizedSentence, strlen(capitalizedSentence)+1);
close(connectionSocket);
}
}
Write out the result to socket
End of while loop, loop back and wait for another client connection
Wait, on welcoming socket for contact by a client
1.31 TS Distributed Systems
Example: C client (TCP)
/* client.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
int clientSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
char Sentence[128];
char modifiedSentence[128];
host = argv[1]; port = atoi(argv[2]);
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_port = htons((u_short)port);
ptrh = gethostbyname(host); /* Convert host name to IP address */
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
connect(clientSocket, (struct sockaddr *)&sad, sizeof(sad));
Create client socket, connect to server
1.32 TS Distributed Systems
Example: C client (TCP), cont
gets(Sentence);
n=write(clientSocket, Sentence, strlen(Sentence)+1);
n=read(clientSocket, modifiedSentence, sizeof(modifiedSentence));
printf("FROM SERVER: %s\n”,modifiedSentence);
close(clientSocket);
}
Get input stream
from user
Send line to server
Read line from server
Close connection
1.33 TS Distributed Systems
Other related functions
getpeername( )
gethostbyname( )
gethostbyaddr( )
getsockopt( )
setsockopt ( )
sigaction(SIGINT,…);
if ( (pid=fork()) == 0) { /* CHILD PROC */ close(welcomeSocket);
/* give service */ exit(0); } /* PARENT PROC */ close(connectionSocket);
Too costly, use threads!
1.34 TS Distributed Systems
Waiting something from both
socket and stdin
FD_ZERO(&rset);
FD_SET(welcomeSocket, &rset);
FD_SET(fileno(stdin), &rset);
maxfd =max(welcomeSocket,fileno(stdin)) + 1;
select(maxfd, &rset, NULL, NULL, NULL);
if (FD_ISSET(fileno(stdin), &rset)){
/* read something from stdin */
}
OPT
1.35 TS Distributed Systems
Exercise: implement a client-server program using TCP sockets.
The client will first establish a TCP connection with the server. It then sends
the name of a customer and expects server to send back the balance of that
customer. Upon receiving the balance, the client will print it and quit.
The server always waits for connection requests in an infinite loop. Upon
receiving a connection request, the server (parent process) creates a child
process, which performs the task(s) expected by the client. The parent process
(server) will continue to wait for another client request. Suppose Server keeps
accounts in an array of
struct account { char name[20]; double balance;}
(create a static array of 10 customers with random names, balance in your
program)
Make sure you close the socket descriptors that are not needed in the
parent and child.
When implementing client and server programs, please focus on the
key system calls and in which order to call them while using generic
parameters (e.g., IPaddress, portnum) rather than actual structures.
Also ignore error checking for clarity and assume all the necessary
libraries are included.
1.36 TS Distributed Systems
Socket programming with UDP
UDP: no “connection” between
client and server
no handshaking
sender explicitly attaches IP
address and port of
destination to each packet
server must extract IP
address, port of sender from
received packet
UDP: transmitted data may be
received out of order, or lost
application viewpoint
UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server
From Computer Networking by Kurose and Ross.
OPT
1.37 TS Distributed Systems
Client/server socket interaction: UDP
close
clientSocket
Server (running on hostid, port x)
recv reply from
clientSocket
create a UDP socket,
clientSocket = socket(…, SOCK_DGRAM, 0);
Client (running on hostname ?, port ?)
Create, address (hostid, port=x)
send datagram request
using clientSocket
Create a UDP socket, port=x, for
incoming request: serverSocket = socket(…, SOCK_DGRAM, 0);
bind(serverSocket, x);
receive request from
serverSocket
send reply to
serverSocket
specifying client
host address,
port number
OPT
1.38 TS Distributed Systems
Connectionless demux (cont)
Server create UDP Socket and binds it to port 6428
Clients create a UDP socket (port number will be
dynamically assigned)
Client IP:B
P2
client IP: A
P1 P1 P3
server IP: C
SP: 6428
DP: 9157
SP: 9157
DP: 6428
SP: 6428
DP: 5775
SP: 5775
DP: 6428
SP provides “return address”
From Computer Networking by Kurose and Ross.
OPT
1.39 TS Distributed Systems
UDP: Connectionless demultiplexing
UDP socket is identified by
two-tuple:
(dest IP address, dest port number)
When host receives UDP
segment:
checks destination port
number in segment
directs UDP segment to
socket with that port
number
IP datagrams with
different source IP
addresses and/or source
port numbers directed to
same socket
From Computer Networking by Kurose and Ross.
OPT
1.40 TS Distributed Systems
Example: C server (UDP)
/* server.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
struct sockaddr_in cad;
int serverSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
char clientSentence[128];
char capitalizedSentence[128];
port = atoi(argv[1]);
serverSocket = socket(PF_INET, SOCK_DGRAM, 0);
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */
sad.sin_port = htons((u_short)port);/* set the port number */
bind(serverSocket, (struct sockaddr *)&sad, sizeof(sad));
Create welcoming socket at port &
Bind a local address
Complete example is posted at the class
web site. See the socket programming link
under “Online Materials”
OPT
1.41 TS Distributed Systems
Example: C server (UDP), cont
while(1) {
n=recvfrom(serverSocket, clientSentence, sizeof(clientSentence), 0
(struct sockaddr *) &cad, &addr_len );
/* capitalize Sentence and store the result in capitalizedSentence*/
n=sendto(serverSocket, capitalizedSentence, strlen(capitalizedSentence)+1,0
(struct sockaddr *) &cad, &addr_len);
}
close(serverSocket);
}
Write out the result to socket
End of while loop, loop back and wait for another client connection
Receive messages from clients
OPT
1.42 TS Distributed Systems
Example: C client (UDP)
/* client.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
int clientSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
char Sentence[128];
char modifiedSentence[128];
host = argv[1]; port = atoi(argv[2]);
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
/* determine the server's address */
memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET; /* set family to Internet */
sad.sin_port = htons((u_short)port);
ptrh = gethostbyname(host); /* Convert host name to IP address */
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
Create client socket, NO connection to server
OPT
1.43 TS Distributed Systems
Example: C client (UDP), cont.
gets(Sentence);
addr_len =sizeof(struct sockaddr);
n=sendto(clientSocket, Sentence, strlen(Sentence)+1,
(struct sockaddr *) &sad, addr_len);
n=recvfrom(clientSocket, modifiedSentence, sizeof(modifiedSentence).
(struct sockaddr *) &sad, &addr_len);
printf("FROM SERVER: %s\n”,modifiedSentence);
close(clientSocket);
}
Get input stream
from user
Send line to server
Read line from server
Close connection
OPT
1.44 TS Distributed Systems
SOCKET PROGRAMMING
JAVA
EXTRAS for self study
From Computer Networking by Kurose and Ross.
1.45 TS Distributed Systems
Client/server socket interaction: TCP
wait for incoming
connection request connectionSocket =
welcomeSocket.accept()
create socket, port=x, for
incoming request: welcomeSocket =
ServerSocket()
create socket, connect to hostid, port=x clientSocket =
Socket()
close
connectionSocket
read reply from
clientSocket
close
clientSocket
Server (running on hostid, port x) Client (running on hostname ?, port ?)
send request using
clientSocket read request from
connectionSocket
write reply to
connectionSocket
TCP connection setup
From Computer Networking by Kurose and Ross.
1.46 TS Distributed Systems
Socket programming with TCP
Example client-server app:
1) client reads line from standard input (inFromUser stream) , sends to server
via socket (outToServer stream)
2) server reads line from socket
3) server converts line to uppercase,
sends back to client
4) client reads, prints modified line from socket (inFromServer stream)
From Computer Networking by Kurose and Ross. o
utT
oS
erv
er
to network from network
inF
rom
Se
rve
r
inF
rom
Use
r
keyboard monitor
Process
clientSocket
input
stream
input
stream
output
stream
TCP
socket
Client process
client TCP socket
Stream jargon
A stream is a sequence of characters that flow
into or out of a process.
An input stream is attached to some input source
for the process, e.g., keyboard or socket.
An output stream is attached to an output
source, e.g., monitor or socket.
1.47 TS Distributed Systems
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
Create welcoming socket
at port 6789
Wait, on welcoming socket for contact
by client
Create input stream, attached
to socket
From Computer Networking by Kurose and Ross.
Complete example is posted at the class
web site. See the socket programming link
under “Online Materials”
1.48 TS Distributed Systems
Example: Java server (TCP), cont
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Read in line from socket
Create output stream,
attached to socket
Write out line to socket
End of while loop, loop back and wait for another client connection
From Computer Networking by Kurose and Ross.
1.49 TS Distributed Systems
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Create input stream
Create client socket,
connect to server
Create output stream
attached to socket
From Computer Networking by Kurose and Ross.
1.50 TS Distributed Systems
Example: Java client (TCP), cont.
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Create input stream
attached to socket
Send line to server
Read line from server
From Computer Networking by Kurose and Ross.
1.51 TS Distributed Systems
Client/server socket interaction: UDP
Server (running on hostid)
close
clientSocket
read datagram from
clientSocket
create socket,
clientSocket =
DatagramSocket()
Client (running on hostname ?, port ?)
Create datagram with server IP and
port=x; send datagram via
clientSocket
create socket,
port= x.
serverSocket =
DatagramSocket()
read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number
From Computer Networking by Kurose and Ross.
1.52 TS Distributed Systems
Example: Java client (UDP)
sen
dP
ack
et
to network from network
rece
ive
Pa
cke
t
inF
rom
Use
r
keyboard monitor
Process
clientSocket
UDP
packet
input
stream
UDP
packet
UDP
socket
Output: sends packet (recall that TCP sent “byte stream”)
Input: receives packet (recall thatTCP received “byte stream”)
Client process
client UDP socket
From Computer Networking by Kurose and Ross.
1.53 TS Distributed Systems
Example: Java server (UDP)
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Create datagram socket
at port 9876
Create space for received datagram
Receive datagra
m From Computer Networking by Kurose and Ross.
Complete example is posted at the class
web site. See the socket programming link
under “Online Materials”
1.54 TS Distributed Systems
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
serverSocket.send(sendPacket);
}
}
}
Get IP addr port #, of
sender
Write out datagram to socket
End of while loop, loop back and wait for another datagram
Create datagram to send to client
From Computer Networking by Kurose and Ross.
1.55 TS Distributed Systems
Example: Java client (UDP)
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(“localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Create input stream
Create client socket
Translate hostname to IP
address using DNS
From Computer Networking by Kurose and Ross.
1.56 TS Distributed Systems
Example: Java client (UDP), cont.
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Create datagram with data-to-send,
length, IP addr, port
Send datagram to server
Read datagram from server
From Computer Networking by Kurose and Ross.
1.57 TS Distributed Systems
Multi threaded import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}