https://github.com/wkhallen/godtp
Cross-platform networking interfaces for Go.
https://github.com/wkhallen/godtp
golang networking socket socket-client socket-server
Last synced: 4 months ago
JSON representation
Cross-platform networking interfaces for Go.
- Host: GitHub
- URL: https://github.com/wkhallen/godtp
- Owner: WKHAllen
- License: mit
- Created: 2020-03-12T16:35:48.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T23:51:04.000Z (over 1 year ago)
- Last Synced: 2025-01-27T11:22:36.532Z (11 months ago)
- Topics: golang, networking, socket, socket-client, socket-server
- Language: Go
- Homepage: https://wkhallen.com/dtp
- Size: 43.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Data Transfer Protocol for Go
Cross-platform networking interfaces for Go.
## 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/).
## Installation
Install the package:
```sh
$ go get -u github.com/wkhallen/godtp
```
## Creating a server
A server can be built using the `Server` implementation:
```go
package example
import (
"fmt"
"github.com/wkhallen/godtp"
)
func main() {
// Create a server that receives strings and returns the length of each string
server, serverEvent := godtp.NewServer[int, string]()
err := server.Start("127.0.0.1", 29275)
if err != nil {
// Handle server start error
}
// Iterate over events
for event := range serverEvent {
switch event.EventType {
case godtp.ServerConnect:
fmt.Printf("Client with ID %d connected\n", event.ClientID)
case godtp.ServerDisconnect:
fmt.Printf("Client with ID %d disconnected\n", event.ClientID)
case godtp.ServerReceive:
// Send back the length of the string
err := server.Send(len(event.Data), event.ClientID)
if err != nil {
// Handle send error
}
}
}
}
```
## Creating a client
A client can be built using the `Client` implementation:
```go
package example
import (
"fmt"
"github.com/wkhallen/godtp"
)
func main() {
// Create a client that send a message to the server and receives the length of the message
client, clientEvent := godtp.NewClient[string, int]()
err := client.Connect("127.0.0.1", 29275)
if err != nil {
// Handle client connect error
}
// Send a message to the server
message := "Hello, server!"
err = client.Send(message)
if err != nil {
// Handle send error
}
// Receive the response
event := <-clientEvent
switch event.EventType {
case godtp.ClientReceive:
// Validate the response
fmt.Printf("Received response from server: %d", event.Data)
if event.Data != len(message) {
fmt.Printf("Invalid response: expected %d, received %d", len(message), event.Data)
}
default:
// Unexpected response
fmt.Printf("Expected to receive a response from the server, instead got %#v\n", event)
}
}
```
## Security
Information security comes included. Every message sent over a network interface is encrypted with AES-256. Key
exchanges are performed using a 2048-bit RSA key-pair.