Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS 311 - Operating Systems.

Similar presentations

Presentation on theme: "CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS 311 - Operating Systems."— Presentation transcript:

1 CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS 311 - Operating Systems I

2 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. Lecture 182CS 311 - Operating Systems I

3 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 Client 1Client 2Client 3Client n Server Socket/Port Lecture 183CS 311 - Operating Systems I

4 Server Sockets Client 1 Web Server Server Socket/PortNew socket 2: Server creates new file descriptor for client which is used for bi-directional communication Client 2 1: Client requests connection 3: Other clients requesting connections can now be serviced. Lecture 184CS 311 - Operating Systems I

5 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. Lecture 185CS 311 - Operating Systems I

6 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() Lecture 186CS 311 - Operating Systems I

7 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 Lecture 187CS 311 - Operating Systems I

8 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) Lecture 188CS 311 - Operating Systems I

9 struct sockaddr_in and sockaddr_un Both the structures are used to specify a port and an address for the socket. struct sockaddr_un (For local sockets) – sun_family - PF_LOCAL – sun_path - the full pathname of the socket (absolute or relative), up to 108 characters long struct sockaddr_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 Lecture 189CS 311 - Operating Systems I

10 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(“”); ipage/htonsman.html ipage/htonsman.html Lecture 1810CS 311 - Operating Systems I

11 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. Lecture 1811CS 311 - Operating Systems I

12 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. Lecture 1812CS 311 - Operating Systems I

13 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. Lecture 1813CS 311 - Operating Systems I

14 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 (int fd, struct sockaddr* address, int addressLen) – address should be the server socket address. – PF_LOCAL – sockaddr_un casted to sockaddr * – PF_INET – sockaddr_in casted to sockaddr * Lecture 1814CS 311 - Operating Systems I

Download ppt "CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS 311 - Operating Systems."

Similar presentations

Ads by Google