Menu

Home

David Grant

JConn

JConn is a TCP networking framework for Java.

Download- see "release" section.

JavaDoc- https://jggcomputers.ddns.net/repo/public/docs/jconn/

How to use

For both client and server, import the JConn library into your project. Add the import statement import io.github.davidg95.jconn.*;

Data Objects

The JConnData class is used for wrapping data up and sending it from client to server or vice-versa. JConn data consists of a Flag which indicates what the receiving end should do with the data, and a list of parameters. The JConnData.create() method. To Create a data object with "TEST" as the flag and a String parameter called "MESSAGE", the following would be done-

JConnData.create("MESSAGE").addParam("MESSAGE", "this is a message for the receiving end");

The process of sending these data objects is discussed in later sections.

Server

You need to choose a main class for handling your different requests. In the class, you need to annotate method using @JConnMethod. For example, say my client sent a message to the server with "TEST" in the request flag, I would have a method annotated as the following:

@JConnMethod("TEST")
public void testMethod(){

}

This method will not take any parameters. To accept parameters, you annotated the method parameters using the @JConnParameter annotation-

@JConnMethod("TEST")
public void testMethod(@JConnParameter("MESSAGE") String message){

}

This method will not return any reply to the client, to return a reply, simple change your method header to include a return value-

@JConnMethod("TEST")
public String testMethod(@JConnParameter("MESSAGE") String message){
    return "Received " + message;
}

To start the server, you need to create a JConnServer instance. In the same class, create a field of type JConnServer, and the create an instance by doing server = JConnServer.start(4444, this, false);-

public class Server(){
    private JConnServer server;

    public Server(){
        server = JConnServer.start(4444, this, false);
    }

Where "4444" is the port number to listen on, "this" is a reference to the class that has the annotations, and "false" is indicating that you do not want log output from JConn. Changing false to true will output debug info from JConn.

The next thing to do is to create a class that implements JConnListener or use the current class. You will then need to override 3 method onReceive(JConnData data), onConnectionDrop(JConnEvent event) and onConnectionEstablish(JConnEvent event)-

public class Server implements JConnListener{

    public void onReceive(JConnData data){

    }

    public void onConnectionDrop(JConnEvent event){

    }

    public void onConnectionEstablish(JConnEvent event){

    }

    public void onServerGracefulEnd(){

    }
}

onReceive() is called whenever data is received from a client and there is not a @JConnMethod annotated method to match it up to. onConnectionDrop() is called whenever a client disconnects.

Next, you need to register the listener-

public Server(){
    server.registerListener(this);
}

To send data to client, Use server.sendData(), passing in a JConnData object-

public Server(){
    server.sendData(IP, JConnData.create("SERVER-MESSAGE").addParam("BODY", "Hello"));
}

IP is the IP address of the connected client, you can also pass in null and the data will be sent to all connected clients.

Client

Create a new JConn object by doing JConn conn = new JConn(). Then connect to the server by doing conn.connect(IP, PORT); Where IP is the server address and PORT is the port number-

public Client(){
    JConn conn = new JConn();
    conn.connect("127.0.0.1", 4444);
}

There are two ways to send data from the client, conn.sendData(JConnData) which returns the server reply in a JConnData object, and conn.sendData(JConnData, JConnRunnable) which returns the reply to the runnable object and executes it when the reply is received-

Method 1-

String reply = (String) conn.sendData(JConnData.create("TEST").addParam("MESSAGE", "This is a message for the server"));

Method 2-

conn.sendData(JConnData.create("TEST").addParam("MESSAGE", "This is a message for the server"), new JConnRunnable(){
    @Override
    public void run(JConnData reply){
        String message = (String) reply.getReturnValue();
    }
});

Method 1 will block the calling thread until a reply will be received. Method 2 will allow the calling thread to continue its execution.

Next, similar to the server, create or use a class that implements the JConnListener interface and override its methods. onReceive(JConnData) is called whenever the server sends data to the client from its JConnServer.sendData() method. onConnectionDrop(JConnEvent) is called whenever the server connection is lost and onConnectionReestablish(JConnEvent) is called whenever the server connection is reestablished. When the connection is lost, the client will attempt to reconnect every second.