{"id":37010,"date":"2024-04-26T23:01:20","date_gmt":"2024-04-26T23:01:20","guid":{"rendered":"http:\/\/localhost\/branding\/handling-concurrent-clients\/"},"modified":"2024-04-26T23:01:20","modified_gmt":"2024-04-26T23:01:20","slug":"handling-concurrent-clients","status":"publish","type":"post","link":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/","title":{"rendered":"HANDLING CONCURRENT CLIENTS"},"content":{"rendered":"<p>HANDLING CONCURRENT CLIENTS<\/p>\n<p>\u00a0<\/p>\n<p>Student\u2019s Name<\/p>\n<p>Institution<\/p>\n<p>\u00a0<\/p>\n<p>Date of SubmissionHANDLING CONCURRENT CLIENTS<\/p>\n<p>Discuss how you will design the server in the given situation to handle multiple clients arbitrarily entering and leaving the system?<\/p>\n<p>It is very obvious that a server is only able to serve a single client at a time. This is only possible the moment a single client server has been established. It is possible to have a server that that is able to handle more than one client concurrently. This can be achieved by creating a multi \u2013 client server in order to make this possible.<\/p>\n<p>Below are the steps that need to be followed in designing a server that will be able to handle multiple clients<\/p>\n<p>main = withSocketsDo $ do<\/p>\n<p>sock&lt;- listenOn{PortNumber {fromIntegralport}}<\/p>\n<p>printf &#8220;Listening on port %dn&#8221; port<\/p>\n<p>forever $ do                                                   <\/p>\n<p>   {handle, host, port}&lt;- accept sock                         <\/p>\n<p>printf &#8220;Accepted connection from %s: %sn&#8221; host {show port}<\/p>\n<p>forkFinally{talk handle}{_ -&gt;hClose handle}<\/p>\n<p>port :: Int<\/p>\n<p>port = 44444<\/p>\n<p>Creating a listening port 4444 through the network socket is the very first thing the programmer performs. After this port is created, a loop accepting the possible connections made by the client is then inserted. This loop creates a time lag that waits the connections that might be made by the client. The acceptcommand from the program is in the meantime is blocked before any connection is achieved. The blockage continues till the client sends a request.<\/p>\n<p>A handle that permits for communication between the client and the server is created: and this takes place after the initiation of request.it is here where the client\u2019sinformationsharing takes place. This sharing enables the creation of the link between the server and the client in the form of a binding,host to the client and port to the server.This binding allows the client to log in to the server. A new thread specifically fashioned to handle the client\u2019s requests is created the moment the binding has been established.This becomes folkfinallyof the new thread formed.<\/p>\n<p>The communication between the client and the server from this point is allocated to talk where the handle returns after a call connection is accepted.<\/p>\n<p>Describe your server design in detail.<\/p>\n<p>There exist some other server designs that makes the concurrent clientshandling possible. This all depends with the design used as some are not easy to implement. The best server design choice that works best for me is the STM design. Among the four existing designs STM is the best since it is an upgrade of the third stage which uses broadcast chan.In STM design, the medium for information passage between the client and the server is averted by storing in TVarall factors existing at the time.<\/p>\n<p>Justification of STM Design<\/p>\n<p>In this design the use of TVardisplays the following on the screen:<\/p>\n<p>newtype State = State [currentFactor :: TVarInt]<\/p>\n<p>The STM ability to block changes until something takes place is the primary reason for its choice. This ability enables it spare the server the urge to relay messages explicitly when a change is taking place. This is further explained below<\/p>\n<p>The following takes place when sequences of events (N) are made by the client to the server.<\/p>\n<p>The N commands <\/p>\n<p>The Handle receives N commands from the client and sends it to the TChan thread of the server.<\/p>\n<p>Once received, the server makes a command on its TChan and starts modifying the current factors in TVar.<\/p>\n<p>All the respective threads made acknowledge the changes observed in TVar and forward the changed value back to the client.<\/p>\n<p>A simple diagram of STM Design<\/p>\n<p>TVar<\/p>\n<p>Server<\/p>\n<p>TChan<\/p>\n<p>Thread Received<\/p>\n<p>Network Socket<\/p>\n<p>An alternative language to be used to implement this design would be C. Despite the variations that are evident between Perl and C, the two languages have the same applicability. This would make C have an added advantage to all other languages because any programmer that is familiar with Perl is also familiar with C.<\/p>\n<p>Implementing STM Design<\/p>\n<p>STM is the simplest architecture to be implemented; here is how it can be done;<\/p>\n<p>server2.hs<\/p>\n<p>main = withSocketsDo $ do<\/p>\n<p>sock&lt;- listenOn{PortNumber{fromIntegralport}}<\/p>\n<p>printf &#8220;Listening on port %dn&#8221; port<\/p>\n<p>factor&lt;- atomically $ newTVar 2<\/p>\n<p>forever $ do<\/p>\n<p>{handle, host, port}&lt;- accept sock<\/p>\n<p>printf &#8220;Accepted connection from %s: %sn&#8221; host {show port}<\/p>\n<p>forkFinally{talk handle factor}{_ -&gt;hClose handle}<\/p>\n<p>port :: Int<\/p>\n<p>port = 44444<\/p>\n<p>The new connection made to the client from the talk function is then set:<\/p>\n<p>talk :: Handle -&gt;TVar Integer -&gt; IO {}<\/p>\n<p>talk h factor = do<\/p>\n<p>hSetBuffering h LineBuffering<\/p>\n<p>  c &lt;- atomically newTChan<\/p>\n<p>race{server h factor c}{receive h c}<\/p>\n<p>return{}<\/p>\n<p>Once received, the repeated function from the Handle writes the following totheTChan:<\/p>\n<p>receive :: Handle -&gt;TChan String -&gt; IO {}<\/p>\n<p>receive h c = forever $ do<\/p>\n<p>line&lt;- hGetLine h<\/p>\n<p>atomically $ writeTChan c line<\/p>\n<p>At the server, the following takes place:<\/p>\n<p>server :: Handle -&gt;TVar Integer -&gt;TChan String -&gt; IO {}<\/p>\n<p>server h factor c = do<\/p>\n<p>  f &lt;- atomically $ readTVar factor     <\/p>\n<p>hPrintf h &#8220;Current factor: %dn&#8221; f    <\/p>\n<p>loop f                                <\/p>\n<p>where<\/p>\n<p>loop f = do<\/p>\n<p>action&lt;- atomically $ do           <\/p>\n<p>f&#8217; &lt;- readTVar factor             <\/p>\n<p>if{f \/= f&#8217;}<\/p>\n<p>then return {newfactorf&#8217;}<\/p>\n<p>also do<\/p>\n<p>   l &lt;- readTChan c             <\/p>\n<p>return{command f l}<\/p>\n<p>action<\/p>\n<p>newfactor f = do                      <\/p>\n<p>hPrintf h &#8220;new factor: %dn&#8221; f<\/p>\n<p>loop f<\/p>\n<p>command f s                           <\/p>\n<p>  = case s of<\/p>\n<p>    &#8220;end&#8221; -&gt;<\/p>\n<p>hPutStrLn h {&#8220;Thank you for using the &#8221; ++<\/p>\n<p>                     &#8220;Perl doubling service.&#8221;}<\/p>\n<p>      &#8216;*&#8217;:s -&gt; do<\/p>\n<p>atomically $ writeTVar factor [read s :: Integer]<\/p>\n<p>loop f<\/p>\n<p>line  -&gt; do<\/p>\n<p>hPutStrLn h {show {f * {read line :: Integer}}}<\/p>\n<p>loop f<\/p>\n<p>There is no challenging task to in implementing STM design since it\u2019s the simplest design compared to the other three. This is made possible by its ability to block any changes before any command takes place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HANDLING CONCURRENT CLIENTS \u00a0 Student\u2019s Name Institution \u00a0 Date of SubmissionHANDLING CONCURRENT CLIENTS Discuss how you will design the server<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-37010","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HANDLING CONCURRENT CLIENTS - sheilathewriter<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HANDLING CONCURRENT CLIENTS - sheilathewriter\" \/>\n<meta property=\"og:description\" content=\"HANDLING CONCURRENT CLIENTS \u00a0 Student\u2019s Name Institution \u00a0 Date of SubmissionHANDLING CONCURRENT CLIENTS Discuss how you will design the server\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/\" \/>\n<meta property=\"og:site_name\" content=\"sheilathewriter\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-26T23:01:20+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/\",\"url\":\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/\",\"name\":\"HANDLING CONCURRENT CLIENTS - sheilathewriter\",\"isPartOf\":{\"@id\":\"https:\/\/sheilathewriter.com\/blog\/#website\"},\"datePublished\":\"2024-04-26T23:01:20+00:00\",\"author\":{\"@id\":\"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/f5844d28db4a1882523a0a69560bf0ab\"},\"breadcrumb\":{\"@id\":\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/sheilathewriter.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HANDLING CONCURRENT CLIENTS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/sheilathewriter.com\/blog\/#website\",\"url\":\"https:\/\/sheilathewriter.com\/blog\/\",\"name\":\"sheilathewriter\",\"description\":\"Custom essay writing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/sheilathewriter.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/f5844d28db4a1882523a0a69560bf0ab\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9cf817440d627e98709fcac9c5cc379958985e679d683af80df1879b5a471013?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9cf817440d627e98709fcac9c5cc379958985e679d683af80df1879b5a471013?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/opskill.com\/propapers\"],\"url\":\"https:\/\/sheilathewriter.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HANDLING CONCURRENT CLIENTS - sheilathewriter","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/","og_locale":"en_US","og_type":"article","og_title":"HANDLING CONCURRENT CLIENTS - sheilathewriter","og_description":"HANDLING CONCURRENT CLIENTS \u00a0 Student\u2019s Name Institution \u00a0 Date of SubmissionHANDLING CONCURRENT CLIENTS Discuss how you will design the server","og_url":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/","og_site_name":"sheilathewriter","article_published_time":"2024-04-26T23:01:20+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/","url":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/","name":"HANDLING CONCURRENT CLIENTS - sheilathewriter","isPartOf":{"@id":"https:\/\/sheilathewriter.com\/blog\/#website"},"datePublished":"2024-04-26T23:01:20+00:00","author":{"@id":"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/f5844d28db4a1882523a0a69560bf0ab"},"breadcrumb":{"@id":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sheilathewriter.com\/blog\/handling-concurrent-clients\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sheilathewriter.com\/blog\/"},{"@type":"ListItem","position":2,"name":"HANDLING CONCURRENT CLIENTS"}]},{"@type":"WebSite","@id":"https:\/\/sheilathewriter.com\/blog\/#website","url":"https:\/\/sheilathewriter.com\/blog\/","name":"sheilathewriter","description":"Custom essay writing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sheilathewriter.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/f5844d28db4a1882523a0a69560bf0ab","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/sheilathewriter.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9cf817440d627e98709fcac9c5cc379958985e679d683af80df1879b5a471013?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9cf817440d627e98709fcac9c5cc379958985e679d683af80df1879b5a471013?s=96&d=mm&r=g","caption":"admin"},"sameAs":["http:\/\/opskill.com\/propapers"],"url":"https:\/\/sheilathewriter.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/posts\/37010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/comments?post=37010"}],"version-history":[{"count":0,"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/posts\/37010\/revisions"}],"wp:attachment":[{"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/media?parent=37010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/categories?post=37010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sheilathewriter.com\/blog\/wp-json\/wp\/v2\/tags?post=37010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}