WatsonTcp provides a simple TCP messaging framework with message framing, authentication, and connection management. This Go implementation shares the same wire protocol as the C# library and can communicate with it directly.
- Message framing compatible with WatsonTcp for C#
- TLS encryption support
- Optional preshared key authentication
- Idle timeouts and keepalive settings
- Send and receive byte slices or streams
- Synchronous request/response messaging
- Connection filters (allow/deny lists)
- Connection limit enforcement
- Runtime statistics (bytes and messages sent/received)
- Optional debug logging with customizable logger
go get github.com/WasimAhmad/watsontcp-go
Import the packages you need:
import (
"github.com/WasimAhmad/watsontcp-go/client"
"github.com/WasimAhmad/watsontcp-go/server"
)
package main
import (
"fmt"
"log"
"github.com/WasimAhmad/watsontcp-go/message"
"github.com/WasimAhmad/watsontcp-go/server"
)
func main() {
cb := server.Callbacks{}
cb. string, msg *message.Message, data []byte) {
fmt.Printf("%s: %s\n", id, string(data))
}
srv := server.New("127.0.0.1:9000", nil, cb, nil)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
defer srv.Stop()
select {}
}
package main
import (
"log"
"github.com/WasimAhmad/watsontcp-go/client"
"github.com/WasimAhmad/watsontcp-go/message"
)
func main() {
cb := client.Callbacks{}
cb. *message.Message, data []byte) {
log.Printf("server: %s", string(data))
}
c := client.New("127.0.0.1:9000", nil, cb, nil)
if err := c.Connect(); err != nil {
log.Fatal(err)
}
defer c.Disconnect()
c.Send(&message.Message{}, []byte("hello"))
}
Both the client and server accept Options structures which enable debug
logging. Set DebugMessages to true and provide a Logger function matching
fmt.Printf to receive logs whenever messages are sent or received.
opts := client.DefaultOptions()
opts.Logger = log.Printf
opts.DebugMessages = true
c := client.New("127.0.0.1:9000", nil, cb, &opts)
The examples directory contains small programs that demonstrate most
library features:
Test.Client– interactive console clientTest.Server– simple console serverTest.FileTransfer– streaming data between client and serverTest.Metadata– sending messages with metadata mapsTest.Parallel– multiple clients sending concurrentlyTest.Reconnect– reconnect logic for unreliable networksTest.SyncMessages– synchronous request/response messaging
Build an example with:
go build ./examples/<ExampleName>
Because the framing and message structure mirror the C# implementation, a Go client can communicate with a C# server and vice versa. Ensure that features such as preshared key authentication are enabled or disabled consistently on both sides.