using System;
using System.Net.Sockets;
namespace SuperSocket.Common
{
///
/// Socket extension class
///
public static class SocketEx
{
///
/// Close the socket safely.
///
/// The socket.
public static void SafeClose(this Socket socket)
{
if (socket == null)
return;
if (!socket.Connected)
return;
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch
{
}
try
{
socket.Close();
}
catch
{
}
}
///
/// Sends the data.
///
/// The client.
/// The data.
public static void SendData(this Socket client, byte[] data)
{
SendData(client, data, 0, data.Length);
}
///
/// Sends the data.
///
/// The client.
/// The data.
/// The offset.
/// The length.
public static void SendData(this Socket client, byte[] data, int offset, int length)
{
int sent = 0;
int thisSent = 0;
while ((length - sent) > 0)
{
thisSent = client.Send(data, offset + sent, length - sent, SocketFlags.None);
sent += thisSent;
}
}
}
}