/// <summary> /// EndReceive paired call. /// </summary> /// <param name="asyncResult">Paired result object from the BeginReceive call.</param> private void EndReceive(IAsyncResult asyncResult) { try { UdpState udpState = (UdpState)asyncResult.AsyncState; Socket socket = udpState.Client.Client; IPEndPoint ipEndPoint = udpState.IPEndPoint; if (socket != null) { int bytes = socket.EndReceive(asyncResult); if (bytes > 0) { OnDataReceived(new UdpDataReceivedEventArgs(ipEndPoint, mDataBuffer)); } if (mAcceptingConnections) { mUdpClient.Client.BeginReceive(mDataBuffer, 0, mDataBuffer.Length, SocketFlags.None, mAsynCallback, udpState); } } } catch (ObjectDisposedException) { // Suppress error } }
/// <summary> /// Start the Udp server and begin receiving data. /// </summary> public void Start() { IPEndPoint ipEndPoint; switch (mTransmissionType) { case TransmissionType.Unicast: { ipEndPoint = new IPEndPoint(mIPAddress, mPort); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Bind(ipEndPoint); mUdpClient = new UdpClient(); mUdpClient.Client = socket; break; } case TransmissionType.Multicast: { ipEndPoint = new IPEndPoint(IPAddress.Any, mPort); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Bind(ipEndPoint); mUdpClient = new UdpClient(); mUdpClient.Client = socket; mUdpClient.JoinMulticastGroup(mMulticastAddress); break; } case TransmissionType.Broadcast: case TransmissionType.LocalBroadcast: { ipEndPoint = new IPEndPoint(IPAddress.Any, mPort); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Bind(ipEndPoint); mUdpClient = new UdpClient(); mUdpClient.Client = socket; break; } default: throw new Exception(); } UdpState udpState = new UdpState(mUdpClient, ipEndPoint); mAcceptingConnections = true; mUdpClient.BeginReceive(mAsynCallback, udpState); }
/// <summary> /// Start the Udp server and begin receiving data. /// </summary> public void Start() { IPEndPoint ipEndPoint; Console.WriteLine("UdpServer.Start: IPAddress={0}, TransmissionType={1}", IPAddress.ToString(), TransmissionType.ToString()); switch (TransmissionType) { case TransmissionType.Unicast: { ipEndPoint = new IPEndPoint(IPAddress, Port); mUdpClient = new UdpClient(); mUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); mUdpClient.Client.Bind(ipEndPoint); break; } case TransmissionType.Multicast: { ipEndPoint = new IPEndPoint(IPAddress.Any, Port); mUdpClient = new UdpClient(); mUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); mUdpClient.Client.Bind(ipEndPoint); mUdpClient.JoinMulticastGroup(MulticastAddress); break; } case TransmissionType.Broadcast: case TransmissionType.LocalBroadcast: { ipEndPoint = new IPEndPoint(IPAddress.Any, Port); mUdpClient = new UdpClient(); mUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); mUdpClient.Client.Bind(ipEndPoint); break; } default: throw new Exception(); } UdpState udpState = new UdpState(mUdpClient, ipEndPoint); Console.WriteLine("UdpServer.Start: udpState={0}, ipEndPoint={1}", udpState.ToString(), ipEndPoint.ToString()); mAcceptingConnections = true; mUdpClient.BeginReceive(mAsynCallback, udpState); }
/// <summary> /// Start the UDP server and begin receiving data. /// </summary> public void Start() { IPEndPoint ipEndPoint; switch (mTransmissionType) { case TransmissionType.Unicast: ipEndPoint = new IPEndPoint(mIPAddress, mPort); mUdpClient = new UdpClient(ipEndPoint); break; case TransmissionType.Multicast: ipEndPoint = new IPEndPoint(IPAddress.Any, mPort); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Bind(ipEndPoint); mUdpClient = new UdpClient(); mUdpClient.Client = socket; mUdpClient.JoinMulticastGroup(mMulticastAddress); break; case TransmissionType.Broadcast: case TransmissionType.LocalBroadcast: ipEndPoint = new IPEndPoint(IPAddress.Any, mPort); mUdpClient = new UdpClient(ipEndPoint); break; default: throw new Exception(); } UdpState udpState = new UdpState(mUdpClient, ipEndPoint); if (mAcceptingConnections) { #if WINDOWS mUdpClient.BeginReceive(mAsynCallback, udpState); #else mUdpClient.Client.BeginReceive(mDataBuffer, 0, mDataBuffer.Length, SocketFlags.None, mAsynCallback, udpState); #endif } }
/// <summary> /// EndReceive paired call. /// </summary> /// <param name="asyncResult">Paired result object from the BeginReceive call.</param> private void EndReceive(IAsyncResult asyncResult) { try { UdpState udpState = (UdpState)asyncResult.AsyncState; UdpClient udpClient = udpState.Client; IPEndPoint ipEndPoint = udpState.IPEndPoint; byte[] data = udpClient.EndReceive(asyncResult, ref ipEndPoint); if (data != null && data.Length > 0) { OnDataReceived(new UdpDataReceivedEventArgs(ipEndPoint, data)); } if (mAcceptingConnections) { udpClient.BeginReceive(mAsynCallback, udpState); } } catch (ObjectDisposedException) { // Suppress error } }