Created
April 25, 2017 00:47
-
-
Save mushfiqweb/6c0c7f55ae3e8c5b599d1e1ef88c07b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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