C#编写基于.Net IOCP的高性能服务器

无论什么平台,编写支持高并发性的网络服务器,瓶颈往往出在I/O上,目前最高效的是采用Asynchronous I/O模型,Linux平台提供了epoll,Windows平台提供了I/O Completion Port(IO完成端口,即IOCP)。

Windows自winsock2开始就提供了IOCP支持,可以通过C++直接调用API,但对于基于.Net的C#开发,是在.Net Framework2.0开始才引入的,在2.0版本下,最高效的网络服务器是通过异步Socket的一些列Beginxxx,Endxxx方法实现的, 底层就是基于IOCP的。

当.Net Framework升级到2.0 sp1之后,.Net Socket又提供了一种更高效的一些列xxxAsync方法,对底层IOCP实现性能有不少改进,.Net Framework升级到3.5之后更是成熟稳定,微软也开始大力推广。

在实际应用中,证明C#编写基于.Net IOCP的高性能服务器可以支持10000个以上的TCP长连接。但在具体实现过程中需要注意几个问题:
1.SocketAsyncEventArgs和Buffer最好预先分配,并能回收重复利用。
2.一个Socket的Send和Receive最好分别对应一个SocketAsyncEventArgs,因为当一个 SocketAsyncEventArgs被ReceiveAsync调用挂起后,在调用SendAsync时就会出异常。同样不要对一个 SocketAsyncEventArgs在一个异步操作被挂起时再次调用。

参考文章:
http://msdn.microsoft.com/zh-cn/magazine/cc163356.aspx
http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs.aspx

40,928人评论了“C#编写基于.Net IOCP的高性能服务器”

  1. We’re a group of volunteers and starting a brand new scheme in our community.
    Your web site provided us with valuable info to work on. You’ve done an impressive process and
    our entire group will be grateful to you.

  2. Hey there just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading correctly.

    I’m not sure why but I think its a linking issue. I’ve tried it in two different
    browsers and both show the same outcome.

  3. An impressive share! I’ve just forwarded this onto a friend who was doing a little research on this.
    And he actually bought me breakfast simply because I found
    it for him… lol. So allow me to reword this…. Thank
    YOU for the meal!! But yeah, thanks for spending some time
    to discuss this topic here on your internet site.

  4. Hello very nice blog!! Guy .. Beautiful .. Superb ..
    I’ll bookmark your website and take the feeds additionally?

    I am happy to find a lot of helpful information right here in the submit, we need work out extra strategies in this regard, thank you for sharing.

    . . . . .

  5. It is perfect time to make some plans for the future and it’s time to
    be happy. I’ve read this post and if I could I desire to suggest you some interesting
    things or advice. Perhaps you could write next articles referring to this
    article. I want to read more things about it!

  6. I’m really inspired with your writing skills and also
    with the format on your blog. Is that this a paid
    subject or did you customize it your self? Either way stay up the nice high quality writing, it’s uncommon to see a
    great blog like this one these days..

  7. I am not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for wonderful info I was looking for this info for my mission.

  8. An intriguing discussion is worth comment. There’s
    no doubt that that you should write more about this issue,
    it might not be a taboo subject but generally people don’t discuss
    these subjects. To the next! Many thanks!!

  9. May I simply say what a relief to find an individual who genuinely knows what they’re talking about over the internet.

    You definitely know how to bring a problem to light and make it important.
    A lot more people really need to read this and understand
    this side of the story. I was surprised that you’re not more popular
    because you certainly have the gift.

  10. I’m truly enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more enjoyable for me to come here
    and visit more often. Did you hire out a developer to create your theme?
    Excellent work!

  11. I don’t know whether it’s just me or if everybody else experiencing issues with your website.
    It seems like some of the text within your content are running
    off the screen. Can someone else please provide feedback and let me know if this is happening to them too?
    This might be a problem with my web browser because I’ve had this happen previously.
    Cheers

  12. I was wondering if you ever thought of changing the structure of your blog?
    Its very well written; I love what youve got to say.

    But maybe you could a little more in the way of
    content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  13. Wow that was strange. I just wrote an very long comment but after I clicked
    submit my comment didn’t show up. Grrrr… well I’m not writing all
    that over again. Anyways, just wanted to say fantastic blog!

  14. Definitely imagine that which you stated. Your favorite justification appeared
    to be on the web the easiest thing to remember of.
    I say to you, I definitely get irked whilst other folks
    consider worries that they just do not understand about.
    You managed to hit the nail upon the top as well as defined out
    the entire thing with no need side-effects , other folks could take a signal.
    Will probably be back to get more. Thanks

发表评论

电子邮件地址不会被公开。 必填项已用*标注