翔云

Just try, don't shy. 最新文章请点击
随笔 - 294, 文章 - 0, 评论 - 27, 阅读 - 49万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Golang client绑定本地IP和端口

Posted on   翔云123456  阅读(1582)  评论(0编辑  收藏  举报

有时需要指定网络通信时本地使用的IP地址和端口号。

在Go语言中可通过定义 Dialer 中LocalAddr 成员实现。
Dialer结构定义如下:

// A Dialer contains options for connecting to an address.
//
// The zero value for each field is equivalent to dialing
// without that option. Dialing with the zero value of Dialer
// is therefore equivalent to just calling the Dial function.
type Dialer struct {
    ...

	// LocalAddr is the local address to use when dialing an
	// address. The address must be of a compatible type for the
	// network being dialed.
	// If nil, a local address is automatically chosen.
	LocalAddr Addr
}

Addr是接口类型,其定义如下:

// Addr represents a network end point address.
//
// The two methods Network and String conventionally return strings
// that can be passed as the arguments to Dial, but the exact form
// and meaning of the strings is up to the implementation.
type Addr interface {
	Network() string // name of the network (for example, "tcp", "udp")
	String() string  // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
}

目前实现Addr接口的类型并且被net 库支持的类型 包括:TCPAddr、UDPAddr、IPAddr。

下面通过代码演示如何指定client 使用的IP和端口号。

例子中使用TCPAddr 类型。

client


package main


import (

	"bufio"
	"fmt"
	"net"
	"os"
	"time"
)


func DialCustom(network, address string, timeout time.Duration, localIP []byte, localPort int)(net.Conn,error) {
	netAddr := &net.TCPAddr{Port:localPort}

	if len(localIP) != 0 {
		netAddr.IP = localIP
	}

	fmt.Println("netAddr:", netAddr)

	d := net.Dialer{Timeout: timeout, LocalAddr: netAddr}
	return d.Dial(network, address)
}


func main() {

	serverAddr := "172.20.22.160:8080"

	// 172.28.0.180
	//localIP := []byte{0xAC, 0x1C, 0, 0xB4}  // 指定IP
	localIP := []byte{} //  any IP,不指定IP
	localPort := 9001   // 指定端口
	conn, err := DialCustom("tcp", serverAddr, time.Second*10, localIP,localPort)
	if err != nil {
		fmt.Println("dial failed:", err)
		os.Exit(1)
	}
	defer conn.Close()


	buffer := make([]byte, 512)
	reader := bufio.NewReader(conn)

	n, err2 := reader.Read(buffer)
	if err2 != nil {
		fmt.Println("Read failed:", err2)
		return
	}

	fmt.Println("count:", n, "msg:", string(buffer))

	select{}	
}




server

package main

import (
	"fmt"
	"net"
	"log"
)

func main() {

	addr := "0.0.0.0:8080"

	tcpAddr, err := net.ResolveTCPAddr("tcp",addr)

	if err != nil {
		log.Fatalf("net.ResovleTCPAddr fail:%s", addr)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatalf("listen %s fail: %s", addr, err)
	} else {
	
		log.Println("rpc listening", addr)
	}


	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Println("listener.Accept error:", err)
			continue
		}
	
		go handleConnection(conn)
	
	}

}


func handleConnection(conn net.Conn) {

	//defer conn.Close()

	var buffer []byte = []byte("You are welcome. I'm server.")

	n, err := conn.Write(buffer)

	if err != nil {
	
		fmt.Println("Write error:", err)
	}
	fmt.Println("send:", n)

	fmt.Println("connetion end")

}



测试

启动client,只指定端口

$ ./client 
netAddr: :9001
count: 28 msg: You are welcome. I'm server.

启动client,指定IP,Port

$ ./client
netAddr: 172.28.172.180:9001
count: 28 msg: You are welcome. I'm server

server输出

./sever
2018/06/19 18:15:41 rpc listening 0.0.0.0:8080
send: 28
connetion end

查看连接

$ netstat -anp | grep 8080
tcp        0      0 :::8080                     :::*                        LISTEN      27328/./server      
tcp        0      0 ::ffff:172.20.22.160:8080   ::ffff:172.28.0.180:9001  ESTABLISHED 27328/./server   

从测试结果看,可以成功指定IP和端口。

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示