httpu client bind to specific address function (#11)

added additional function to allow for specifying which IP the PacketConn should listen on and broadcast on
This commit is contained in:
traetox 2016-09-22 14:02:28 -06:00 committed by Huin
parent 46bde78b11
commit d7ddae7b46

View File

@ -3,6 +3,7 @@ package httpu
import (
"bufio"
"bytes"
"errors"
"fmt"
"log"
"net"
@ -28,6 +29,20 @@ func NewHTTPUClient() (*HTTPUClient, error) {
return &HTTPUClient{conn: conn}, nil
}
// NewHTTPUClientAddr creates a new HTTPUClient which will broadcast packets
// from the specified address, opening up a new UDP socket for the purpose
func NewHTTPUClientAddr(addr string) (*HTTPUClient, error) {
ip := net.ParseIP(addr)
if ip == nil {
return nil, errors.New("Invalid listening address")
}
conn, err := net.ListenPacket("udp", ip.String()+":0")
if err != nil {
return nil, err
}
return &HTTPUClient{conn: conn}, nil
}
// Close shuts down the client. The client will no longer be useful following
// this.
func (httpu *HTTPUClient) Close() error {