Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fourcube/ltcp

Simple TCP Handlers in golang
https://github.com/fourcube/ltcp

Last synced: 4 days ago
JSON representation

Simple TCP Handlers in golang

Awesome Lists containing this project

README

        

# ltcp

[![GoDoc](https://godoc.org/github.com/fourcube/ltcp?status.svg)](http://godoc.org/github.com/fourcube/ltcp) [![Build Status](https://travis-ci.org/fourcube/ltcp.svg?branch=master)](https://travis-ci.org/fourcube/ltcp)

ltcp is a simple library providing an easy way to spawn tcp handlers. It might be useful for your tests.

Handlers are functions of type `func (net.Conn)`.

Sample code:

```go
package main

import (
"github.com/fourcube/ltcp"
"net"
)

func main() {
// Listens on some random, available port on all interfaces
// handles all connections with the ltcp.EchoHandler
//
// You can send ltcp.Shutdown through the 'done' channel to stop the listener.
//
done := make(chan struct{})
addr, err := ltcp.ListenAny(ltcp.EchoHandler, done)

conn, err := net.Dial("tcp", addr.String())
if err != nil {
// ...
}

// ... do whatever with the connection

conn.Close()
// The listener will shutdown when it receives something on the done channel
// It then closes the done channel
done <- ltcp.Shutdown
<-done
}
```