读取 udp [::]:4604: 使用已关闭的网络连接
read udp [::]:4604: use of closed network connection
我在 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 服务器上工作。在后台,服务器侦听 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);
}
}
问题出在客户端。