+ All Categories
Home > Documents > Chat Room (Client and Server Model) - 國立中興大學 · 2002. 1. 22. · Client Tom Server Chat...

Chat Room (Client and Server Model) - 國立中興大學 · 2002. 1. 22. · Client Tom Server Chat...

Date post: 26-Feb-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
24
Chat Room (Client and Server Model) Homework 2
Transcript
  • Chat Room (Client and Server Model)

    Homework 2

  • OutlineRelated knowledgeRequired FunctionFunction and input/outputDemoReferences

  • Related knowledgeclient-server model

    Detail: Server Algorithms

    http://www.eli.sdsu.edu/courses/spring95/cs596_3/notes/servertypes/lect3.html

  • Concurrent Connection-Oriented Model

    Concurrent Servers (Single-Process) (80 points)

    Concurrent Servers (Master and Slave) (20 points)

    Concurrent Servers (Master and Pre-allocated Slave) (option 加學期總成績五分以上)

    Concurrent Servers (Thread) (option)

  • Concurrent Servers (Single-Process)(1/5)

    Server端Socket Listen Port負責Listen處理所有client連線建立要求(綠1),當Server accept連線時(綠2),在mapping到新的socket(綠3)負責處理與client通訊[Single-Process中利用select()I/O多工指令來得知各個sockets是否有訊息需要處理]

  • Concurrent Servers (Single-Process)(2/5)

    select()synchronous I/O multiplexing

    SYNOPSISint select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

    Macro utilities: FD_ZERO(&set); /* empties the set */FD_SET(fd,&set); /* adds FD to the set */FD_CLR(fd,&set); /* removes FD from the set */ FD_ISSET(fd,&set) /* true if FD is in the set */

  • Concurrent Servers (Single-Process)(3/5)

    PARAMETERSnumfds: the number of file descriptors to watch. readfds: the file descriptor to watch for readability. writefds: the file descriptors to watch for writabilityexceptfds: the file descriptor to watch exceptions. timeout: the timeout value

    DESCRIPTIONMakes the calling task sleep until some conditions on the file descriptors become true or until a timeout value expires. There are three conditions for which files may be tested:

    with readfds: Can the file be read? with writefds: Can the file be written? with exceptfds: Is there any exception pending on the file?

    Whenever one of the conditions on one the specified files is true, the call returns.

  • Concurrent Servers (Single-Process)(4/5)

  • Concurrent Servers (Single-Process)(5/5)

    Concurrent Servers (Single-Process)流程

    1. 建立socket並bind socket port,將socket fd加至fd_set名單中。2. 使用select()來等待fd_set名單中sockets的I/O動作。3. 若fd_set名單中listen port connection的socket為ready,用

    accept來接收新的連線, mapping到新的socket,並將socket fd加至fd_set名單中名單中。

    4. 若fd_set名單中其他sockets為ready,用read()來讀取sockets的訊息,並做相對的處理,在用write()將訊息透過socket傳至clients。

    5. 回到步驟2繼續運作。

  • Concurrent Servers (Master and Slave)(1/3)

    Master process負責接受連結(綠1),並Check client是否要建立新的聊天室(綠2),假設如果要建立新的聊天室(綠3),master process就fork() 出新的(slave) processes,每一個slave process負責一個聊天室,並將accept後的socket加入新的討論區fd_set名單中(綠4),用select指令來偵測聊天室內各個sockets的要求

    ClientTom

    Server Chat Room Master and Slave Model

    Listen port

    ClientChen Client

    Mary

    1

    2

    4 Connection Connection

    是否要建立chat room

    S

    Master process

    S S

    3

    加入chat room

    Slave (child) process

    程式討論區K書區

    S=Socket

    fd_set

  • Concurrent Servers (Master and Slave)(2/3)

  • Concurrent Servers (Master and Slave)(3/3)

    Concurrent Servers (Master and Slave)的流程

    Master 1. 建立socket、bind socket port,並listen portMaster 2. 如有聽到client要建立連線,就accept並接收client request,並看此client是要加入聊天室或是要產生新的聊天室[fork() (slave) process處理]。

    Slave 1. 在被建立時接收連結訊息。Slave 2. 用select來等待fd_set名單中sockets的I/O動作,並利用read 和write來收、送訊息。Slave 3. 於所有client connections中斷時,slave process就destroy。

  • Concurrent Servers (Master and Pre-allocated Slave)(1/3)

    在一些大型BBS系統中,會事先fork出一些slave processes放在pool中,開新聊天室時直接讓先前fork好的slave process去負責,加快建立速度,關閉聊天室時再將之放回pool中,圖中1、2、5步驟與Master and Slave一樣,當要建立聊天室時,先去pool找(綠3)是否還有process可用,如果有就不fork(),直接從pool中拿出來用(綠4)

  • Concurrent Servers (Master and Pre-allocated Slave)(2/3)

  • Concurrent Servers (Master and Pre-allocated Slave)(3/3)

  • Concurrent Servers (Thread)fork()動作:

    執行與parent一樣的程式碼,並複製parent process記憶體內容和所有descriptors

    問題:parent和child process必須靠interprocess communication來傳遞資訊的問題

    Thread動作:

    執行與parent一樣的程式碼,但parent和child process共享相同記憶體的特質,可大幅減短產生child process的時間

    問題:共享變數和descriptors,所以會有data synchronization的問題master & slave processes裡使用的函式在threads應用程式中不一定為thread-safe

    解決方式:用thread-specific data的技巧pthread_setspecific() to set a value to thread-specific datapthread_getspecific() to retrieve the current value

  • Required Function(1/2)

    Note:實作時畫面與指令名稱不需與投影片一樣,但功能都要有,同時如果有興趣的同學功能還可以外加,分數也會外加。

    當使用者連上server後,必須提供以下功能:1.查詢現有聊天室及主題、各聊天室成員2.使用者可選加入現有聊天室,或者開新聊天室,並決定為開放式或封閉式,當使用者欲加入封閉式聊天室時,系統需送訊息給開此聊天室的室長,由室長決定是否允許新成員加入。

    3.可執行指令包括:/Room => 顯示目前所有聊天室及主題/Command => 顯示所有能夠執行的指令及其用法/Member => 顯示目前所有聊天室及成員/Join + Room_Id => 加入指定聊天室/Create +`-o' /`-c'+ Topic => 開新的聊天室,並指 定

    聊天室為開放或封閉與主題

  • Required Function(2/2)

    當使用者正式進入聊天室之後,必須能執行以下的指令:

    Help => 顯示聊天室內所有指令及其用法Who => 列出聊天室裡的所有使用者及其上線位址Broadcast=> 將使用者說的訊息告知所有使用者Talkto => 將使用者說的訊息告知指定的使用者Bye => 離開聊天室其中封閉式聊天室的室長另外可執行兩個指令:

    Admit + User_Id => server詢問,室長同意使用者加入時用Reject + User_Id => server詢問,室長拒絕使用者加入時用

  • Function and input/output(1/4)root # chat_client 192.168.15.127 7777login: chen進入Server後畫面如下:

    歡迎光臨XXXXXServer

    以下指令供您操作

    ***********************************************************

    /Room => 顯示目前所有聊天室及主題

    /Command =>顯示所有能夠執行的指令及其用法

    /Member => 顯示目前所有聊天室及成員

    /Join + Room_Id =>加入指定聊天室

    / Create +`-o' /`-c'+ Topic => 開新的聊天室,並指定聊天室為開放或封閉與主題

  • Function and input/output(2/4)進入聊天室後畫面如下:

    *************************************************/Help => 顯示聊天室內所有指令及其用法

    /Who => 列出聊天室裡的所有使用者及其上線位址

    /Broadcast => 將使用者說的訊息告知所有使用者

    /Talkto => 將使用者說的訊息告知指定的使用者

    /Bye => 離開聊天室

  • Function and input/output(3/4)

    >/Room

    1.程式討論 open

    2.攝影討論 open

    3.美食討論 close

    >/Join 2

    室長Chen 歡迎您的加入!

    > /who

    1.Chen 192.168.15.1

  • Function and input/output(4/4)>怎樣才能拍出淺景深?Chen:怎樣才能拍出淺景深?Tom:光圈、拍攝物距離、焦長都有影響Mary:我懂了謝謝,再見!>再見Chen:再見Tom :再見Mary離開[攝影討論]了

  • DemoDealline:2006-6-27並交source code

    格式:Hw2_學號.tar.gz

  • ReferencesLinux C/C++ 網路程式設計

    Linux Programming by Example

    Linux 系統程式實例設計

    Unix Network Programming

    Internetworking with TCP/IP Volume III

    Thread-Specific Data and Signal Handling in Multi-Threaded Applications

    http://www.books.com.tw/exep/prod/booksfile.php?item=0010218690http://tlsj.tenlong.com.tw/WebModule/BookSearch/bookSearchViewAction.do?isbn=0131429647&sid=21420http://tlsj.tenlong.com.tw/WebModule/BookSearch/bookSearchViewAction.do?isbn=9574422801&sid=27523http://tlsj.tenlong.com.tw/WebModule/BookSearch/bookSearchViewAction.do?isbn=0131230522&sid=20140http://tlsj.tenlong.com.tw/WebModule/BookSearch/bookSearchViewAction.do?isbn=013260969X&sid=388http://www.linuxjournal.com/article/2121http://www.linuxjournal.com/article/2121

    Chat Room (Client and Server Model)OutlineRelated knowledgeConcurrent Connection-Oriented ModelConcurrent Servers (Single-Process)(1/5) Concurrent Servers (Single-Process)(2/5)Concurrent Servers (Single-Process)(3/5)Concurrent Servers (Single-Process)(4/5)Concurrent Servers (Single-Process)(5/5)Concurrent Servers (Master and Slave)(1/3) Concurrent Servers (Master and Slave)(2/3)Concurrent Servers (Master and Slave)(3/3)Concurrent Servers (Master and Pre-allocated Slave)(1/3)Concurrent Servers (Master and Pre-allocated Slave)(2/3)Concurrent Servers (Master and Pre-allocated Slave)(3/3)Concurrent Servers (Thread)Required Function(1/2)Required Function(2/2)Function and input/output(1/4)Function and input/output(2/4)Function and input/output(3/4)Function and input/output(4/4)DemoReferences


Recommended