I'm writing a program to fetch a udp server. When i first fetch the server it works.
我正在编写一个程序来获取UDP服务器。当我第一次获取服务器时,它可以工作。
But when i input the program second time. This gives me this error
但当我第二次输入程序时。这给了我这个错误
panic: Listen failed:dial udp :8829->:9781: bind: address already in use
Code:
代码:
package main
import (
"fmt"
"log"
"net"
"os"
)
func main() {
log.SetFlags(log.Lshortfile)
udpServer, err := net.ResolveUDPAddr("udp", ":9781")
if err != nil {
panic(fmt.Sprint("ResolveUDPAddr failed:", err.Error()))
os.Exit(1)
}
client, err := net.ResolveUDPAddr("udp", ":8829")
if err != nil {
panic(err)
}
for {
fmt.Printf(">> ")
var input string
fmt.Scanf("%s", &input)
conn, err := net.DialUDP("udp", client, udpServer)
if err != nil {
panic(fmt.Sprint("Listen failed:", err.Error()))
os.Exit(1)
}
_, err = conn.Write([]byte(input))
if err != nil {
panic(fmt.Sprint("Write data failed:", err.Error()))
os.Exit(1)
}
received := make([]byte, 1024)
_, err = conn.Read(received)
if err != nil {
panic(fmt.Sprint("Read data failed:", err.Error()))
os.Exit(1)
}
fmt.Printf("Response: %s\n", string(received))
}
}
I think this happens because i'm using the client twice.
我认为发生这种情况是因为我在使用客户端两次。
But why this causes "address already in use"
但是为什么这会导致“地址已在使用”呢?
I'm not re-creating the client.
我不会重新创建客户的。
Solutions I Tried
我尝试过的解决方案
One of the solution could be set client as nil.
其中一个解决方案可以将客户端设置为空。
Setting to nil will generate a random port.
设置为nil将生成随机端口。
But in my case i need fixed port and fixed network address.
但在我的情况下,我需要固定的端口和固定的网络地址。
So that would not be the solution.
因此,这不是解决方案。
更多回答
But in my case i need fixed port and fixed network address.
Why?
但在我的情况下,我需要固定的端口和固定的网络地址。为什么?
"But when i input the program second time. " - what do you mean with this: run the same program again in parallel to an existing instance or run it after the previous program has exited?
“但当我第二次输入程序时。”-你这样做是什么意思:在现有实例的同时再次运行相同的程序,或者在前一个程序退出后运行它?
You are recreating the connection every loop. Create a single connection and reuse it
您将在每个循环中重新创建连接。创建单个连接并重新使用它
@tkausl Because my server tracks the clients address
@tkaul,因为我的服务器跟踪客户端地址
@SteffenUllrich I mean when i fetch the server second time.
@SteffenUllrich我是指当我第二次获取服务器时。
Just create the connection before the loop:
只需在循环之前创建连接:
package main
import (
"fmt"
"log"
"net"
"os"
)
func main() {
log.SetFlags(log.Lshortfile)
udpServer, err := net.ResolveUDPAddr("udp", ":9781")
if err != nil {
panic(fmt.Sprint("ResolveUDPAddr failed:", err.Error()))
os.Exit(1)
}
client, err := net.ResolveUDPAddr("udp", ":8829")
if err != nil {
panic(err)
}
conn, err := net.DialUDP("udp", client, udpServer)
if err != nil {
panic(fmt.Sprint("Listen failed:", err.Error()))
os.Exit(1)
}
for {
fmt.Printf(">> ")
var input string
fmt.Scanf("%s", &input)
_, err = conn.Write([]byte(input))
if err != nil {
panic(fmt.Sprint("Write data failed:", err.Error()))
os.Exit(1)
}
received := make([]byte, 1024)
_, err = conn.Read(received)
if err != nil {
panic(fmt.Sprint("Read data failed:", err.Error()))
os.Exit(1)
}
fmt.Printf("Response: %s\n", string(received))
}
}
更多回答
我是一名优秀的程序员,十分优秀!