Skip to content

Instantly share code, notes, and snippets.

@mushfiqweb
Created April 25, 2017 00:47
Show Gist options
  • Save mushfiqweb/6c0c7f55ae3e8c5b599d1e1ef88c07b6 to your computer and use it in GitHub Desktop.
Save mushfiqweb/6c0c7f55ae3e8c5b599d1e1ef88c07b6 to your computer and use it in GitHub Desktop.
public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
int startTickCount = Environment.TickCount;
int sent = 0; // how many bytes is already sent
do {
if (Environment.TickCount > startTickCount + timeout)
throw new Exception("Timeout.");
try {
sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
// socket buffer is probably full, wait and try again
Thread.Sleep(30);
}
else
throw ex; // any serious error occurr
}
} while (sent < size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment