你在这里做错了什么?Golang

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

What am i doing wrong here? Golang

问题

当我尝试连接到Linux机器上的php-fpm.service时,遇到了一个问题,该服务监听端口9000,服务正常工作,以下是我的代码:

package main

import (
	"log"
	"net"
)

func main() {
	listener, err := net.Listen("unix", "127.0.0.1:9000")

	if err != nil {
		log.Fatal("连接错误:", err)
	}

	for {
		fd, err := listener.Accept()

		if err != nil {
			log.Fatal("接受错误:", err)
		}

		log.Print("a")
	}
}

问题是,在这行代码fd, _ := listener.Accept()之后,不再发生任何事情,后面的代码不再执行。如你在上面的代码中所看到的,我有这行代码log.Print("a"),但是该文本从未显示在控制台上。我做错了什么?希望你能帮助我,提前感谢。

英文:

I have a problem when I try to connect to php-fpm.service on a Linux machine, the service listens on port 9000, the service works perfectly and this is my code:

package main

import (
	"log"
	"net"
)

func main() {
	listener, err := net.Listen("unix", "127.0.0.1:9000")

	if err != nil {
		log.Fatal("Connection error: ", err)
	}

	for {
		fd, err := listener.Accept()

		if err != nil {
			log.Fatal("Accept error: ", err)
		}

		log.Print("a")
	}
}

The bad thing is that after this line of code: fd, _ := listener.Accept() nothing happens anymore, the code that follows is no longer executed, as you can see in the code above I have this line log.Print("a") but that text is never displayed on the console. What am I doing wrong? I hope you can help me, thanks in advance.

答案1

得分: 2

你的问题可能是你正在创建一个名为"127.0.0.1:9000"的Unix套接字。该地址可能意味着你想要使用tcp或udp作为地址类型。

listener, err := net.Listen("tcp", "127.0.0.1:9000")

通过这个更改,我能够连接到服务器并看到你的日志消息。

英文:

Your issue is likely that you're creating a unix socket with the name "127.0.0.1:9000". The address likely means you want either tcp or udp as the address type.

listener, err := net.Listen("tcp", "127.0.0.1:9000")

With this change, I'm able to connect to the server and see your log message.

huangapple
  • 本文由 发表于 2022年9月16日 09:26:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/73738917.html
匿名

发表评论

匿名网友

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

确定