Skip to content

net: udp server error #74313

Closed as not planned
Closed as not planned
@rocket049

Description

@rocket049

Go version

go version go1.24.4 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/fuhz/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/fuhz/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1848721630=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/fuhz/src/udpserver/go.mod'
GOMODCACHE='/home/fuhz/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/fuhz/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/opt/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/fuhz/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I write a udp server to register clients incoming, and send list of clients.

my server main code:

addr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf(":%v", flag.Arg(0)))
	if err != nil {
		panic(err)
	}
	var buffer [1024]byte
	var store = list.New()
	store.Init()
	c, err := net.ListenUDP("udp4", addr)
	if err != nil {
		panic(err)
	}
	defer c.Close()

	for {
		log.Println("listen udp:", c.LocalAddr().String())
		c.SetDeadline(time.Now().Add(time.Second * 30))
		n, raddr, err := c.ReadFrom(buffer[:])
		if n == 0 || err != nil {
			break
		}

		if !contains(store, raddr.String()) {
			store.PushBack(raddr.String())
		}

		log.Println(string(buffer[:n]), raddr.String())
		c.WriteTo([]byte("from="+c.LocalAddr().String()), raddr)
		if string(buffer[:n]) == "list" {
			res := []string{}
			p := store.Front()
			for {
				if p == nil {
					break
				}
				res = append(res, p.Value.(string))
				p = p.Next()
			}
			data := strings.Join(res, ";")
			c.WriteTo([]byte("list="+data), raddr)
			log.Println("list")
		}

	}

my client main code:

	conn, err := net.DialUDP("udp", nil, uaddr)
	if err != nil {
		return err
	}
	once.Do(func() {
		go udpLoop(c)
	})

	n, err := conn.Write([]byte(msg))
	if err != nil {
		return err
	}
	if n == 0 {
		return errors.New("send null")
	}
	return nil
}

func udpLoop(c *net.UDPConn) {

	var buffer [1024]byte
	for {
		n, raddr, err := c.ReadFromUDP(buffer[:])
		if n == 0 || err != nil {
			break
		}
		fmt.Printf("%v:%v", string(buffer[:n]), raddr.String())
	}
}

What did you see happen?

The server only reply the first client.
The message from second client can not be received.

Seriously, after the second client send the message, the server seems to work full speed and blocked, the cpu work 100%, and the deadline time is not work.

What did you expect to see?

The server should reply every client incoming. The CPU load should be very low.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions