golang UDP服务器的奇怪行为 [英] Strange behaviour of golang UDP server

查看:163
本文介绍了golang UDP服务器的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的UDP服务器。



当我执行 go run udp.go 时,它会打印我发送给它的所有包。但是当运行 go run udp.go> out 它停止在客户端停止时将 stdout 传递给 out 文件。



客户端是发送10k个请求的简单程序。所以在文件中我有大约50%的发送包。当我再次运行客户端时, out 文件将再次增长,直到客户端脚本完成。



服务器代码:

 包主

导入(


fmt


func main(){
addr,_:= net.ResolveUDPAddr(udp,:2000)
sock,_ := net.ListenUDP(udp,addr)

i:= 0
for {
+++
buf:= make([] byte,1024)
rlen,_,err:= sock.ReadFromUDP(buf)
if err!= nil {
fmt.Println(err)
}
fmt.Println(string (buf [0:rlen]))
fmt.Println(i)
// go handlePacket(buf,rlen)
}
}

$ p
$ b

以下是客户端代码:

  package main 

import(
net

fmt


func main(){

num:= 0
for i:= 0;我< 100; i ++ {
for j:= 0; j< 100; j ++ {
num ++
con,_:= net.Dial(udp,127.0.0.1:2000)
fmt.Println(num)
buf:= [ ]字节(bla bla bla我是数据包)
_,err:= con.Write(buf)
if err!= nil {
fmt.Println(err)





$ div class =h2_lin>解决方案

正如你怀疑的那样,由于UDP的性质,它看起来像是 UDP数据包丢失。由于UDP是无连接的,因此客户端并不关心服务器是否可用或是否准备好接收数据。所以如果服务器正在忙于处理,它将无法处理下一个传入的数据报。您可以使用 netstat -u (它应该包含UDP数据包丢失信息)进行检查。我遇到了同样的情况,服务器(接收端)无法跟上发送的数据包。



您可以尝试两件事(第二件适用于我以您的示例为例):



调用SetReadBuffer。确保接收套接字具有足够的缓冲区来处理您输入的所有内容。 b
$ b

  sock,_:= net.ListenUDP(udp,addr)
sock.SetReadBuffer(1048576)

在例行程序中执行所有数据包处理。尝试通过确保服务器每秒增加数据报当你希望它可以接收时,不忙于做其他工作。即将处理工作移动到执行例行程序,所以您不会阻挡ReadFromUDP()。

  //使用计数参数重新引入go handlePacket(buf,rlen)
func handlePacket(buf [] byte ,rlen int,count int)
fmt.Println(string(buf [0:rlen]))
fmt.Println(count)
}

...

  go handlePacket(buf ,rlen,i)

最后一个选项:



最后,可能不是您想要的,您在您的客户端进行睡眠,这会降低速度并消除问题。例如

  buf:= [] byte(bla bla bla我是包)
time.Sleep(100 * time.Millisecond)
_,err:= con.Write(buf)


I wrote a simple UDP server in go.

When I do go run udp.go it prints all packages I send to it. But when running go run udp.go > out it stops passing stdout to the out file when the client stops.

The client is simple program that sends 10k requests. So in the file I have around 50% of sent packages. When I run the client again, the out file grows again until the client script finishes.

Server code:

package main

import (
  "net"

  "fmt"
)

func main() {
  addr, _ := net.ResolveUDPAddr("udp", ":2000")
  sock, _ := net.ListenUDP("udp", addr)

  i := 0
  for {
    i++
    buf := make([]byte, 1024)
    rlen, _, err := sock.ReadFromUDP(buf)
    if err != nil {
      fmt.Println(err)
    }
    fmt.Println(string(buf[0:rlen]))
    fmt.Println(i)
    //go handlePacket(buf, rlen)
  }
}

And here is the client code:

package main

import (
  "net"

  "fmt"
)

func main() {

  num := 0
  for i := 0; i < 100; i++ {
    for j := 0; j < 100; j++ {
      num++
      con, _ := net.Dial("udp", "127.0.0.1:2000")
      fmt.Println(num)
      buf := []byte("bla bla bla I am the packet")
      _, err := con.Write(buf)
      if err != nil {
        fmt.Println(err)
      }
    }
  }
}

解决方案

As you suspected, it seems like UDP packet loss due to the nature of UDP. Because UDP is connectionless, the client doesn't care if the server is available or ready to receive data. So if the server is busy processing, it won't be available to handle the next incoming datagram. You can check with netstat -u (which should include UDP packet loss info). I ran into the same thing, in which the server (receive side) could not keep up with the packets sent.

You can try two things (the second worked for me with your example):

Call SetReadBuffer. Ensure the receive socket has enough buffering to handle everything you throw at it.

sock, _ := net.ListenUDP("udp", addr)
sock.SetReadBuffer(1048576)

Do all packet processing in a go routine. Try to increase the datagrams per second by ensuring the server isn't busy doing other work when you want it to be available to receive. i.e. Move the processing work to a go routine, so you don't hold up ReadFromUDP().

//Reintroduce your go handlePacket(buf, rlen) with a count param
func handlePacket(buf []byte, rlen int, count int)
        fmt.Println(string(buf[0:rlen]))
        fmt.Println(count)
}

...

go handlePacket(buf, rlen, i)

One final option:

Lastly, and probably not what you want, you put a sleep in your client which would slow down the rate and would also remove the problem. e.g.

buf := []byte("bla bla bla I am the packet")
time.Sleep(100 * time.Millisecond)
_, err := con.Write(buf)

这篇关于golang UDP服务器的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆