1Client-Server ModelServer(file server,mail server, web server)NetworkNetworkPassive participantWaiting to be contactedClientWeb browserEmailerApplicationsActive participantInitiate contacts2Message Passing• Mechanism to pass data between two processes Sender sends a message from its memory Receiver receives the message and places it into its memory• Message passing is like using a telephone Caller Receiver3Network SubsystemApplication programTCP or UDPIPDevice DriverNICUserLevelKernelLevelHWSocket APIReliable data streamor unreliable data gramsRoutes through the internetTransmit or receive on LANNetwork interface card4Names and Addresses• Host name like a post office name; e.g., www.cs.princeton.edu• Host address like a zip code; e.g., 128.112.92.191• Port number like a mailbox; e.g., 0-64k5Socket• Socket abstraction An end-point of network connection Treat like a file descriptor• Conceptually like a telephone Connect to the end of a phone plug You can speak to it and listen to itNetworkNetwork6Steps for Client and ServerClient• Create a socket with the socket() system call • Connect the socket to the address of the server using the connect() system call • Send and receive data, using write() and read() system calls or send() and recv() system calls Server • Create a socket with the socket() system call • Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine. • Listen for connections with the listen() system call • Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. • Send and receive data 7client.c (part 1)8client.c (part 2)9server.c (part 1)10server.c (part 2)11Creating A Socket (Install A Phone)• Creating a socket#include <sys/types.h>#include <sys/socket.h>int socket(int domain, int type, int protocol)– Domain: PF_INET (Internet), PF_UNIX (local)– Type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW– Protocol: 0 usually for IP (see /etc/protocols for details)• Like installing a phone Need to what services you want– Local or long distance– Voice or data– Which company do you want to use12Connecting To A Socket• Active open a socket (like dialing a phone number)int connect(int socket, struct sockaddr *addr, int addr_len)13Binding A Socket• Need to give the created socket an address to listen to(like getting a phone number)int bind(int socket,struct sockaddr *addr, int addr_len)– Passive open on a serverserver.c14Specifying Queued Connections• Queue connection requests (like “call waiting”)int listen(int socket, int backlog)– Set up the maximum number of requests that will be queued before being denied (usually the max is 5)server.c15Accepting A Socket• Wait for a call to a socket (picking up a phone when it rings)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 socketserver.c16Sending Data• Sending a messageint send(int socket, char *buf, int blen, int flags)client.c17Receiving Data• Receiving a messageint recv(int socket, char *buf, int blen, int flags)server.c18Close A Socket• Done with a socket (like hanging up the phone)close(int socket)• Treat it just like a file descriptor19Summary• Pipes Process communication on the same machine Connecting processes with stdin and stdout• Messages Process communication across machines Socket is a common communication channels They are built on top of basic communication
View Full Document