非阻塞套接字与BeginXXX对比SocketAsyncEventArgs

时间:2021-10-31 22:29:16

Can anyone please enlighten me about current .NET socket techniques?

任何人都可以请教我当前的.NET套接字技术吗?

  1. Non-blocking sockets

    非阻塞套接字

    If I set Socket.Blocking = false and use async operations - what will happen?

    如果我设置Socket.Blocking = false并使用异步操作 - 会发生什么?

    Is there any method of polling multiple non-blocking sockets instead of checking them for availability one-by-one (something like trivial select() or any other mechanism, some IOCP-related may be) aside from Socket.Select()?

    是否有任何轮询多个非阻塞套接字的方法,而不是逐个检查它们的可用性(类似于普通的select()或任何其他机制,一些与IOCP相关的机制)除了Socket.Select()?

  2. BeginXXX and SocketAsyncEventArgs

    BeginXXX和SocketAsyncEventArgs

    Are they operating on blocking sockets under the hood and just hide thread creation?

    它们是否在引擎盖下阻塞套接字并隐藏线程创建?

    Will manual creation of threads be equal to using BeginXXX methods?

    手动创建线程是否等于使用BeginXXX方法?

    Is there any other pros on using SocketAsyncEventArgs other then it allows to create pool of sockets and everything related to them?

    有没有其他专业人士使用SocketAsyncEventArgs,那么它是否允许创建套接字池以及与它们相关的所有内容?

And one final question: if app is working as some kind of heavily loaded binary proxy with most logic done in single thread - what provides better scalability: non-blocking approach or async operations?

最后一个问题:如果应用程序作为某种负载很重的二进制代理工作,大多数逻辑都是在单线程中完成的 - 那么提供更好的可伸缩性:非阻塞方法还是异步操作?

1 个解决方案

#1


2  

1: Socket.Select should do that, although I don't tend to use that approach personally; in particular those IList get annoying at high volumes

1:Socket.Select应该这样做,虽然我不倾向于亲自使用这种方法;特别是那些IList在大批量上令人讨厌

2: no, other way around; the blocking operations are essentially using the non-blocking in the background, but with gates. No, they don't create threads under the hood - unless you count the callback when something is inbound. I have an example here that is serving 12k connections using SocketAsyncEventArgs - the thread count is something like 20. Among the intentions of SocketAsyncEventArgs is that:

2:不,反过来;阻塞操作基本上是在后台使用非阻塞,但是使用门。不,他们不会在引擎盖下创建线程 - 除非你在入站时计算回调。我在这里有一个使用SocketAsyncEventArgs服务12k连接的例子 - 线程数类似于20. SocketAsyncEventArgs的意图是:

  • it is far easier to pool effectively, without having lots of objects created/collected per operation
  • 如果没有为每个操作创建/收集大量对象,则可以更有效地进行池化
  • you can handle the "data is available now" scenario very efficiently without needing a callback at all (if the method returns false, you are meant to process the data immediately - no callback will be forthcoming)
  • 你可以非常高效地处理“数据现在可用”的场景,而根本不需要回调(如果方法返回false,则意味着立即处理数据 - 不会有回调)

For scalability: async

对于可伸缩性:async

#1


2  

1: Socket.Select should do that, although I don't tend to use that approach personally; in particular those IList get annoying at high volumes

1:Socket.Select应该这样做,虽然我不倾向于亲自使用这种方法;特别是那些IList在大批量上令人讨厌

2: no, other way around; the blocking operations are essentially using the non-blocking in the background, but with gates. No, they don't create threads under the hood - unless you count the callback when something is inbound. I have an example here that is serving 12k connections using SocketAsyncEventArgs - the thread count is something like 20. Among the intentions of SocketAsyncEventArgs is that:

2:不,反过来;阻塞操作基本上是在后台使用非阻塞,但是使用门。不,他们不会在引擎盖下创建线程 - 除非你在入站时计算回调。我在这里有一个使用SocketAsyncEventArgs服务12k连接的例子 - 线程数类似于20. SocketAsyncEventArgs的意图是:

  • it is far easier to pool effectively, without having lots of objects created/collected per operation
  • 如果没有为每个操作创建/收集大量对象,则可以更有效地进行池化
  • you can handle the "data is available now" scenario very efficiently without needing a callback at all (if the method returns false, you are meant to process the data immediately - no callback will be forthcoming)
  • 你可以非常高效地处理“数据现在可用”的场景,而根本不需要回调(如果方法返回false,则意味着立即处理数据 - 不会有回调)

For scalability: async

对于可伸缩性:async