diff --git a/client/client.go b/client/client.go index 298e393..d7b375d 100644 --- a/client/client.go +++ b/client/client.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "net" - "strconv" "time" "github.com/veggiedefender/torrent-client/bitfield" @@ -64,8 +63,7 @@ func recvBitfield(conn net.Conn) (bitfield.Bitfield, error) { // New connects with a peer, completes a handshake, and receives a handshake // returns an err if any of those fail. func New(peer peers.Peer, peerID, infoHash [20]byte) (*Client, error) { - hostPort := net.JoinHostPort(peer.IP.String(), strconv.Itoa(int(peer.Port))) - conn, err := net.DialTimeout("tcp", hostPort, 3*time.Second) + conn, err := net.DialTimeout("tcp", peer.String(), 3*time.Second) if err != nil { return nil, err } diff --git a/peers/peers.go b/peers/peers.go index fdb5d0d..b6872b4 100644 --- a/peers/peers.go +++ b/peers/peers.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "fmt" "net" + "strconv" ) // Peer encodes connection information for a peer @@ -28,3 +29,7 @@ func Unmarshal(peersBin []byte) ([]Peer, error) { } return peers, nil } + +func (p Peer) String() string { + return net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port))) +} diff --git a/peers/peers_test.go b/peers/peers_test.go index 2194d7d..20cd507 100644 --- a/peers/peers_test.go +++ b/peers/peers_test.go @@ -37,3 +37,19 @@ func TestUnmarshal(t *testing.T) { assert.Equal(t, test.output, peers) } } + +func TestString(t *testing.T) { + tests := []struct { + input Peer + output string + }{ + { + input: Peer{IP: net.IP{127, 0, 0, 1}, Port: 8080}, + output: "127.0.0.1:8080", + }, + } + for _, test := range tests { + s := test.input.String() + assert.Equal(t, test.output, s) + } +}