remote-mic/socketops/socketops.go

52 lines
770 B
Go
Raw Normal View History

2020-04-28 21:42:45 +00:00
package socketops
import (
"fmt"
"net"
)
const bufSize = 1024
func AcceptConn(connstr string) (net.Conn, error) {
addr, err := net.ResolveTCPAddr("tcp", connstr)
if err != nil {
return nil, err
}
sock, err := net.ListenTCP("tcp", addr)
if err != nil {
return nil, err
}
conn, err := sock.AcceptTCP()
if err != nil {
return nil, err
}
conn.SetNoDelay(true)
return conn, nil
}
func HandleConn(conn net.Conn, out chan<- []byte) {
buf := make([]byte, bufSize)
for {
n, err := conn.Read(buf)
if err != nil {
close(out)
break
}
if n > 0 {
out <- buf[:n]
fmt.Printf("Bytes read: %d\n", n)
}
}
}
func HandleData(data <-chan []byte) {
for {
buf, open := <-data
if !open {
break
}
fmt.Printf("%v\n", string(buf))
}
}