Client-Server Model Message Passing
• Mechanism to pass data between two processes A Sender sends a message from its memory A Receiver receives the message and places it into its memory
Server • Message passing is like using a telephone (file server, A Caller NetworkNetwork mail server, A Receiver Client web server) Web browser Passive participant Emailer Waiting to be contacted Applications
Active participant Initiate contacts
1 2
Network Subsystem Names and Addresses
• Host name User Application A like a post office name; e.g., www.cs.princeton.edu Level program Socket API • Host address like a zip code; e.g., 128.112.92.191 TCP or UDP Reliable data stream A or unreliable data grams • Port number A like a mailbox; e.g., 0-64k Kernel IP Routes through the internet Level
Device Driver Transmit or receive on LAN
HW NIC Network interface card
3 4 Socket Steps for Client and Server
• Socket abstraction Client Server A An end-point of network connection • Create a socket with the socket() • Create a socket with the socket() A Treat like a file descriptor system call system call • Conceptually like a telephone • Connect the socket to the • Bind the socket to an address A Connect to the end of a phone plug address of the server using the using the bind() system call. For connect() system call a server socket on the Internet, A You can speak to it and listen to it an address consists of a port • Send and receive data, using number on the host machine. write() and read() system calls or send() and recv() system calls • Listen for connections with the listen() system call NetworkNetwork • Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. 5 • Send and receive data 6
client.c (part 1) client.c (part 2)
7 8 server.c (part 1) server.c (part 2)
9 10
Creating A Socket (Install A Phone) Connecting To A Socket
• Creating a socket #include
• Like installing a phone • Active open a socket (like dialing a phone number) A Need to what services you want int connect(int socket, – Local or long distance struct sockaddr *addr, – Voice or data int addr_len) – Which company do you want to use 11 12 Binding A Socket Specifying Queued Connections
server.c
server.c • Need to give the created socket an address to listen to (like getting a phone number) • Queue connection requests (like “call waiting”) int listen(int socket, int backlog) int bind(int socket, struct sockaddr *addr, – Set up the maximum number of requests that will be queued before being denied (usually the max is 5) int addr_len) – Passive open on a server
13 14
Accepting A Socket Sending Data
client.c server.c • Sending a message • Wait for a call to a socket (picking up a phone when it rings) int send(int socket, char *buf, int blen, int flags) int accept(int socket, struct sockaddr *addr, int addr_len) – Return a socket which is connected to the caller – Typically blocks until the client connects to the socket 15 16 Receiving Data Close A Socket
server.c
• Receiving a message • Done with a socket (like hanging up the phone) int recv(int socket, char *buf, int blen, int flags) close(int socket)
• Treat it just like a file descriptor 17 18
Summary
• Pipes A Process communication on the same machine A Connecting processes with stdin and stdout • Messages A Process communication across machines A Socket is a common communication channels A They are built on top of basic communication mechanisms
19