TCP客户端在未达到某些语句的情况下结束。

huangapple go评论86阅读模式
英文:

tcp client ends without reaching some statements

问题

下面的代码应该打开与服务器的连接,发送一条消息,并在结束之前等待回复,但问题是它甚至没有达到函数的末尾。可能出了什么问题?我应该使用 wg sync.WaitGroup 吗?

func client(servId uint16, servAddr string) {

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

    conn, err := net.DialTCP("tcp", nil, tcpAddr)
    check(err)

    _, err = conn.Write(handshake(servId, 1500))
    check(err)

    init := make([]byte, 8)
    _, err = io.ReadFull(conn, init)
    check(err)

    fmt.Println("is reached") // 为什么没有打印出来?
    fmt.Println(init)
}

上面的函数如何从 main 中调用:

// 变量
var c, startId, servId uint16

// 对于每个服务器
for s := 0; s < len(config.Servers); s++ {

    // 对于每个连接
    for c = 0; c < config.Total_clients; c++ {

        startId = config.Server_id * config.Total_clients
        servId = startId + c

        servAddr := fmt.Sprintf("%s:%d", config.Servers[s].Host, config.Servers[s].Port)

        // 在新的 Go 协程中创建客户端
        go client(servId, servAddr)
    }
}
英文:

The code below is suppose to open a connection to a server, send a message and wait for a reply before ending but the problem is it isn't even reaching the end of the function. What could be wrong? Should I use wg sync.WaitGroup?

func client(servId uint16, servAddr string) {

	tcpAddr, err := net.ResolveTCPAddr(&quot;tcp&quot;, servAddr)
	check(err)

	conn, err := net.DialTCP(&quot;tcp&quot;, nil, tcpAddr)
	check(err)

	_, err = conn.Write(handshake(servId, 1500))
	check(err)

	init := make([]byte, 8)
	_, err = io.ReadFull(conn, init)
	check(err)

    fmt.Println(&quot;is reached&quot;) // doesn&#39;t get printed WHY?
	fmt.Println(init)
}

How the function above is called from main

// vars
var c, startId, servId uint16

// for each servers
for s := 0; s &lt; len(config.Servers); s++ {

	// for each connection
	for c = 0; c &lt; config.Total_clients; c++ {

		startId = config.Server_id * config.Total_clients
		servId = startId + c

		servAddr := fmt.Sprintf(&quot;%s:%d&quot;, config.Servers
展开收缩
.Host, config.Servers
展开收缩
.Port) // create the clients in new go routines go client(servId, servAddr) } }

答案1

得分: 0

你的程序在执行完成之前就会退出,你需要使用sync.WaitGroup或通道来等待所有输出完成后再退出。

func client(wg *sync.WaitGroup, servId uint16, servAddr string) {
	defer wg.Done()
	......
}

func main() {
	var (
		c, startId, servId uint16
		wg                 sync.WaitGroup
	)
	for _, srv := range config.Servers {
		for c = 0; c < config.Total_clients; c++ {
		......
			wg.Add(1)
			go client(&wg, servId, servAddr)
		}
	}
	wg.Wait()
}

请注意,我只翻译了代码部分,其他内容已被忽略。

英文:

Your program will exit before it finishes executing, you need to use sync.WaitGroup or channels to wait for all the output before exiting.

func client(wg *sync.WaitGroup, servId uint16, servAddr string) {
	defer wg.Done()
.....
}

func main() {
	var (
		c, startId, servId uint16
		wg                 sync.WaitGroup
	)
	for _, srv := range config.Servers {
		for c = 0; c &lt; config.Total_clients; c++ {
		......
			wg.Add(1)
			go client(&amp;wg, servId, servAddr)
		}
	}
	wg.Wait()
}

huangapple
  • 本文由 发表于 2015年2月27日 12:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/28757552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定