You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c8e53ad
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: nanoFramework.System.Net/Sockets/NetworkStream.cs
+19-36Lines changed: 19 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ public class NetworkStream : Stream
22
22
protectedint_socketType;
23
23
24
24
/// <summary>
25
-
/// Internal endpoint ref used for dgram sockets
25
+
/// Internal endpoint ref of the remote peer
26
26
/// </summary>
27
27
protectedEndPoint_remoteEndPoint;
28
28
@@ -39,7 +39,7 @@ public class NetworkStream : Stream
39
39
/// </summary>
40
40
/// <param name="socket">The <see cref="Socket"/> that the <see cref="NetworkStream"/> will use to send and receive data.</param>
41
41
/// <exception cref="ArgumentNullException"><paramref name="socket"/> is <see langword="null"/>.</exception>
42
-
/// <exception cref="IOException"><paramref name="socket"/> is not connected. -or- The <see cref="Socket.SocketType"/> property of <paramref name="socket"/> is not <see cref="SocketType.Stream"/>. -or- <paramref name="socket"/> is in a nonblocking state.</exception>
42
+
/// <exception cref="IOException"><paramref name="socket"/> is not connected. -or- The <see cref="Socket.SocketType"/> property of <paramref name="socket"/> is not <see cref="SocketType.Stream"/>.</exception>
43
43
publicNetworkStream(Socketsocket)
44
44
:this(socket,false)
45
45
{
@@ -54,7 +54,7 @@ public NetworkStream(Socket socket)
54
54
/// <param name="ownsSocket"><see langword="true"/> to indicate that the <see cref="NetworkStream"/> will take ownership of the <see cref="Socket"/>;
55
55
/// otherwise, <see langword="false"/>.</param>
56
56
/// <exception cref="ArgumentNullException"><paramref name="socket"/> is <see langword="null"/>.</exception>
57
-
/// <exception cref="IOException"><paramref name="socket"/> is not connected. -or- The <see cref="Socket.SocketType"/> property of <paramref name="socket"/> is not <see cref="SocketType.Stream"/>. -or- <paramref name="socket"/> is in a nonblocking state.</exception>
57
+
/// <exception cref="IOException"><paramref name="socket"/> is not connected. -or- The <see cref="Socket.SocketType"/> property of <paramref name="socket"/> is not <see cref="SocketType.Stream"/>.</exception>
58
58
publicNetworkStream(Socketsocket,boolownsSocket)
59
59
{
60
60
if(socket==null)thrownewArgumentNullException();
@@ -70,7 +70,15 @@ public NetworkStream(Socket socket, bool ownsSocket)
70
70
71
71
thrownewIOException(errCode.ToString(),e);
72
72
}
73
-
73
+
74
+
// NetworkStream is only valid for stream-oriented sockets (same as .NET).
75
+
// Datagram sockets don't have stream semantics: e.g. a zero-length datagram
76
+
// would make Read return 0 without the peer having shut down the connection.
77
+
if(socket.SocketType!=SocketType.Stream)
78
+
{
79
+
thrownewIOException();
80
+
}
81
+
74
82
// Set the internal socket
75
83
_socket=socket;
76
84
@@ -245,14 +253,14 @@ public override void Flush()
245
253
/// <param name="offset">The location in <paramref name="buffer"/> to begin storing the data to.</param>
246
254
/// <param name="count">The number of bytes to read from the <see cref="NetworkStream"/>.</param>
247
255
/// <returns>The total number of bytes read into the buffer between zero (0) and the requested count. The method returns zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown.</returns>
248
-
/// <exception cref="IOException">The underlying <see cref="Socket"/> is closed.</exception>
256
+
/// <exception cref="IOException">The underlying <see cref="Socket"/> is closed. -or- There was a failure while reading from the network.</exception>
249
257
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
250
-
/// <exception cref="ObjectDisposedException">The <see cref="NetworkStream"/> is closed or there is a failure reading from the network.</exception>"
258
+
/// <exception cref="ObjectDisposedException">The <see cref="NetworkStream"/> is closed.</exception>
251
259
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is less than 0 or greater than the length of <paramref name="buffer"/>. -or- <paramref name="count"/> is less than 0 or greater than the length of <paramref name="buffer"/> minus the value of the <paramref name="offset"/> parameter.</exception>
252
260
/// <remarks>
253
261
/// <para>This method reads data into the <paramref name="buffer"/> parameter and returns the number of bytes successfully read. The <see cref="Read"/> operation reads as much data as is available, up to the number of bytes specified by the <paramref name="count"/> parameter. If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.</para>
254
262
/// <note type="important">
255
-
/// Check to see if the <see cref="NetworkStream"/> is readable by calling the <see cref="CanRead"/> property. If you attempt to read from a <see cref="NetworkStream"/> that is not readable, you will get an <see cref="IOException"/>.
263
+
/// Check to see if the <see cref="NetworkStream"/> is readable by calling the <see cref="CanRead"/> property. If you attempt to read from a <see cref="NetworkStream"/> that is not readable, you will get an <see cref="IOException"/>.
@@ -315,10 +312,9 @@ public override void SetLength(long value)
315
312
/// <param name="offset">The location in <paramref name="buffer"/> from which to start writing data.</param>
316
313
/// <param name="count">The number of bytes to write to the <see cref="NetworkStream"/>.</param>
317
314
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is less than 0 or greater than the length of <paramref name="buffer"/>. -or- <paramref name="count"/> is less than 0 or greater than the length of <paramref name="buffer"/> minus the value of the <paramref name="offset"/> parameter.</exception>
318
-
/// <exception cref="ObjectDisposedException">The <see cref="NetworkStream"/> is closed or there is a failure reading from the network.</exception>
319
-
/// <exception cref="IOException">There was a failure while writing to the network. -or-An error occurred when accessing the socket. See the Remarks section for more information.</exception>
315
+
/// <exception cref="ObjectDisposedException">The <see cref="NetworkStream"/> is closed.</exception>
316
+
/// <exception cref="IOException">There was a failure while writing to the network. -or-An error occurred when accessing the socket. See the Remarks section for more information.</exception>
320
317
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
321
-
/// <exception cref="NotSupportedException">The underlying <see cref="Socket"/> is not of type <see cref="SocketType.Stream"/> or <see cref="SocketType.Dgram"/>.</exception>
322
318
/// <remarks>
323
319
/// The Write method starts at the specified offset and sends count bytes from the contents of buffer to the network.
324
320
/// The Write method blocks until the requested number of bytes is sent or a <see cref="SocketException"/> is thrown.
@@ -333,20 +329,7 @@ public override void Write(byte[] buffer, int offset, int count)
0 commit comments