Don't throw in EndReceive/EndSend + out error
[mono.git] / mcs / class / System / System.Net.Sockets / Socket_2_1.cs
index 78bfb95cf0ce346bd3960caba2d4e57a1418b5e8..c342bb7f7bf8edd4e0fc0d4dd6ba04c9143de7e1 100644 (file)
@@ -1687,7 +1687,10 @@ namespace System.Net.Sockets {
                int EndReceive (IAsyncResult result)
                {
                        SocketError error;
-                       return (EndReceive (result, out error));
+                       int bytesReceived = EndReceive (result, out error);
+                       if (error != SocketError.Success)
+                               throw new SocketException ((int)error);
+                       return bytesReceived;
                }
 
 #if !MOONLIGHT
@@ -1713,7 +1716,11 @@ namespace System.Net.Sockets {
                                asyncResult.AsyncWaitHandle.WaitOne ();
 
                        errorCode = req.ErrorCode;
-                       req.CheckIfThrowDelayedException ();
+                       // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
+                       // kinds of exceptions that should be thrown.
+                       if (errorCode == SocketError.Success)
+                               req.CheckIfThrowDelayedException();
+
                        return(req.Total);
                }
 
@@ -1725,7 +1732,10 @@ namespace System.Net.Sockets {
                int EndSend (IAsyncResult result)
                {
                        SocketError error;
-                       return(EndSend (result, out error));
+                       int bytesSent = EndSend (result, out error);
+                       if (error != SocketError.Success)
+                               throw new SocketException ((int)error);
+                       return bytesSent;
                }
 
 #if !MOONLIGHT
@@ -1750,7 +1760,11 @@ namespace System.Net.Sockets {
                                asyncResult.AsyncWaitHandle.WaitOne ();
 
                        errorCode = req.ErrorCode;
-                       req.CheckIfThrowDelayedException ();
+                       // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
+                       // kinds of exceptions that should be thrown.
+                       if (errorCode == SocketError.Success)
+                               req.CheckIfThrowDelayedException ();
+
                        return(req.Total);
                }