Agenda WORKING WITH SOCKETS continued Sockets n Programming

  • Slides: 24
Download presentation
Agenda WORKING WITH SOCKETS / continued … Sockets n Programming for Multiple Clients n

Agenda WORKING WITH SOCKETS / continued … Sockets n Programming for Multiple Clients n n n Using the fork() system call Networking utilities (netstat) Using the select() system call

Multiple Clients n n In the last example, the server program was not able

Multiple Clients n n In the last example, the server program was not able to listen to two different clients at the same time. There are two possible solutions: n n Using the fork() system call – create a “child process” to handle each client. This is considered useful for handling multiple clients, but requires considerable programming if using it for shared resources (such as databases). Using the select() system call – creates a “file descriptor set ” that allows multiple clients to interact with shared resources (such as a central database). Select also allows a convenient method for server to read data issued from the terminal (eg. /dev/tty).

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd 2

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Parent process Client

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Parent process Client Process Establish connection Client 1 fd 2

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process Establish

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process Establish connection Client 1 fd 2 fd 1 fd 2 Child process fork() called while server is listening (fork returns 0 to distinguish it is child process)

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process Establish

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process Establish connection Client 1 fd 2 fd 1 Data connection fd 2 The child process is created to server the client

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd 1 Client 1 fd 2 fd 1 Data connection fd 2 Parent process waits for new connection

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd 1 Client 2 fd 1 Establish connection fd 1 fd 2 fd 1 Data connection fd 2

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd 1 Client 2 fd 1 Establish connection fd 1 fd 2 fd 1 Data connection fd 2 fd 1 fd 2 fork() called while server is listening (fork returns 0 )

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client 1 fd 1 Client 2 fd 1 Establish connection fd 1 fd 2 fd 1 Data connection fd 2 The child process is created to server the client

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd

Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd 1 Client 1 fd 1 Client 2 fd 1 fd 2 fd 1 Data connection fd 2 Parent process waits for new connection

Multiple Clients – using fork() n n Creating separate processes using fork() Refer to

Multiple Clients – using fork() n n Creating separate processes using fork() Refer to Examples in ~msaul/unx 511/sockets (in Sigma account) child_process_server 1. c child_process_client 1 a. c child_process_client 1 b. c child_process_client 1 c. c Only server program uses fork() system call Each of these client programs pass up a different character (a, b, & C). Run by running each process in background

Multiple Clients – using fork() n n Creating separate processes using fork() Refer to

Multiple Clients – using fork() n n Creating separate processes using fork() Refer to Examples in ~msaul/unx 511/sockets (in Sigma account) This example will child_process_server 2. c child_process_client 2. c work for multiple clients, but not considered an easy method to share resources like databases or shutdown server other than using CTRL C

Multiple Clients n Creating File Descriptor Sets n n n So far, you have

Multiple Clients n Creating File Descriptor Sets n n n So far, you have seen that file descriptors are used in sockets to transfer data between the server and client application. It may be required for the server program to handle multiple clients or to accept stdin from the terminal itself. In this case, the file descriptor is zero (refer to textbook for example) An efficient method to accomplish this task is to work with multiple file descriptors using a table-of contents that associates the client or terminal stdin with the file descriptor called a file descriptor set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Establish connection fd 1 fd 2 fd 3 fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Establish connection Data connection fd 1 fd 2 fd 3 fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Client 2 fd 1 Data connection fd 1 fd 2 fd 3 fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Client 2 fd 1 Data connection Establish connection fd 1 fd 2 fd 3 fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Client 2 fd 1 Data connection Establish connection Data connection fd 1 fd 2 fd 3 fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Data connection fd 1 fd 2 Client 2 fd 1 fd 3 Data connection fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Data connection fd 2 Client 2 fd 1 Client 3 fd 1 fd 3 Data connection Establish connection fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Data connection fd 2 Client 2 fd 1 Client 3 fd 1 fd 3 Data connection Establish connection Data connection fd 4 File Descriptor Set

Multiple Clients – using select() n Creating File Descriptor Sets n How it works

Multiple Clients – using select() n Creating File Descriptor Sets n How it works Server Process Client 1 fd 1 Data connection fd 2 Client 2 fd 1 Client 3 fd 1 fd 3 Data connection fd 4 File Descriptor Set

Multiple Clients n Creating File Descriptor Sets n n n The select() system call

Multiple Clients n Creating File Descriptor Sets n n n The select() system call is used to “listen” for activity in the file descriptor set. As a result, select will “block” (stop execution of program) until one or more of the file descriptors has activity. FD_ZERO() is used to initialize the file descriptor set, and FD_SET() is used to add a file descriptor to the file descriptor set and FD_CLR() is used to remove a file descriptor from the file descriptor set. Refer to Examples in ~msaul/unx 511/sockets: terminal. c select_server 1. c, select_client 1. c select_server 2. c, select_client 2. c select_server 3. c, select_client 3. c