Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alessandro-salerno/framedtcp
A simple Java networking library that encapsulates messages using length-prefixing
https://github.com/alessandro-salerno/framedtcp
framing framing-protocols tcp
Last synced: about 1 month ago
JSON representation
A simple Java networking library that encapsulates messages using length-prefixing
- Host: GitHub
- URL: https://github.com/alessandro-salerno/framedtcp
- Owner: Alessandro-Salerno
- License: mit
- Created: 2023-05-12T21:17:28.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-14T14:41:36.000Z (6 months ago)
- Last Synced: 2024-07-14T16:26:21.837Z (6 months ago)
- Topics: framing, framing-protocols, tcp
- Language: Java
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FramedTCP
A simple Java networking library that encapsulates messages using length-prefixing## The problem
When you send messages over TCP sockets, you're not guaranteed to receive those messages exactly how you sent them.
TCP only guarantees that you'll receive all packets in the correct order, but does not keep bounderies between messages.
This means that if you were to read the entire buffer, you may get a partial message or multiple messages at once.## The solution
A possible solution is to encapsule messages into an application-level frame that also carries the **length** of the message. This way, the receiver always knows where a message starts and where it ends.## Build
This project uses Maven as its build system.## How to use
The library provides a series of classes that you can use to take advantage of its features. Unfortunatelly these are **NOT** standardized for now.
This is how you build an echo server, for example.### Server
```java
try (ServerSocket serverSocket = new ServerSocket(8000)) {
Socket client = serverSocket.accept();
FramedReader reader = new FramedReader(client.getInputStream());
FramedWriter writer = new FramedWriter(new OutputStreamWriter(client.getOutputStream()));
writer.writeBytes(reader.readBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
```
### Client
```java
try (Socket socket = new Socket("", 8000)) {
FramedReader reader = new FramedReader(socket.getInputStream());
FramedWriter writer = new FramedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.writeString("Hello world");
System.out.println("ECHO: " + reader.readString());
} catch (Exception e) {
throw new RuntimeException(e);
}
```## License
This projects is license under the MIT License.