1 / 24

Agenda

Learn how to manage multiple clients in socket programming efficiently using fork() system call or select() system call. Discover the benefits and challenges of each method. Explore examples and best practices for creating file descriptor sets.

jwhitehorn
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Multiple Clients • 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: • 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).

  3. Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd1 fd2 Client 1fd1

  4. Multiple Clients – using fork() Creating Multiple Child Processes Server Process Parent process Client Process Establish connection fd1 fd2 Client 1fd1

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

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

  7. Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd1 fd2 Parent process waits for new connection Client 1fd1 fd1 fd2 Data connection

  8. Multiple Clients – using fork() Creating Multiple Child Processes Server Process Client Process fd1 fd2 Establish connection Client 1fd1 fd1 fd2 Client 2fd1 Data connection

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

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

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

  12. Multiple Clients – using fork() • Creating separate processes using fork() • Refer to Examples in ~msaul/unx511/sockets(in Sigma account)child_process_server1.cchild_process_client1a.cchild_process_client1b.cchild_process_client1c.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

  13. Multiple Clients – using fork() • Creating separate processes using fork() • Refer to Examples in ~msaul/unx511/sockets(in Sigma account)child_process_server2.cchild_process_client2.c This example willwork for multipleclients, but notconsidered an easy method to share resources like databases or shutdown server other than using CTRL C

  14. Multiple Clients • Creating File Descriptor Sets • 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

  15. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process Establish connection File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4

  16. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process Establish connection File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection

  17. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1

  18. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Establish connection

  19. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Establish connection Data connection

  20. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Data connection

  21. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Data connection Client 3fd1 Establish connection

  22. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Data connection Client 3fd1 Establish connection Data connection

  23. Multiple Clients – using select() • Creating File Descriptor Sets • How it works Server Process Client Process File Descriptor Set Client 1fd1 fd1 fd2 fd3 fd4 Data connection Client 2fd1 Data connection Client 3fd1 Data connection

  24. Multiple Clients • Creating File Descriptor Sets • 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/unx511/sockets:terminal.c select_server1.c, select_client1.c select_server2.c, select_client2.c select_server3.c, select_client3.c

More Related