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() {

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

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

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

 }

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

 }

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

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

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

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

 @Override
 public void onConnect(Client me, Boolean connected) {
  if(connected)
  {
   //sends some message to server
   me.sendMessage("HELLO"); 
   me.sendMessage("Wa");
   me.sendMessage(""); 
   me.sendMessage(null);
  }
  else
  {
   System.out.println("connection closed");
  }
 }
});
//thread starts, socket is going to open
client.start();

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