Wednesday, August 8, 2012

String-based Socket communication classes

I recently removed an old post called "A little framework for TCP client-server communication".

That's why I think it was not so clear and useful.
Today I propose the "engine" I used.

I used those libs for some projects like:
  • A push server for Android clients
  • A server that controls some computer's feature like volume, services, tasks, also file exchange
  • A computer's mouse controller
  • Data exchange between two different processes

I think it's very useful, scalar and powerful for simple projects.

It's basically composed by two class and two interfaces.

class Server: wraps a ServerSocket object and manages incoming client connections, and messages through an interface called OnServerEventsListener.
interface OnServerEventsListener: defines some standard events like, adding a client, receiving messages, receiving errors of Server or from Clients.
class Client: wraps a Socket object and manages a simple communication through an interface called OnClientEventsListener.
interface OnClientEventsListener: defines events like connecting, disconnecting, on receiving message, on exception generated.
I remember that messages are all Strings, reading criteria is based by end of the line (readline). You can download source and modify it as your necessity.

I also remember that those 2 classes are not linked.

Let's have a look to Server class usage:
It's a thread. It listens to incoming client connections and provide a very simple access to the connected clients list.Events are passed by interface.
//creates an object prepared to listen to port 8888
Server server = new Server(8888);
//listen to its main events
server.setServerEventsListener(new OnServerEventsListener() {

 public void onServerExceptionReceived(Exception ex) {
  System.out.println("server exception"+ex.getMessage());

 public void onNewClientReceived(Client client) {
  System.out.println("hello "+client.getInetAddress().toString());

 public void onMessageReceived(Client client, String message) {
  System.out.println("message from: "+client.getInetAddress().toString());
  System.out.println("message body: "+message);


 public void onClientExceptionReceived(Client client, Exception ex) {
  System.out.println("client exception"+ ex.getMessage());


 public void onClientDisconnected(Client client) {
  System.out.println("bye bye "+client.getInetAddress().toString());
//thread starts, socket is going to open

Very simple, isn't? Now let's have a look to Client class usage:
//creates and prepares the client
Client client= new Client("", 8888);
//listens to events
client.setClientEventsListener(new OnClientEventsListener() {

 public void onReceiveMessage(Client me, String message) {
  System.out.println("message from server "+message);

 public void onException(Client me, Exception exception) {
  System.out.println("client exception "+exception.getMessage());

 public void onConnect(Client me, Boolean connected) {
   //sends some message to server
   System.out.println("connection closed");
//thread starts, socket is going to open

Multipurpose and scalar: you can download these lightweight lib or src and embed in your java-based platform.

Here some downloads:

Happy coding!

No comments:

Post a Comment