當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TCPAddr.Port方法代碼示例

本文整理匯總了Golang中net.TCPAddr.Port方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPAddr.Port方法的具體用法?Golang TCPAddr.Port怎麽用?Golang TCPAddr.Port使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.TCPAddr的用法示例。


在下文中一共展示了TCPAddr.Port方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ts6_main

func ts6_main(msg chan string, exit chan int) {

	// No configuration, so this is fixed.
	var addr net.TCPAddr
	addr.IP = net.IPv4(127, 0, 0, 1)
	addr.Port = 3725

	// Start our listener.
	l, err := net.ListenTCP("tcp4", &addr)
	if err != nil {
		fmt.Printf("No bind: %s\n", err)
		exit <- 0
	} else {
		go listen(l)

		// Again, no configuration.
		addr.Port = 13725
		c, err := net.DialTCP("tcp4", nil, &addr)
		if err != nil {
			fmt.Printf("No connection: %s\n", err)
		} else {
			go link(c, true)
		}
	}

	// Handle messages.
	for message := range msg {
		if message == "exit" {
			exit <- 0
		}
	}
}
開發者ID:jbeshir,項目名稱:OddComm,代碼行數:32,代碼來源:main.go

示例2: MakeConn

func MakeConn(ip string) (*net.TCPConn, error) {
	var addr net.TCPAddr
	addr.IP = net.ParseIP(ip)
	addr.Port = Port
	c, err := net.DialTCP("tcp4", nil, &addr)
	return c, err
}
開發者ID:dilfish,項目名稱:dist,代碼行數:7,代碼來源:main.go

示例3: ListenTCP

// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
	m := channelForwardMsg{
		"tcpip-forward",
		true, // sendGlobalRequest waits for a reply
		laddr.IP.String(),
		uint32(laddr.Port),
	}
	// send message
	resp, err := c.sendGlobalRequest(m)
	if err != nil {
		return nil, err
	}

	// If the original port was 0, then the remote side will
	// supply a real port number in the response.
	if laddr.Port == 0 {
		port, _, ok := parseUint32(resp.Data)
		if !ok {
			return nil, errors.New("unable to parse response")
		}
		laddr.Port = int(port)
	}

	// Register this forward, using the port number we obtained.
	//
	// This does not work on OpenSSH < 6.0, which will send a
	// ChannelOpenMsg with port number 0, rather than the actual
	// port number.
	ch := c.forwardList.add(*laddr)

	return &tcpListener{laddr, c, ch}, nil
}
開發者ID:viking,項目名稱:go-ssh,代碼行數:35,代碼來源:tcpip.go

示例4: ListenTCP

// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
	if laddr.Port == 0 && isBrokenOpenSSHVersion(c.serverVersion) {
		return c.autoPortListenWorkaround(laddr)
	}

	m := channelForwardMsg{
		"tcpip-forward",
		true, // sendGlobalRequest waits for a reply
		laddr.IP.String(),
		uint32(laddr.Port),
	}
	// send message
	resp, err := c.sendGlobalRequest(m)
	if err != nil {
		return nil, err
	}

	// If the original port was 0, then the remote side will
	// supply a real port number in the response.
	if laddr.Port == 0 {
		port, _, ok := parseUint32(resp.Data)
		if !ok {
			return nil, errors.New("unable to parse response")
		}
		laddr.Port = int(port)
	}

	// Register this forward, using the port number we obtained.
	ch := c.forwardList.add(*laddr)

	return &tcpListener{laddr, c, ch}, nil
}
開發者ID:npe9,項目名稱:minimega,代碼行數:35,代碼來源:tcpip.go

示例5: main

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, "Usage: %s message", os.Args[0])
		os.Exit(1)
	}
	message := os.Args[1]

	serverIP := "127.0.0.1" //サーバ側のIP
	serverPort := "55555"   //サーバ側のポート番號
	myIP := "127.0.0.1"     //クライアント側のIP
	myPort := 55556         //クライアント側のポート番號

	tcpAddr, err := net.ResolveTCPAddr("tcp", serverIP+":"+serverPort)
	checkError(err)
	myAddr := new(net.TCPAddr)
	myAddr.IP = net.ParseIP(myIP)
	myAddr.Port = myPort
	conn, err := net.DialTCP("tcp", myAddr, tcpAddr)
	checkError(err)
	defer conn.Close()

	conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
	conn.Write([]byte(message))

	readBuf := make([]byte, 1024)
	conn.SetReadDeadline(time.Now().Add(10 * time.Second))
	readlen, err := conn.Read(readBuf)
	checkError(err)

	fmt.Println("server: " + string(readBuf[:readlen]))
}
開發者ID:Goryudyuma,項目名稱:go-work,代碼行數:31,代碼來源:client.go

示例6: ListenTCP

// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
	m := channelForwardMsg{
		"tcpip-forward",
		true, // sendGlobalRequest waits for a reply
		laddr.IP.String(),
		uint32(laddr.Port),
	}
	// send message
	resp, err := c.sendGlobalRequest(m)
	if err != nil {
		return nil, err
	}
	// fixup laddr. If the original port was 0, then the remote side will
	// supply one in the resp.
	if laddr.Port == 0 {
		port, _, ok := parseUint32(resp.Data)
		if !ok {
			return nil, errors.New("unable to parse response")
		}
		laddr.Port = int(port)
	}

	// register this forward
	ch := c.forwardList.add(laddr)
	return &tcpListener{laddr, c, ch}, nil
}
開發者ID:rajasaur,項目名稱:sync_gateway,代碼行數:29,代碼來源:tcpip.go

示例7: ListenTCP

// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *Client) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
	if laddr.Port == 0 && isBrokenOpenSSHVersion(string(c.ServerVersion())) {
		return c.autoPortListenWorkaround(laddr)
	}

	m := channelForwardMsg{
		laddr.IP.String(),
		uint32(laddr.Port),
	}
	// send message
	ok, resp, err := c.SendRequest("tcpip-forward", true, Marshal(&m))
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, errors.New("ssh: tcpip-forward request denied by peer")
	}

	// If the original port was 0, then the remote side will
	// supply a real port number in the response.
	if laddr.Port == 0 {
		var p struct {
			Port uint32
		}
		if err := Unmarshal(resp, &p); err != nil {
			return nil, err
		}
		laddr.Port = int(p.Port)
	}

	// Register this forward, using the port number we obtained.
	ch := c.forwards.add(*laddr)

	return &tcpListener{laddr, c, ch}, nil
}
開發者ID:CodeJuan,項目名稱:kubernetes,代碼行數:38,代碼來源:tcpip.go

示例8: TCPAddr

func TCPAddr(buf []byte) (net.TCPAddr, int) {

	var value net.TCPAddr
	value.IP = make([]byte, 16)
	copy(value.IP, buf[:16])
	value.Port = int(binary.BigEndian.Uint16(buf[16:18]))

	return value, Size

}
開發者ID:spearson78,項目名稱:guardian,代碼行數:10,代碼來源:tcpaddr.go

示例9: main

func main() {

	var localaddr net.TCPAddr
	var remoteaddr net.TCPAddr
	localaddr.IP = net.ParseIP("192.168.0.109")
	localaddr.Port = 0
	remoteaddr.IP = net.ParseIP("192.168.0.1")
	remoteaddr.Port = 80

	if localaddr.IP == nil || remoteaddr.IP == nil {
		fmt.Println("error")
	}

	if _, err := net.DialTCP("tcp", &localaddr, &remoteaddr); err != nil {
		fmt.Println(err)
	}

	fmt.Println("End")

}
開發者ID:z0x010,項目名稱:sharp-proxy,代碼行數:20,代碼來源:1.go

示例10: webserver

// Start a webserver serving files from the given path. Take the address given and attach the HTTP
// server to a random port. If portCh is non-nil, sends the chosen port on this channel.
func webserver(path string, addr net.TCPAddr, portCh chan int) {
	addr.Port = 0
	listener, err := net.ListenTCP("tcp", &addr)
	if portCh != nil {
		portCh <- listener.Addr().(*net.TCPAddr).Port
	}

	log.Printf("webserver listening on: %s", listener.Addr())
	err = http.Serve(listener, http.FileServer(http.Dir(path)))
	if err != nil {
		panic(err)
	}
}
開發者ID:samthor,項目名稱:atvlib,代碼行數:15,代碼來源:web.go

示例11: Handle

func (s *Server) Handle(conn net.Conn) {
	defer conn.Close()
	ccfb := cipher.NewCFBDecrypter(s.ClientCipher, iv)
	scfb := cipher.NewCFBEncrypter(s.ServerCipher, iv)
	out := make([]byte, 1024)
	readAndDecode(conn, out[:4], ccfb)
	if out[1] != 1 {
		log.Print("unsupport command")
		return
	}
	addr := new(net.TCPAddr)
	switch out[3] { //atyp
	case 1: //ipv4
		readAndDecode(conn, out[:4], ccfb)
		addr.IP = net.IPv4(out[0], out[1], out[2], out[3])
	case 3: //domain
		readAndDecode(conn, out[:1], ccfb)
		l := out[0]
		readAndDecode(conn, out[:l], ccfb)
		host := string(out[:l])
		addrs, err := net.LookupIP(host)
		if err != nil || len(addrs) == 0 {
			log.Print("cannot resolve ", host)
			return
		}
		addr.IP = addrs[0]
	default:
		log.Print("unsupport address type")
		return
	}
	readAndDecode(conn, out[:2], ccfb)
	addr.Port = nl2int(out)

	sConn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		log.Print("cannot connect to server", addr.String())
		encodeAndWrite(conn, out[:10], scfb)
		return
	}
	defer sConn.Close()
	response := make([]byte, 10)
	response[0] = 5
	response[1] = 0
	response[2] = 0
	response[3] = 1
	copy(response[4:], sConn.LocalAddr().(*net.TCPAddr).IP)
	copy(response[8:], int2nl(sConn.LocalAddr().(*net.TCPAddr).Port))
	encodeAndWrite(conn, response, scfb)
	go HandleStream(conn, sConn, scfb)
	HandleStream(sConn, conn, ccfb)
}
開發者ID:hyqhyq3,項目名稱:avsocks,代碼行數:51,代碼來源:server.go

示例12: Listen

// Opens a TCP server socket and returns a stream listener, ready to accept. If
// an auto-port (0) is requested, the port is updated in the argument.
func Listen(addr *net.TCPAddr) (*Listener, error) {
	// Open the server socket
	sock, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return nil, err
	}
	addr.Port = sock.Addr().(*net.TCPAddr).Port

	// Initialize and return the listener
	return &Listener{
		socket: sock,
		Sink:   make(chan *Stream),
		quit:   make(chan chan error),
	}, nil
}
開發者ID:simia-tech,項目名稱:iris,代碼行數:17,代碼來源:stream.go

示例13: autoPortListenWorkaround

// autoPortListenWorkaround simulates automatic port allocation by
// trying random ports repeatedly.
func (c *Client) autoPortListenWorkaround(laddr *net.TCPAddr) (net.Listener, error) {
	var sshListener net.Listener
	var err error
	const tries = 10
	for i := 0; i < tries; i++ {
		addr := *laddr
		addr.Port = 1024 + portRandomizer.Intn(60000)
		sshListener, err = c.ListenTCP(&addr)
		if err == nil {
			laddr.Port = addr.Port
			return sshListener, err
		}
	}
	return nil, fmt.Errorf("ssh: listen on random port failed after %d tries: %v", tries, err)
}
開發者ID:CodeJuan,項目名稱:kubernetes,代碼行數:17,代碼來源:tcpip.go

示例14: main

func main() {

	fmt.Printf("Hello, 世界\n")

	// socket einrichten und auf verbindung warten
	var listenAddr *net.TCPAddr = new(net.TCPAddr)
	listenAddr.Port = INPORT
	listenSock, err := net.ListenTCP("tcp4", listenAddr)
	if err != nil {
		fmt.Printf(err.String())
	}

	for {
		//establisch connection
		conn, _ := listenSock.AcceptTCP()
		go handleConnection(conn) // add 'go' to allow multiple connections
	}
}
開發者ID:cc-syncsuite,項目名稱:piggydemon,代碼行數:18,代碼來源:piggydemon.go

示例15: slimprotoConnect

// Connect to slimproto
func slimprotoConnect(addr net.IP, port int) {

	sbsAddr := new(net.TCPAddr)
	sbsAddr.IP = addr
	sbsAddr.Port = port

	var err error
	slimproto.Conn, err = net.DialTCP("tcp", nil, sbsAddr)
	checkError(err)
	//slimproto.Conn.SetDeadline(time.Time(10e9))

	if *debug {
		log.Println("Connected to slimproto")
	}

	return

}
開發者ID:terual,項目名稱:slimgo,代碼行數:19,代碼來源:slimproto.go


注:本文中的net.TCPAddr.Port方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。