Using SslStream with IOCP

走远了吗. 提交于 2019-12-02 09:36:19

问题


I have written a TCP server using the Socket class's asynchronous/IOCP methods, BeginSend()/BeginRead()/etc. I would like to add SSL capability using SslStream, but from the interface it looks like Socket and SslStream are not intended to work together, in particular because I'm not using Streams at all and SslStream appears to depend on having a Stream to work with.

Is this possible, or am I looking in the wrong place? Do I need to fashion my own Stream subclass that feeds into my Socket instances and point SslStream at that? It's important to me that my server use IOCP due to scaling concerns.


回答1:


Wrap your Socket in a NetworkStream to use it with an SslStream.

Socket socket;
...
using (var networkStream = new NetworkStream(socket, true))
using (var sslStream = new SslStream(networkStream, ...))
{
   // use sslStream.BeginRead/BeginWrite here
}

Streams expose BeginRead/BeginWrite methods as well, so you don't loose this aspect.



来源:https://stackoverflow.com/questions/1262324/using-sslstream-with-iocp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!