Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xtrinch/socket-live
Java TCP/IP socket communication library
https://github.com/xtrinch/socket-live
java-sockets sockets thread
Last synced: 24 days ago
JSON representation
Java TCP/IP socket communication library
- Host: GitHub
- URL: https://github.com/xtrinch/socket-live
- Owner: xtrinch
- Created: 2017-10-22T04:08:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-26T22:29:08.000Z (almost 7 years ago)
- Last Synced: 2024-10-11T13:42:11.265Z (about 1 month ago)
- Topics: java-sockets, sockets, thread
- Language: Java
- Homepage:
- Size: 29.3 KB
- Stars: 10
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/si.trina/socket-live/badge.svg)](https://maven-badges.herokuapp.com/maven-central/si.trina/socket-live)
# socket-live
Java library for TCP/IP socket communication.
Consists of three main components (which later result into three running threads):
* SocketConnection.java: main thread, run by the user of the library, which makes sure the connection is always open
* SocketRead.java: read thread which continuously attempts to read incoming messages if any
* SocketWrite.java: write thread which writes any messages in write queue to socket## Installation
Package can be installed via maven by adding the following to your pom.xml:
si.trina
socket-live
0.0.3
## How to use**Initialize the socket connection**
/*
args:
** name of socket connection
** IP of socket endpoint
** port of socket endpoint
*/
SocketConnection example = new SocketConnection("m3scan-channel1", "10.0.0.2", 5040);
new Thread(example).start();**Add listeners to socket connection**
public class MyListener implements SocketListener {
@Override
public void processSocketEvent(byte[] data, SocketConnection connection) {
short aShort = connection.byteArrayToUInt16(new byte[] {data[0],reply[data[1]]});
byte[] stringByteArray = Arrays.copyOfRange(data,data[2],data[20]);
String aString = connection.byteArrayToString(stringByteArray);
...
}
}
MyListener listener = new MyListener();
example.addListener(listener);
Note that read thread expects an integer (4 bytes) with byte length of message before every message.**Send data to socket**
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(example.intToByteArray(42)); // for example: datagram length in bytes
os.write(example.uint16ToByteArray((short)1)); // 1, 2, 3
os.write(example.stringToByteArray("test", 40));example.sendToServer(os.toByteArray());
### Optional parameters of SocketConnection instance
**charsetName**
String indicating which charset to use when encoding/decoding strings (Java Charset strings apply).
**reconnectInterval**
Long indicating how often should connection attempt to be restored in case of target endpoind disconnecting.
**checkConnectionInterval**
Long indicating how often should the connection status be checked. Checking the connection status will result in attempting to reconnect if disconnected.
**readEnabled**
In case only writing to socket is required, this boolean can be set to false.
**writeEnabled**
In case only reading from socket is required, this boolean can be set to false.
## Logging
All logging with various priorities inside the library is done with slf4j. Meaning, you need a logger binding (for example slf4j-simple) to see logs.