{"id":23902796,"url":"https://github.com/mtumilowicz/java12-io-socket-client-server-workshop","last_synced_at":"2025-10-06T14:54:58.735Z","repository":{"id":110877730,"uuid":"194346957","full_name":"mtumilowicz/java12-io-socket-client-server-workshop","owner":"mtumilowicz","description":"Blocking implementations of server using java.io and java.net: single thread, thread per connection, thread pool.","archived":false,"fork":false,"pushed_at":"2024-05-27T07:38:37.000Z","size":155,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-10T04:01:44.590Z","etag":null,"topics":["blocking","blocking-sockets","blockingsockets","java-io","java-net","server","serversocket","socket","thread-per-connection","thread-pool","workshop","workshop-materials","workshops"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtumilowicz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-06-29T00:19:17.000Z","updated_at":"2024-05-27T07:38:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"c41e08e3-a31b-4357-ac32-01d931970894","html_url":"https://github.com/mtumilowicz/java12-io-socket-client-server-workshop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mtumilowicz/java12-io-socket-client-server-workshop","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava12-io-socket-client-server-workshop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava12-io-socket-client-server-workshop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava12-io-socket-client-server-workshop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava12-io-socket-client-server-workshop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtumilowicz","download_url":"https://codeload.github.com/mtumilowicz/java12-io-socket-client-server-workshop/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava12-io-socket-client-server-workshop/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273747788,"owners_count":25160652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blocking","blocking-sockets","blockingsockets","java-io","java-net","server","serversocket","socket","thread-per-connection","thread-pool","workshop","workshop-materials","workshops"],"created_at":"2025-01-04T22:50:35.679Z","updated_at":"2025-10-06T14:54:53.714Z","avatar_url":"https://github.com/mtumilowicz.png","language":"Java","readme":"[![Build Status](https://app.travis-ci.com/mtumilowicz/java12-io-socket-client-server-workshop.svg?branch=master)](https://travis-ci.com/mtumilowicz/java12-io-socket-client-server-workshop)\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n\n# java12-io-socket-client-server-workshop\n\n_Reference_: https://www.amazon.com/Reactive-Programming-RxJava-Asynchronous-Applications/dp/1491931655  \n_Reference_: https://www.amazon.com/Netty-Action-Norman-Maurer/dp/1617291471  \n_Reference_: https://docs.oracle.com/javase/tutorial/networking/sockets/index.html  \n_Reference_: https://stackoverflow.com/questions/28480575/whats-the-difference-between-websocket-and-plain-socket-communication  \n_Reference_: https://www.javaworld.com/article/2077995/java-concurrency-asynchronous-processing-support-in-servlet-3-0.html  \n_Reference_: https://docs.oracle.com/javaee/7/tutorial/servlets012.htm  \n_Reference_: https://www.hackerearth.com/practice/notes/asynchronous-servlets-in-java/  \n_Reference_: https://developerlife.com/2011/04/13/creating-asynchronous-servlets-with-tomcat-7-servlet-3-0-api/\n\n# project description\n* the main goal of this project is to show how to implement blocking server using `java.io` and `java.net`:\n    * single threaded server\n    * thread per connection server\n    * thread pool server\n* in the workshop we will try to fix failing tests from `test/workshop` package by following steps and hints in\n`java/workshop` package\n* answers: `java/answers` package\n\n# theory in a nutshell\n* `java.io`, `java.net` can be used for implementing low-level network communication, for example: client-server \napplication\n* it uses TCP (communication that occurs between the client and the server must be reliable - no data can be \ndropped and it must arrive on the client side in the same order in which the server sent it)\n* client and server bind a socket to its end of the connection\n* to communicate, the client and the server each reads from and writes to the socket bound to the connection\n* server just waits, listening to the socket for a client to make a connection request\n* webSockets and regular sockets are not the same thing\n    * a webSocket runs over a regular socket\n    * webSocket connections start with an HTTP request from client to server\n        * it is critically important because if you can reach the web server for normal web communication, \n        then you can reach it for a webSocket request without any networking infrastructure anywhere between \n        client and server having to open new holes in the firewall or open new ports or anything like that\n    \n## concerning threads\n* on 64 bit JVM 1.8 thread consumes 1,024 KB of RAM by default (-Xss flag)\n    * 1000 concurrent connections mean 1,000 threads and about 1 GB of stack space\n    * stack space is independent from heap space - application will consume far more than this a gigabyte\n    * threads are relatively expensive in terms of memory use\n* a thread pool has many advantages over simply creating threads on demand\n    * thread is already initialized and started, therefore you do not have to wait or\n    warm up, reducing client latency\n    * we put a limit on the total number of threads running in our system so\n    that we can safely reject connections under peak load rather than crashing\n    * a thread pool has a configurable queue that can temporarily hold short peaks of\n    load\n    * if both the pool and queue are saturated, there is also a configurable rejection\n    policy (error, running in client thread instead, etc.)\n* servlet 3.0 specification made it possible to write asynchronous servlets\n    * decouples the processing request from the container thread\n    * most of the processing has nothing to do with the servlet request or response\n    * whenever the application wants to send the response, it can do it from any\n    thread at any point in time\n    * the thread that picked up the request might be already gone or it might be \n    handling some other request\n    * just shift the problem of thread explosion into a different place\n    * application could begin responding slowly due to frequent garbage collection cycles and\n    context switching\n* there are two common scenarios in which a thread associated with a request can be sitting idle\n    * the thread needs to wait for a resource to become available or process data before building the \n    response, for example an application may need to query a database or access data from a remote web \n    service before generating the response\n    * the thread needs to wait for an event before generating the response, for example an application \n    may have to wait for a JMS message, new information from another client, or new data available in a \n    queue before generating the response\n    * asynchronous processing refers to assigning these blocking operations to a new thread and returning \n    the thread associated with the request immediately to the container\n    \n## http 1.1 and persistent connections\n* in HTTP 1.0, a connection between a Web client and server is closed after a single request/response cycle\n* in HTTP 1.1, a connection is kept alive and reused for multiple requests\n* persistent connections reduce communication lag perceptibly, because the client doesn't need to\n renegotiate the TCP connection after each request\n* thread per HTTP connection, which is based on HTTP 1.1's persistent connections, is a common solution vendors \nhave adopted\n    * each HTTP connection between client and server is associated with one thread on the server side\n    * threads are allocated from a server-managed thread pool and once a connection is closed, the dedicated thread is \n    recycled back to the pool and is ready to serve other tasks\n\n# conclusions in a nutshell\n* `Socket` - client side of the connection\n* `ServerSocket` - server side of the connection\n* closing `Socket` will close its streams\n    ```\n    * \u003cp\u003e Closing this socket will also close the socket's\n    * {@link java.io.InputStream InputStream} and\n    * {@link java.io.OutputStream OutputStream}.\n    public synchronized void close() throws IOException\n    ```\n* `ServerSocket` is a `java.net` class that provides a system-independent implementation of the server side of a \nclient/server socket connection\n    * useful constructors\n        ```\n        public ServerSocket(int port) throws IOException {\n            this(port, 50, null);\n        }\n        ```\n        ```\n        public ServerSocket(int port, int backlog) throws IOException {\n            this(port, backlog, null);\n        }\n        ```\n        * backlog (the default is 50) requested maximum length of the queue of incoming connections\n            * note that HTTP/1.1 uses persistent connections by default and until the client\n          disconnects we keep the TCP/IP connection open - blocking any new clients\n    * `serverSocket.accept()`\n      * waits until a client starts up and requests a connection on the host and port of this server\n      * returns a new `Socket` which is bound to the same local port and has its remote address \n      and remote port set to that of the client when a connection is successfully established \n* `Step9_ThreadPoolServerAnswer`\n    * tomcat: The standard executor internally uses a `java.util.concurrent.ThreadPoolExecutor`\n        * http://tomcat.apache.org/tomcat-9.0-doc/config/executor.html\n* this is pretty much how servlet containers like Tomcat work, managing 200 threads in a pool\n  by default\n  * note that Tomcat has the so-called NIO connector that handles some of the operations\n    on sockets asynchronously, but the real work in servlets and frameworks built\n    on top of them is still blocking\n  * this means that traditional applications are inherently\n    limited to a couple thousand connections, even built on top of modern servlet\n    containers\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava12-io-socket-client-server-workshop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtumilowicz%2Fjava12-io-socket-client-server-workshop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava12-io-socket-client-server-workshop/lists"}