private void BindSocketsDerivative(IPAddress objInterfaceIpAddr, IPAddress objBroadcastIpAddrDer, int port) { try { InterfaceIpDer = objInterfaceIpAddr; BroadCastIpDer = objBroadcastIpAddrDer; BroadCastPortDer = port; //sw = File.AppendText("SuhasOrders.txt"); //sw.AutoFlush = true; if (_clientDer == null) { _clientDer = new UdpClient(); } _clientDer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _clientDer.JoinMulticastGroup(BroadCastIpDer); _clientDer.Client.Bind(new IPEndPoint(InterfaceIpDer, BroadCastPortDer)); UdpState state = new UdpState { WorkSocket = _clientDer }; _clientDer.BeginReceive(DataReceivedDer, state); } catch (Exception e) { ExceptionUtility.LogError(e); //MessageBox.Show(e.Message); } }
private void BindSocketsCurrency(IPAddress objInterfaceIpAddr, IPAddress objBroadcastIpAddrCurr, int port) { #region Commented //try //{ // InterfaceIpCurr = objInterfaceIpAddr; // BroadCastIpCurr = objBroadcastIpAddrCurr; // BroadCastPortCurr = port; // //sw = File.AppendText("SuhasOrders.txt"); // //sw.AutoFlush = true; // if (_clientCurr == null) // _clientCurr = new UdpClient(); // _clientCurr.JoinMulticastGroup(BroadCastIpCurr); // _clientCurr.Client.Bind(new IPEndPoint(InterfaceIpCurr, BroadCastPortCurr)); // UdpState state = new UdpState { WorkSocket = _clientCurr }; // _clientCurr.BeginReceive(DataReceivedCurr, state); //} //catch (Exception e) //{ // ExceptionUtility.LogError(e); // //MessageBox.Show(e.Message); //} #endregion try { InterfaceIpCurr = objInterfaceIpAddr; BroadCastIpCurr = objBroadcastIpAddrCurr; BroadCastPortCurr = port; //sw = File.AppendText("SuhasOrders.txt"); //sw.AutoFlush = true; if (_clientCurr == null) { _clientCurr = new UdpClient(); } _clientCurr.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _clientCurr.JoinMulticastGroup(BroadCastIpCurr); _clientCurr.Client.Bind(new IPEndPoint(InterfaceIpCurr, BroadCastPortCurr)); UdpState state = new UdpState { WorkSocket = _clientCurr }; _clientCurr.BeginReceive(DataReceivedCurr, state); } catch (Exception e) { ExceptionUtility.LogError(e); //MessageBox.Show(e.Message); } }
private void DataReceived(IAsyncResult ar) { try { // tBcast.Stop(); UdpClient c = ((UdpState)ar.AsyncState).WorkSocket; IPEndPoint receivedIpEndPoint = new IPEndPoint(BroadCastIp, 0); Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint); Int32 msgType = Swap32(BitConverter.ToInt32(receiveBytes, 0)); switch (msgType) { case 2020: try { if (Constants._sessionId >= Constants.MemberQuerySession) { return; } //Commented as this causes duplication in recieving the data at the same millisecond // ThreadPool.QueueUserWorkItem(UpdateScripTicker, receiveBytes); // Common objCommon = new Common(); UpdateScripTicker(receiveBytes); } catch (Exception e) { ExceptionUtility.LogError(e); Console.WriteLine(e); } break; } UdpState state = new UdpState { WorkSocket = c }; c.BeginReceive(DataReceived, state); } catch (Exception ex) { ExceptionUtility.LogError(ex); Console.WriteLine(ex.Message); } }