C#:带有SocketAsyncEventArgs的SSL? [英] C#: SSL with SocketAsyncEventArgs?

查看:56
本文介绍了C#:带有SocketAsyncEventArgs的SSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET开发套接字服务器.我使用的是SocketAsyncEventArgs类提供的异步模型,因为它必须是高性能服务器,才能在短时间内支持许多连接.接下来,我要确保之间的通信客户端和服务器,我想我可以使用SSL.

I'm developing a socket server using C# .NET. I'm using the async model provided by the SocketAsyncEventArgs class, because it must be a high performance server to support many connections in short periods of time. Next, I want to secure the communication between clients and server, and I think I could use SSL.

有什么方法可以将SSL与SocketAsyncEventArgs模型一起使用?我知道.NET具有用于SSL安全的SslStream类,但是我需要使用SocketAsyncEventArgs来实现高性能.

Is there any way of using SSL with the SocketAsyncEventArgs model? I know .NET has the SslStream class for SSL securing, but I need to use SocketAsyncEventArgs for high performance.

是否可以在不使用服务器代码中实现SSL的情况下在更高级别使用SSL?

Is it possible to use SSL in an upper level, without implementing it in the server code?

谢谢.

推荐答案

不知道是否有人在乎,因为这已经很老了,但我本周只需要这样做,在互联网上找不到任何满足我需要的东西.也许框架中有一些新东西无法做到这一点……无论如何,我会发布源代码,但是由于我为公司编写了源代码,而他们对此并不满意,所以我只概述一下我的方法.拍摄了:

Not sure if anyone cares anymore since this is so old but I needed to do just that this week and could not find anything on the internet that met my needs. Maybe there is something new in the framework that does this that I was unable find... Regardless, I would post source code but since I wrote it for my company and they tend to frown on that, I'll just outline the approach I took:

由于SslStream在构造函数中接收流,所以我实现了自己的Stream子类型,并使用了一个用于读取的底层MemoryStream和用于写入的另一个MemoryStream.我也将TcpClient传递给该对象.

Since SslStream takes a stream in the constructor, I implemented my own Stream subtype with an underlying MemoryStream for reads and another for writes. I also pass in the TcpClient to this object as well.

我使用TcpClient进行握手以建立SSL连接.根据我的使用方式对服务器或客户端进行身份验证之后,然后将两个MemoryStream用于SslStream读/写.

I used the TcpClient to do the handshake for setting up the SSL connection. After authenticating the server or client depending on how I am using it, I then use my two MemoryStreams for the SslStream read/writes.

因此,对于异步写入,我首先将有效负载写入SslStream,后者填充用于加密数据写入的MemoryStream.使用来自MemoryStream的加密数据,我填充SocketAsyncEventArgs缓冲区并调用TcpClient SendAsync方法.对于阅读而言,情况恰恰相反.

So for Async writes, I first write my payload to the SslStream which populates my MemoryStream for writing with encrypted data. With the encrypted data from the MemoryStream, I populate the SocketAsyncEventArgs buffer and call the TcpClient SendAsync method. For reads, it's pretty much the opposite.

我不能说这特别使我兴奋地像这样移动数据,但是只要您不让您的MemoryBuffer对象不断地重新分配,这不是性能问题.至少通过这种方式,我可以仅使用框架和自己的代码,而无需依赖第三方软件.

I can't say it particular excites me to move the data like that but as long as you don't let your MemoryBuffer objects get reallocated constantly, it's not a performance issue. At least this way, I can use just the framework and my own code without relying on third party software.

这篇关于C#:带有SocketAsyncEventArgs的SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆