Skip to content

Instantly share code, notes, and snippets.

@clausjoergensen
Created January 10, 2012 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clausjoergensen/1588116 to your computer and use it in GitHub Desktop.
Save clausjoergensen/1588116 to your computer and use it in GitHub Desktop.
int startTickCount = Environment.TickCount;
int timeout = 10000;
var dataSent = false;
var socket = tcpClient.Client;
while (dataSent == false)
{
if (Environment.TickCount > startTickCount + timeout)
{
throw new Exception("Timeout.");
}
try
{
socket.Send(data, 0, data.Length, SocketFlags.None);
dataSent = true;
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.WouldBlock ||
e.SocketErrorCode == SocketError.IOPending ||
e.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
{
Thread.Sleep(30);
}
else
{
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment