Use net.JoinHostPort() instead of hand-joining

Hand-joining breaks IPv6 literal addresses (which must be enclosed in
brackets).
pull/71/head
Eugene Kim 6 years ago
parent 0d95101f50
commit dbc28e6fcf
  1. 3
      identitychain/identitychain.go
  2. 5
      node/node.go
  3. 4
      p2p/peer.go

@ -124,7 +124,8 @@ func (IDC *IdentityChain) StartServer() {
}
func (IDC *IdentityChain) listenOnPort() {
listen, err := net.Listen("tcp4", ":"+IDC.Peer.Port)
addr := net.JoinHostPort("", IDC.Peer.Port)
listen, err := net.Listen("tcp4", addr)
if err != nil {
IDC.log.Crit("Socket listen port failed")
os.Exit(1)

@ -82,14 +82,15 @@ func (node *Node) StartServer(port string) {
}
func (node *Node) listenOnPort(port string) {
listen, err := net.Listen("tcp4", ":"+port)
addr := net.JoinHostPort("", port)
listen, err := net.Listen("tcp4", addr)
defer func(listen net.Listener) {
if listen != nil {
listen.Close()
}
}(listen)
if err != nil {
node.log.Error("Socket listen port failed", "port", port, "err", err)
node.log.Error("Socket listen port failed", "addr", addr, "err", err)
return
}
for {

@ -100,7 +100,7 @@ func ConstructP2pMessage(msgType byte, content []byte) []byte {
// TODO(minhdoan, rj): need to check if a peer is reachable or not.
func sendWithSocketClient(ip, port string, message []byte) (res string) {
//log.Printf("Sending message to ip %s and port %s\n", ip, port)
addr := strings.Join([]string{ip, port}, ":")
addr := net.JoinHostPort(ip, port)
conn, err := net.Dial("tcp", addr)
if err != nil {
@ -127,7 +127,7 @@ func send(ip, port string, message []byte) (returnMessage string) {
func DialWithSocketClient(ip, port string) (conn net.Conn, err error) {
//log.Printf("Sending message to ip %s and port %s\n", ip, port)
addr := strings.Join([]string{ip, port}, ":")
addr := net.JoinHostPort(ip, port)
conn, err = net.Dial("tcp", addr)
return
}

Loading…
Cancel
Save