Fully Duplex Multi Client Socket Communication

I’ve written a small, complete and reliable socket communication library. I wanted to avoid using the nio package and use the default ServerSocket and Socket classes provided in the java.net package.

Fully Duplex; Async
This means the server and client can asynchronously send messages to each other whenever they’d like. This is different than most libraries where the server is only listening for messages and responds when a client requests for some information.

Multi-Client
The server can host as many clients as the internal sockets implementation allows. Java’s ServerSocket implementation sets the size to 50 connections.
The server can also restrict the number of simultaneous clients it’d like to host at once.
Any client can send a message to any other client.

Server-Client symmetric API
Effectively, it really doesn’t matter who the server is; each device in the network gets a callback when a device connects or disconnects, and each client is assigned IDs by the server.
The server has some extra abilities in that it can kick out certain devices.

Several Barts from different dimensions talking to the angry Moe. The Simpsons was always very sci-fi-y.

The reason I wrote this library
It’s surprising I had to write this as socket communication is the basis of any multi-device system; I assumed there’d be an open source socket communication library written in Java with features I’ve written above. But apart from the ones provided by nio there aren’t any. What’s worse is any tutorial or blogpost on sockets are limited to echo servers. Who uses echo servers?

How this works
It works on the basic principle that every socket connection is uniquely identified by IP address and port of the server and the client. This means you can you have multiple clients connected to the same port.

Client Socket:
  • initialize(String hostIp, String port); initialize with the host’s ip address and a port number. 
  • If the number of hosts exceeds than the number specified by the host, you get a callback with host busy.
  • The client socket then initializes two threads running in parallel; one waiting to read messages, and one asynchronously writing out messages to the server.
Server Socket:
  • Initializes a ServerSocket at a specified port.
  • For every new client connected, the server creates two new thread just as the client socket above; one waiting to read messages, and the other to send messages asynchronously.
  • The server socket then goes back to listening for more clients.
Improvements
On the server, there are 2 new threads created for every new client; as long as there’s no communication, both the reader and writer threads are asleep. The nio packages use multiplexing to serve multiple clients from the same package.
I haven’t yet gone into how this works. This looks like a great place to start.

The implementation is actually fairly straightforward, and we’ve been using it for over a year at Tonbo Imaging.

Comments

Popular posts from this blog

Finding My Parter

Two Worlds, Two Lives

Keeping Fit in India