登录
首页 >  Golang >  Go问答

读取 udp:4604:使用关闭的网络连接

来源:stackoverflow

时间:2024-04-06 12:27:37 442浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《读取 udp:4604:使用关闭的网络连接》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在开发 udp 服务器。在后台,服务器侦听 actions,这是对客户端发送的数据包的抽象:

go func() {
        for {
            buf := make([]byte, 1024)
            n, addr, err := pc.readfrom(buf)
            clientaddresses[addr] = struct{}{}
            action := &service.action{}
            proto.unmarshal(buf[:n], action)
            actions.insertaction(action)
        }
    }()

当客户端发送消息时,服务器将地址保存在一个集合中,如下所示:

clientaddresses[addr] = struct{}{}

服务器每 500 毫秒处理一次客户端发送的每个操作,并通过迭代地址集将状态更改发送给客户端:

for changes.len() > 0 {
            logwhitetext("sending state change:")
            logpurpletext(changes.getchange())
            change := changes.getchange()
            for addr, _ := range clientaddresses {
                sendchange(change, pc, addr)
            }
            changes.removechange()
        }
// sendchange sends an individual state change to a client
func sendchange(change *service.change, pc net.packetconn, addr net.addr) {
    packet, err := proto.marshal(change)
    if err != nil {
        logredtext(err)
        return
    }
    pc.writeto(packet, addr)
}

服务器启动并且第一个客户端连接后,立即触发以下错误:

2020/10/10 01:39:48  send state changes to the clients
2020/10/10 01:39:48  sending state change:
2020/10/10 01:39:48  locationchange:{coordinate:{lng:12.476284  lat:41.91051}}
2020/10/10 01:39:48  read udp [::]:4604: use of closed network connection
panic: assignment to entry in nil map

我想得到一些关于错误原因的建议。正如错误所示,连接已关闭,但我没有从客户端关闭它:

private UdpClient udpClient = new UdpClient();

    void onStart()
    {

        locationManager.onLocationChanged.AddListener(this.SendChangedLocation);
        try
        {
            udpClient.Connect("localhost", 4604);
            Coordinate coordinate = new Coordinate
            {
                Lng = (float)locationManager.currentLocation.longitude,
                Lat = (float)locationManager.currentLocation.latitude,
            };
            this.CreateNewCar(coordinate);
        }
        catch (Exception exc)
        {
            Debug.LogError(exc);
        }
    }

解决方案


问题出在客户端。

今天关于《读取 udp:4604:使用关闭的网络连接》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>