C# Sockets and SslStreams

前端 未结 1 1602
野的像风
野的像风 2020-12-24 11:21

I\'m confused as to what is \"best\" to use when dealing with sockets. The Socket object provides Send/Receive methods (and async equivalents), but also allows a NetworkStre

相关标签:
1条回答
  • 2020-12-24 11:49

    Rules of thumb:

    • If you're not using a NetworkStream, use the Socket.Send/Socket.Receive methods.
    • If you're using a NetworkStream (which is wrapping the Socket), use the NetworkStream.Read/Write methods but don't call the Socket methods.
    • If you're wrapping a NetworkStream in an SslStream, use the NetworkStream.Read/Write methods before calling AuthenticateAsClient/Server, and the SslStream.Read/Write methods after.

    After AuthenticateAsClient/Server, your connection will be SSL secured. Don't call the Socket or NetworkStream methods then: this will break the SslStream.

    (It's possible to ignore these rules under certain circumstances, but then you need to be really careful and precisely know what Socket, NetworkStream and SslStream do under the hood. If you always use the outermost wrapper, you're on the safe side.)

    0 讨论(0)
提交回复
热议问题