Tutorial - czifro/JSocket GitHub Wiki
Some example use lambda expression. Your Java configuration will need to be set appropriately for that.
Instantiate a new Socket object using one of two methods below:
// Use this:
java.net.ServerSocket server = new ServerSocket(port); // Used to accept connections
java.net.Socket conn = server.accept(); // Accepts a TCP Socket connection
// Or this:
java.net.Socket conn = new Socket("ipaddress", port); // Connect to a TCP server
// to instantiate Socket object and wrap with following classes
Then wrap with one of the following classes
JSocket -- capable of sending and receiving a byte[], sample code:
JSocket sock = new JSocket(conn); // Wraps around Socket
sock.send(object.getBytes()); // Pass a byte[] as argument
byte[] fixedBytes = sock.recv(); // Receives bytes, buffer size is limited
sock.CHUNK_SIZE = 128; // Set how big of chunks to read, all subclasses inherit this
byte[] allBytes = sock.recv_all(size); // Receives bytes, buffer size is dynamic
sock.send_all(allBytes, allBytes.length); // can send a large byte array
sock.close(); // Closes Socket and I/O stream objects, inherited by all subclasses
MessageSocket -- sends and receives strings, implementation 1:
MessageSocket sock = new MessageSocket(conn); // Wraps around Socket
sock.send_msg("Hello World!"); // Pass a string as argument, string is written to stream and sent
String msg = sock.recv_msg(); // Receives a message, buffer size is limited
String wholeMsg = sock.recv_all_msg(size); // If message is large, use this to set new buffer size
MessageSocket -- with lambda functions, implementation 2:
// The following will configure MessageSocket to use a filtering function on the received message
MessageSocket sock = new MessageSocket(conn, (s) -> {
return s.replace('/', '-');
});
// Or you can use a predefined function from the FunctionTool class
sock.setDefaultFunction(FunctionTool.sanitizeFunction(FunctionType.ONLY_NULLS);
// Then you can use the following to have the lambda function be applied to the incoming message
// Following uses first configuration
String dateMessage = sock.recvSanitizedMsg() // incoming msg: 8/5/2015 -- dateMessage will have 8-5-2015
// Following uses second
String sanitizedMessage = sock.recvSanitizedMsg() // incoming: h\0e\0l\0l\0o\0 -- sanitizedMessage = hello
// If there is only one instance a certain function needs to be used,
// the following allows a function to be used for a single instance
String msg = sock.recvSanitizedMsg((s) -> {
String clean = "";
for (char c : s.toCharArray())
if (c != '\0')
clean += c;
clean = clean.replace("Bob", "Will");
return clean;
});
// This method has an overload that uses a boolean for a second argument that will tell the class to use
// the function that was passed as the default for all following incoming requests
// this class is backwards compatible with implementation 1
ObjectSocket -- sends and receives objects, converts objects to and from JSON, sample code:
ObjectSocket sock = new ObjectSocket(conn); // Wraps around Socket
Person p = new Person("William", 22);
sock.send_object(p, Person.class); // Object will be converted to a JSON String and sent
Person p2 = sock.recv_object(Person.class); // Specify class type and it will return an object of that type
String json = sock.recv_object_asString(); // Returns the JSON that is received
String failedJson = sock.recover_failed_json() // Should parsing the received JSON fail, you can recover it
FileTransferSocket -- send and receive files over a Socket connection. File can be of any format and of arbitrary size.
FileTransferSocket sock = new FileTransferSocket(conn); // Wraps around Socket
File f = new File("/Users/username/Desktop/Person.java"); // File to be sent
sock.send_file(f); // Object will be converted to a JSON String and sent
File rF = sock.recv_file(path); // Specify path to save file as String and it will write the file to that location