Skip to content

Commit

Permalink
Send all data to a socket before exit from Socket.Send.
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjarobot committed Sep 21, 2016
1 parent 3319d6a commit a1b1a7c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mcs/class/System/System.Net.Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2457,18 +2457,19 @@ int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out Soc
}

int nativeError;
int ret = Send_internal (safe_handle, buf, offset, size, flags, out nativeError);

error = (SocketError)nativeError;

if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
is_connected = false;
is_bound = false;
} else {
is_connected = true;
}
int sent = 0;
do {
sent += Send_internal (safe_handle, buf, offset + sent, size - sent, flags, out nativeError);
error = (SocketError)nativeError;
if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
is_connected = false;
is_bound = false;
} else {
is_connected = true;
}
} while (sent < size);

return ret;
return sent;
}

public bool SendAsync (SocketAsyncEventArgs e)
Expand Down
61 changes: 61 additions & 0 deletions mcs/class/System/Test/System.Net.Sockets/SocketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,67 @@ public void SendGeneric ()
listensock.Close ();
}

[Test]
public void ConcurrentExceedSocketLimit ()
{
var tasks = new Task[4];
for (int i = 0; i < 4; i++) {
tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
}
Task.WaitAll (tasks);
}

[Test]
public void SendGenericExceedBuffer ()
{
// Create a buffer larger than the default max.
const int BUFFER_SIZE = 256 * 256 * 65;
int i;

IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());

Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listensock.Bind (endpoint);
listensock.Listen (1);

Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sendsock.Connect (endpoint);

Socket clientsock = listensock.Accept ();

byte[] sendbuf = new byte[BUFFER_SIZE];

for (i = 0; i < BUFFER_SIZE; i++) {
sendbuf[i] = (byte)i;
}

SocketError err;
int sent = sendsock.Send (sendbuf);

Assert.AreEqual (BUFFER_SIZE, sent, "#1");

byte[] recvbuf = new byte[BUFFER_SIZE];

int totalReceived = 0;
byte[] buffer = new byte[256];
while (totalReceived < sendbuf.Length) {
int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
buffer.CopyTo (recvbuf, totalReceived);
totalReceived += recvd;
}

Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");

for (i = 0; i < BUFFER_SIZE; i++) {
Assert.AreEqual (recvbuf[i], sendbuf[i],
"#3/" + i.ToString());
}

sendsock.Close ();
clientsock.Close ();
listensock.Close ();
}

[Test]
public void ListenNotBound ()
{
Expand Down

0 comments on commit a1b1a7c

Please sign in to comment.