Skip to content

Support udp reconnection #263

@guidao

Description

@guidao

For a "connected UDP socket", write will return an error when the udp port is unreachable, so it might be possible to perform a reconnection based on this error.

package main

import (
        "fmt"
        "net"
        "time"
)

func main() {
        conn, err := net.Dial("udp", "127.0.0.1:9999")
        if err != nil {
                panic(err)
        }
        for {
                n, err := conn.Write([]byte("xxx.yyy.zzz.count:1|c"))
                if err != nil {
                        fmt.Println("conn write failed:", err)
                } else {
                        fmt.Println("conn write byte:", n)
                }
                time.Sleep(time.Second)
        }
}

result:

conn write byte: 21
conn write failed: write udp 127.0.0.1:62924->127.0.0.1:9999: write: connection refused
conn write byte: 21
conn write failed: write udp 127.0.0.1:62924->127.0.0.1:9999: write: connection refused
conn write byte: 21

This feature is useful when the dns changes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions