140 likes | 250 Views
CS 311 – Lecture 18 Outline. IPC via Sockets Server side socket() bind() accept() listen() Client side connect(). Sockets. Sockets are IPC mechanism which allows processes in same or different machines to communicate. Printing a file on one machine from another machine
E N D
CS 311 – Lecture 18 Outline • IPC via Sockets • Server side • socket() • bind() • accept() • listen() • Client side • connect() CS 311 - Operating Systems I
Sockets • Sockets are IPC mechanism which allows processes in same or different machines to communicate. • Printing a file on one machine from another machine • Transferring files between machines. • IPC via sockets follows a client-server model. (Server creates a socket and the clients talks over the socket) • Sockets connections are bidirectional. CS 311 - Operating Systems I
Server Sockets • A server socket listens on a given port • Many different clients may be connecting to that port • Ideally, you would like a separate file descriptor for each client connection Web Server Server Socket/Port Client 1 Client 2 Client 3 Client n CS 311 - Operating Systems I
Web Server New socket Server Socket/Port Server Sockets 1: Client requests connection 2: Server creates new file descriptor for client which is used for bi-directional communication 3: Other clients requesting connections can now be serviced. Client 1 Client 2 CS 311 - Operating Systems I
Socket types • Classified into • Domain • PF_LOCAL – client and server in same machine • PF_INET – client and server on different machines • PF_INET6 – client and server on IPv6 network • Type • SOCK_STREAM – sequenced, reliable, 2-way connection (TCP) • SOCK_DGRAM – connectionless, unreliable. (UDP) • Protocol • Specifies the low-level means by which the socket type is implemented. CS 311 - Operating Systems I
Server Sockets • How to use the socket API to listen for an accept connections • Start by describing a non-concurrent implementation of a server (only one thread of execution) • Procedure • Create network endpoint with socket() • Bind socket to a port - bind() • Start listening for connections - listen() • Loop and accept connections - accept() • Read and write data to client - send(), recv(), read(), write() CS 311 - Operating Systems I
Creating a socket • Using socket() system call • Prototype: int socket (int domain, int type, int protocol) • Creates an unnamed socket • Returns a server file descriptor CS 311 - Operating Systems I
Naming a socket • Ports allow multiple network processes on a machine with a single address • A server has to choose a port where clients can contact it • bind() associates the chosen port with a socket already created with the socket() command • Protoype: int bind (int fd, const struct sockaddr* address, size_t addressLen) CS 311 - Operating Systems I
structsockaddr_in and sockaddr_un • Both the structures are used to specify a port and an address for the socket. • structsockaddr_un (For local sockets) • sun_family - PF_LOCAL • sun_path - the full pathname of the socket (absolute or relative), up to 108 characters long • structsockaddr_in (For network sockets) • sin_family – address format (PF_INET or PF_LOCAL) • sin_port - port number of internet socket • sin_addr – a structure of type in_addr that holds the internet address CS 311 - Operating Systems I
Setting up an address • Server accepting connections: sin_family = PF_INET (or AF_INET); sin_port = htons(7000); (change machine byte order to network byte order) sin_addr.s_addr = INADDR_ANY; or inet_addr(“172.154.28.1”); • http://beej.us/guide/bgnet/output/html/multipage/htonsman.html • http://www.umiacs.com/inet_ntoaman.html CS 311 - Operating Systems I
Listening for Connections • The server will ignore any connection attempts until you tell the socket() to start listening • This is done with listen() • Prototype: int listen (int fd, int queueLength) • Queue length specifies maximum number of pending connections on a socket • If a client attempts a connection to a socket whose queue is full, it is denied. CS 311 - Operating Systems I
Loop and Accept • Servers generally run continually, waiting for clients to contact them • The accept() function takes the next connection off the listen queue or blocks the process until a connection arrives • Prototype: int accept (int fd, struct sockaddr* address, int* addressLen) • returns a new file descriptor that may be used to talk with the client; otherwise, it returns -1. • The address structure is filled with the address of the client. • http://www.retran.com/beej/sockaddr_inman.html CS 311 - Operating Systems I
Serving a client • This is the most common sequence of events that take place when a client connection succeeds • The server process forks • The parent process closes the newly formed client fds and loops back to accept() • The child process talks to the client using read() and write() or send() and recv(). • When the conversation is complete, the child process closes the client fds and exits. CS 311 - Operating Systems I
Client-side sockets • The client in order to connect to the server socket creates its own socket using the socket() system call • clientFd = socket(PF_LOCAL, SOCK_STREAM, PROTOCOL); • Client connects to server socket using the connect() system call • Prototype: int connect (intfd, structsockaddr* address, intaddressLen) • address should be the server socket address. • PF_LOCAL – sockaddr_un casted to sockaddr * • PF_INET – sockaddr_in casted to sockaddr * CS 311 - Operating Systems I