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’t want. This example project shows handling tcp sockets Asynchronously without running separate threads for each session.

The example async socket server is available on github.

Getting Started

Requirements

  • Java 8
  • Maven

Project Setup

1. Clone Repository

git clone https://github.com/deluxetiky/java-async-socket-server
cd java-async-socket-server

2. Build Project

mvn compile
[INFO] 
[INFO] ----------------< org.example:java-async-socket-sample >----------------
[INFO] Building java-async-socket-sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-async-socket-sample ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-async-socket-sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

3. Spin-Up Server

mvn exec:java -Dexec.mainClass="Server.SocketStandAlone"
[INFO] 
[INFO] ----------------< org.example:java-async-socket-sample >----------------
[INFO] Building java-async-socket-sample 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ java-async-socket-sample ---
2020-03-27 21:16:39 INFO  SocketServer:30 - Device socket server started on /0.0.0.0:5050

4. Finish

We can spin up multiple concurrent sessions with telnet. Our server will be pushing back to same data that we sent.

telnet localhost 5050

Project repository.

That was a quick one. So hope I will come back and explain the code in the repository more detailed.