https://github.com/wkhallen/jdtp
Modern networking interfaces for Java.
https://github.com/wkhallen/jdtp
java networking socket socket-client socket-server
Last synced: about 1 year ago
JSON representation
Modern networking interfaces for Java.
- Host: GitHub
- URL: https://github.com/wkhallen/jdtp
- Owner: WKHAllen
- License: mit
- Created: 2022-07-24T22:36:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-22T01:20:10.000Z (over 3 years ago)
- Last Synced: 2025-01-27T11:22:43.468Z (over 1 year ago)
- Topics: java, networking, socket, socket-client, socket-server
- Language: Java
- Homepage: https://wkhallen.com/dtp
- Size: 128 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Data Transfer Protocol for Java
Modern networking interfaces for Java.
## Data Transfer Protocol
The Data Transfer Protocol (DTP) is a larger project to make ergonomic network programming available in any language.
See the full project [here](https://wkhallen.com/dtp/).
## Creating a server
A server can be built using the `Server` implementation:
```java
import jdtp.Server;
// Create a server that receives strings and returns the length of each string
class MyServer extends Server {
@Override
protected void receive(long clientID, Object data) {
// Send back the length of the string
String message = (String) data;
send(clientID, message.length());
}
@Override
protected void connect(long clientID) {
System.out.printf("Client with ID %d connected\n", clientID);
}
@Override
protected void disconnect(long clientID) {
System.out.printf("Client with ID %d disconnected\n", clientID);
}
}
class Main {
public static void main(String[] args) {
// Start the server
MyServer server = new MyServer();
server.start("127.0.0.1", 29275);
}
}
```
## Creating a client
A client can be built using the `Client` implementation:
```java
import jdtp.Client;
// Create a client that sends a message to the server and receives the length of the message
class MyClient extends Client {
private final String message;
MyClient(String message) {
this.message = message;
}
@Override
protected void receive(Object data) {
// Validate the response
int intData = (int) data;
System.out.printf("Received response from server: %d\n", intData);
assert intData == message.length();
}
@Override
protected void disconnected() {
System.err.println("Unexpectedly disconnected from server");
}
}
class Main {
public static void main(String[] args) {
// Connect to the server
String message = "Hello, server!";
MyClient client = new MyClient(message);
client.connect("127.0.0.1", 29275);
// Send a message to the server
client.send(message);
}
}
```
## Serialization
The protocol is able to serialize and deserialize primitive types. Custom types can be used, though they will need to
implement the [`Serializable`](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) interface.
## Security
Information security comes included. Every message sent over a network interface is encrypted with AES-256. Key
exchanges are performed using a 4096-bit RSA key-pair.