Skip to content

Instantly share code, notes, and snippets.

@Konard
Created May 9, 2014 06:09
Show Gist options
  • Save Konard/e9cd4e0aab3ef7070024 to your computer and use it in GitHub Desktop.
Save Konard/e9cd4e0aab3ef7070024 to your computer and use it in GitHub Desktop.
Представляет вспомогательную сущность для отправки сообщений по протоколу UDP.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Konard.Helpers
{
/// <summary>
/// Представляет вспомогательную сущность для отправки сообщений по протоколу UDP.
/// </summary>
public class UdpSender : IDisposable
{
private bool _disposed;
private readonly UdpClient _udp;
private readonly IPEndPoint _ipendpoint;
public UdpSender(string ipaddress)
{
_udp = new UdpClient();
_ipendpoint = new IPEndPoint(IPAddress.Parse(ipaddress), 15000);
}
public int Send(string message)
{
// Формирование оправляемого сообщения и его отправка.
return _udp.Send(Encoding.Default.GetBytes(message), message.Length, _ipendpoint);
}
#region Finalizers
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// unsafe disposing here?
}
_udp.Close();
_disposed = true;
}
}
~UdpSender()
{
Dispose(false);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment