golang tcp套接字在立即写入后不发送消息

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

golang tcp socket does not send message after write immediately

问题

我的GO版本是1.1.1

服务器在连接关闭后仍然接收到消息,但是NoDelay已经设置。

有什么问题吗?

addr, _ := net.ResolveTCPAddr("tcp", "localhost:5432")
conn, err := net.DialTCP("tcp", nil, addr)
defer conn.Close()
if err != nil {
	fmt.Println("连接失败")
	return
}
err = conn.SetNoDelay(true)
if err != nil {
	fmt.Println(err.Error())
}

for {
	var message string
	_, err := fmt.Scanln(&message)
	if err != nil && err.Error() != "unexpected newline" {
		fmt.Println("输入完成", err)
		break
	}

	if message == "" {
		fmt.Println("没有输入,结束")
		break
	}
	// message = fmt.Sprintf("%s\n",message) 
	//fmt.Fprintf(conn, message) // 立即发送,但是后续消息将不再发送
	conn.Write([]byte(message)) // 直到连接关闭才发送
}
英文:

My GO version is 1.1.1

the sever recieved messages after connection close, but NoDelay was setted.

Is there something wrong

addr, _ := net.ResolveTCPAddr("tcp", "localhost:5432")
conn, err := net.DialTCP("tcp", nil, addr)
defer conn.Close()
if err != nil {
	fmt.Println("connect fail")
	return
}
err = conn.SetNoDelay(true)
if err != nil {
	fmt.Println(err.Error())
}

for {
	var message string
	_, err := fmt.Scanln(&message)
	if err != nil && err.Error() != "unexpected newline" {
		fmt.Println("input finished", err)
		break
	}

	if message == "" {
		fmt.Println("no input, end")
		break
	}
	// message = fmt.Sprintf("%s\n",message) 
	//fmt.Fprintf(conn, message) // send immediately but following message won't send any more
	conn.Write([]byte(message)) // won't send until connection close
}

答案1

得分: 7

你的代码似乎没有什么严重的问题,所以我猜测错误可能出在服务器端。

如果你在5432端口上创建一个本地TCP服务器,你可以测试一下。

尝试运行下面的服务器代码,然后对其进行客户端代码测试。它只是将接收到的所有数据回显到标准输出。

package main

import (
	"io"
	"log"
	"net"
	"os"
)

func main() {
	l, err := net.Listen("tcp", "localhost:5432")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()
	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		go func(c net.Conn) {
			defer c.Close()
			io.Copy(os.Stdout, c)
		}(conn)
	}
}

当你按下回车键时,你应该看到发送到客户端的每一行都被打印出来(不包括换行符)。

英文:

There doesn't seem to be anything vitally wrong with your code so I'm guessing the error is on the server end.

If you create a local TCP server on port 5432 you can test this.

Try running the below server code and then test your client code against it. It just echos all received data to stdout.

package main

import (
	"io"
	"log"
	"net"
	"os"
)

func main() {
	l, err := net.Listen("tcp", "localhost:5432")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()
	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		go func(c net.Conn) {
			defer c.Close()
			io.Copy(os.Stdout, c)
		}(conn)
	}
}

You should see each line sent to the client printed (without the newline) as soon as you hit enter.

答案2

得分: 0

问题出在服务器端。

func handleConnection(conn net.Conn) {
// 我没有放入循环中
message, err := bufio.NewReader(conn).ReadString('\n')
}
英文:

the problem is on the server end.

func handleConnection(conn net.Conn) {
// I didn't put it in for loop
message, err := bufio.NewReader(conn).ReadString('\n')
}

huangapple
  • 本文由 发表于 2013年7月29日 12:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/17915952.html
匿名

发表评论

匿名网友

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

确定