Skip to content

01. User Guids

Naveen Balasooriya edited this page May 13, 2024 · 1 revision

Common library settings

In the library, there are two main but optional settings that users can change according to their causes.

  1. Default buffer size - Set the default maximum buffer size for IO. This will help you when it comes to memory management while sending and receiving data. Buffer size is saved in bytes.

    SocketDataHandler.setDefaultBufferSize(1024);
  2. Temporary folder - Set the default temporary folder for receiving files. If this is not set manually, the library will automatically create and use a folder named 'Temp' in the working directory. You need to consider file permissions when setting or using default locations. The library will try to create a folder if it does not exist.

    SocketDataHandler.setTempFolder(new File("Temp"));

Setting up the receiver

Now you're good to code your receiver file. To do that, make a class that extends the SocketDataHandler and implement the required constructors and methods in that class.

MySocket.java

public class MySocket extends SocketDataHandler {
    public MySocket(Socket SOCKET) {
        super(SOCKET);
    }

    @Override
    public void onUpdateReceived(DataHandler update) {

    }

    @Override
    public void onPreUpdateReceived(PreUpdateHandler preUpdate) {

    }

    @Override
    public void onDisconnected(DataProcessor.DisconnectStatus status, Exception exception) {

    }
}
  • onUpdateReceived - On an update from the other end of the socket completely consumed and processed by the library, this method will call.
  • onPreUpdateReceived - On an update from either this end or other end of the socket consuming and processing by the library, this method will call. Only serializable objects and files updates can be retrieved using this. Assume that you sent a file that has a size of 10KB and the default buffer size is 1 KB. Then, when it comes to sending/receiving, you will receive 11 updates via this method. After every attempt to send/receive data buffer, this method will invoke. The last update will indicate that the process of sending/receiving is completed.
  • onDisconnected - On disconnect of either this end or the other end of the socket, this method will invoke.

Bind socket and library

SocketDataHandler is just a library to manage socket IO operations. So you need to work on your network operations.

// connect to the server
Socket socket = new Socket("localhost", port);

// bind socket with library
MySocket clientSocket = new MySocket(socket);

Send data

You can use the DataHandler object to store your request and use the send method to send it. Note that the send method is part of your receiver class that extends with the SocketDataHandler.

DataHandler dataHandler = new DataHandler("/SendText");
try {
    clientSocket.send(dataHandler);
} catch(IOException e) {
    throw newRuntimeException(e);
}

Connection between onUpdateReceived and onPreUpdateReceived

When an update is received to the receiver/sender, it automatically gives a UUID (Universally Unique Identifier) for that request/response. If your request/response is eligible for pre-update it will automatically share that UUID.

Only Serializable objects and java.io.File are eligible to get pre-updates.

Close socket

You can either directly close the socket or use the close method of your receiver class that extends with the SocketDataHandler. On successful close, you will receive an update for onDisconneted.

try {
    clientSocket.close();
} catch(IOException e) {
    throw new RuntimeException(e);
}

Further reading

Please take a look at the Java-Doc to get a clear idea about methods and interfaces.