public void Send() { var msg = Encoding.ASCII.GetBytes(Message); try { var client = CreateSocket(); if (FromEndPoint != null) { client.Bind(FromEndPoint); } client.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result => { try { client.EndSend(result); } catch (Exception ex) { _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); } finally { try { client.Close(); } catch (Exception) { } } }, null); } catch (Exception ex) { _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); } ++SendCount; }
public void Send() { var msg = Encoding.ASCII.GetBytes(Message); var socket = CreateSocket(); if (socket == null) { return; } if (FromEndPoint != null) { try { socket.Bind(FromEndPoint); } catch (Exception ex) { if (EnableDebugLogging) { _logger.ErrorException("Error binding datagram socket", ex); } if (!IgnoreBindFailure) { CloseSocket(socket, false); return; } } } try { socket.BeginSendTo(msg, 0, msg.Length, SocketFlags.None, ToEndPoint, result => { try { socket.EndSend(result); } catch (Exception ex) { if (!IgnoreBindFailure || EnableDebugLogging) { _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); } } finally { CloseSocket(socket, true); } }, null); } catch (Exception ex) { _logger.ErrorException("Error sending Datagram to {0} from {1}: " + Message, ex, ToEndPoint, FromEndPoint == null ? "" : FromEndPoint.ToString()); CloseSocket(socket, false); } }