This is my first question, and I am not a native speaker of English, so I regret my (possibly) inaccurate English.
I implement the real-time network engine and use the Socket.xxxAsync () method.
I made a server side UDP socket like this.
m_udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_udpSock.Bind(new IPEndPoint(IPAddress.Any, udpPort)); SocketAsyncEventArgs udpRecvArg = new SocketAsyncEventArgs(); udpRecvLoopStart(m_udpSock, udpRecvArg);
(udpPort is known to the client.)
private void udpRecvLoopStart(Socket udpSocket, SocketAsyncEventArgs udpRecvArg) { udpRecvArg.AcceptSocket = udpSocket; byte[] udpRecvBuffer = new byte[NetworkEngineConst.MsgSizeMax]; udpRecvArg.SetBuffer(udpRecvBuffer, 0, udpRecvBuffer.Length); udpRecvArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); udpRecvArg.Completed += new EventHandler<SocketAsyncEventArgs>(udpRecvArg_Completed); udpRecv(udpRecvArg); } private void udpRecv(SocketAsyncEventArgs udpRecvArg) { bool synchronous = false; try { synchronous = !udpRecvArg.AcceptSocket.ReceiveFromAsync(udpRecvArg); } catch (Exception e) { OnIOError("recvUdp()\n" + e.ToString()); return; } if (synchronous) udpRecvArg_Completed(this, udpRecvArg); }
Completed event handler:
void udpRecvArg_Completed(object sender, SocketAsyncEventArgs udpRecvArg) { EndPoint udpEp = udpRecvArg.RemoteEndPoint; string msg = Encoding.UTF8.GetString(udpRecvArg.Buffer, 14, udpRecvArg.BytesTransferred - 14); Debug.WriteLine(udpEp + " " + msg); udpRecv(udpRecvArg); }
(first 14 bytes - message prefix, CRC and sequence number)
And two (or more) clients send a UDP packet to the server. (transfer rate is hundreds per second)
For example, client1 (192.168.0.1►0113) sends "111111111111111111111111111111111", client2 (192.168.0.1/109368) sends "2".
But sometimes (not always) the endpoint is wrong. log looks like this:
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1ל9368 2
192.168.0.1ל9368 2
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1ל9368 1111111111111111111111111111111 <- the wrong part
192.168.0.1ל9368 2
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1ל9368 2
192.168.0.1►0113 1111111111111111111111111111111
192.168.0.1ל9368 2
192.168.0.1ל9368 2
I doubt the batch error, but there was no error in the batch (I confirmed with Wireshark)
This is mistake?
(Add)
I tried
m_udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_udpSock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true); m_udpSock.Bind(new IPEndPoint(IPAddress.Any, udpPort));
and replaced ReceiveFromAsync () with ReceiveMessageFromAsync ().
but SocketAsyncEventArgs.ReceiveMessageFromPacketInfo.Address is null
(Add) Error ReceiveMessageFromAsync () - error.
It is fixed in the .NET framework 4.0.
here
thanks.