src/pkg/net/tcpsock.go - The Go Programming Language

Golang

Source file src/pkg/net/tcpsock.go

     1	// Copyright 2009 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// TCP sockets
     6	
     7	package net
     8	
     9	// TCPAddr represents the address of a TCP end point.
    10	type TCPAddr struct {
    11		IP   IP
    12		Port int
    13	}
    14	
    15	// Network returns the address's network name, "tcp".
    16	func (a *TCPAddr) Network() string { return "tcp" }
    17	
    18	func (a *TCPAddr) String() string {
    19		if a == nil {
    20			return "<nil>"
    21		}
    22		return JoinHostPort(a.IP.String(), itoa(a.Port))
    23	}
    24	
    25	// ResolveTCPAddr parses addr as a TCP address of the form
    26	// host:port and resolves domain names or port names to
    27	// numeric addresses on the network net, which must be "tcp",
    28	// "tcp4" or "tcp6".  A literal IPv6 host address must be
    29	// enclosed in square brackets, as in "[::]:80".
    30	func ResolveTCPAddr(net, addr string) (*TCPAddr, error) {
    31		ip, port, err := hostPortToIP(net, addr)
    32		if err != nil {
    33			return nil, err
    34		}
    35		return &TCPAddr{ip, port}, nil
    36	}