{"id":538,"date":"2020-03-27T21:40:57","date_gmt":"2020-03-27T18:40:57","guid":{"rendered":"https:\/\/sinanbir.com\/?p=538"},"modified":"2020-03-27T21:40:57","modified_gmt":"2020-03-27T18:40:57","slug":"java-async-server-socket-example","status":"publish","type":"post","link":"https:\/\/sinanbir.com\/java-async-server-socket-example\/","title":{"rendered":"Asynchronous Server Application Example &#8211; Java"},"content":{"rendered":"<h1>Async Server<\/h1>\n<p>Asynchronous server application is not suspended while it waits for a connection from a client. If we spin up threads for each session, then the server waits for the IO operations which we don&#8217;t want. This example project shows handling tcp sockets\u00a0Asynchronously without running separate threads for each session.<\/p>\n<p>The example async socket server is available on <a href=\"https:\/\/github.com\/deluxetiky\/java-async-socket-server\" target=\"_blank\" rel=\"noopener\">github<\/a>.<\/p>\n<p><!--more--><\/p>\n<h2>Getting Started<\/h2>\n<p><strong>Requirements<\/strong><\/p>\n<ul>\n<li>Java 8<\/li>\n<li>Maven<\/li>\n<\/ul>\n<h3>Project Setup<\/h3>\n<h4>1. Clone Repository<\/h4>\n<pre class=\"lang:default decode:true\" title=\"git clone\">git clone https:\/\/github.com\/deluxetiky\/java-async-socket-server\ncd java-async-socket-server<\/pre>\n<h4>2. Build Project<\/h4>\n<pre class=\"lang:sh decode:true\" title=\"compile\">mvn compile<\/pre>\n<pre class=\"theme:solarized-dark lang:sh decode:true \">[INFO] \n[INFO] ----------------&lt; org.example:java-async-socket-sample &gt;----------------\n[INFO] Building java-async-socket-sample 1.0-SNAPSHOT\n[INFO] --------------------------------[ jar ]---------------------------------\n[INFO] \n[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-async-socket-sample ---\n[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!\n[INFO] Copying 1 resource\n[INFO] \n[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-async-socket-sample ---\n[INFO] Nothing to compile - all classes are up to date\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS<\/pre>\n<h4>3. Spin-Up Server<\/h4>\n<pre class=\"lang:sh decode:true \">mvn exec:java -Dexec.mainClass=\"Server.SocketStandAlone\"<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \">[INFO] \n[INFO] ----------------&lt; org.example:java-async-socket-sample &gt;----------------\n[INFO] Building java-async-socket-sample 1.0-SNAPSHOT\n[INFO] --------------------------------[ jar ]---------------------------------\n[INFO] \n[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ java-async-socket-sample ---\n2020-03-27 21:16:39 INFO  SocketServer:30 - Device socket server started on \/0.0.0.0:5050<\/pre>\n<h4>4. Finish<\/h4>\n<p>We can spin up multiple concurrent sessions with telnet. Our server will be pushing back to same data that we sent.<\/p>\n<pre class=\"lang:sh decode:true \">telnet localhost 5050<\/pre>\n<p>Project <a href=\"https:\/\/github.com\/deluxetiky\/java-async-socket-server\" target=\"_blank\" rel=\"noopener\">repository.<\/a><\/p>\n<p>That was a quick one. So hope I will come back and explain the code in the repository more detailed.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Async Server Asynchronous server application is not suspended while it waits for a connection from a client. If we spin up threads for each session, then the server waits for the IO operations which we don&#8217;t want. This example project shows handling tcp sockets\u00a0Asynchronously without running separate threads for each session. The example async socket [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,28,30],"tags":[26,27,29,31],"class_list":["post-538","post","type-post","status-publish","format-standard","hentry","category-java","category-server","category-socket","tag-async","tag-java","tag-server","tag-socket"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Asynchronous Server Application Example - Java - Sinan Bir Blog<\/title>\n<meta name=\"description\" content=\"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sinanbir.com\/java-async-server-socket-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Asynchronous Server Application Example - Java - Sinan Bir Blog\" \/>\n<meta property=\"og:description\" content=\"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sinanbir.com\/java-async-server-socket-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Sinan Bir Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-27T18:40:57+00:00\" \/>\n<meta name=\"author\" content=\"deluxetiky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"deluxetiky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/sinanbir.com\/java-async-server-socket-example\/\",\"url\":\"https:\/\/sinanbir.com\/java-async-server-socket-example\/\",\"name\":\"Asynchronous Server Application Example - Java - Sinan Bir Blog\",\"isPartOf\":{\"@id\":\"https:\/\/sinanbir.com\/#website\"},\"datePublished\":\"2020-03-27T18:40:57+00:00\",\"dateModified\":\"2020-03-27T18:40:57+00:00\",\"author\":{\"@id\":\"https:\/\/sinanbir.com\/#\/schema\/person\/4483f0886671e960c730a4026f8a4ffd\"},\"description\":\"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.\",\"breadcrumb\":{\"@id\":\"https:\/\/sinanbir.com\/java-async-server-socket-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/sinanbir.com\/java-async-server-socket-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/sinanbir.com\/java-async-server-socket-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/sinanbir.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Asynchronous Server Application Example &#8211; Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/sinanbir.com\/#website\",\"url\":\"https:\/\/sinanbir.com\/\",\"name\":\"Sinan Bir Blog\",\"description\":\"Developer Soul\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/sinanbir.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/sinanbir.com\/#\/schema\/person\/4483f0886671e960c730a4026f8a4ffd\",\"name\":\"deluxetiky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/sinanbir.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5dcdf9c7d1c6e0527e2c354817d4736929d0098b33196b3d604545386949c86?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5dcdf9c7d1c6e0527e2c354817d4736929d0098b33196b3d604545386949c86?s=96&d=mm&r=g\",\"caption\":\"deluxetiky\"},\"sameAs\":[\"https:\/\/sinanbir.com\"],\"url\":\"https:\/\/sinanbir.com\/author\/deluxetiky\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Asynchronous Server Application Example - Java - Sinan Bir Blog","description":"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.","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:\/\/sinanbir.com\/java-async-server-socket-example\/","og_locale":"en_US","og_type":"article","og_title":"Asynchronous Server Application Example - Java - Sinan Bir Blog","og_description":"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.","og_url":"https:\/\/sinanbir.com\/java-async-server-socket-example\/","og_site_name":"Sinan Bir Blog","article_published_time":"2020-03-27T18:40:57+00:00","author":"deluxetiky","twitter_card":"summary_large_image","twitter_misc":{"Written by":"deluxetiky","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sinanbir.com\/java-async-server-socket-example\/","url":"https:\/\/sinanbir.com\/java-async-server-socket-example\/","name":"Asynchronous Server Application Example - Java - Sinan Bir Blog","isPartOf":{"@id":"https:\/\/sinanbir.com\/#website"},"datePublished":"2020-03-27T18:40:57+00:00","dateModified":"2020-03-27T18:40:57+00:00","author":{"@id":"https:\/\/sinanbir.com\/#\/schema\/person\/4483f0886671e960c730a4026f8a4ffd"},"description":"Java async socket server. Project aims the present base idea of handling concurrent sessions without creating threads. Example project.","breadcrumb":{"@id":"https:\/\/sinanbir.com\/java-async-server-socket-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sinanbir.com\/java-async-server-socket-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sinanbir.com\/java-async-server-socket-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sinanbir.com\/"},{"@type":"ListItem","position":2,"name":"Asynchronous Server Application Example &#8211; Java"}]},{"@type":"WebSite","@id":"https:\/\/sinanbir.com\/#website","url":"https:\/\/sinanbir.com\/","name":"Sinan Bir Blog","description":"Developer Soul","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sinanbir.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sinanbir.com\/#\/schema\/person\/4483f0886671e960c730a4026f8a4ffd","name":"deluxetiky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/sinanbir.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5dcdf9c7d1c6e0527e2c354817d4736929d0098b33196b3d604545386949c86?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5dcdf9c7d1c6e0527e2c354817d4736929d0098b33196b3d604545386949c86?s=96&d=mm&r=g","caption":"deluxetiky"},"sameAs":["https:\/\/sinanbir.com"],"url":"https:\/\/sinanbir.com\/author\/deluxetiky\/"}]}},"_links":{"self":[{"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":0,"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/posts\/538\/revisions"}],"wp:attachment":[{"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/media?parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/categories?post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinanbir.com\/wp-json\/wp\/v2\/tags?post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}