什么是 .net/c# 套接字编程的好教程/方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/104617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a good tutorial/howto on .net / c# socket programming
提问by Chasler
I'm porting old VB6 code that uses the Winsock control to C#. I haven't done any socket programming and I wonder if anyone has a good reference/tutorial/howto that I can use to start getting up to speed.
我正在将使用 Winsock 控件的旧 VB6 代码移植到 C#。我没有做过任何套接字编程,我想知道是否有人有很好的参考/教程/方法,我可以用它来开始加快速度。
I'm appealing to the hive mind while I proceed with my generally unproductive googling.
当我继续进行通常没有成效的谷歌搜索时,我正在吸引蜂群的思想。
I'm using UDP, not TCP at this time.
我目前使用的是 UDP,而不是 TCP。
采纳答案by jeffm
The August 2005 MSDN Magazine had an article about System.Net.Sockets and WinSock:
2005 年 8 月的 MSDN 杂志有一篇关于 System.Net.Sockets 和 WinSock 的文章:
回答by stephenbayer
Are you working on: a client (TCPClient) or a server (TCPListener)
您正在处理:客户端 ( TCPClient) 还是服务器 ( TCPListener)
回答by Thomas Bratt
- I recommend the asynchronous model for most applications, especially if you want performance or applications that don't hang as soon there is a network problem. For this the MSDN articles on Socket.BeginConnectand Socket.BeginReceiveare good places to start.
- The following link is not .NET, but many of the recommendations still hold: http://tangentsoft.net/wskfaq/articles/lame-list.html
- 我建议大多数应用程序使用异步模型,特别是如果您希望性能或应用程序不会在出现网络问题时立即挂起。为此,有关Socket.BeginConnect和Socket.BeginReceive的 MSDN 文章是不错的起点。
- 以下链接不是.NET,但许多建议仍然有效:http: //tangentsoft.net/wskfaq/articles/lame-list.html
回答by Spencer Ruport
Just a heads up:
只是提个醒:
I would recommend first working with TCP rather than UDP. UDP doesn't automatically redeliver lost packets like TCP so it will add another element to the equation that will probably just confuse you as you're just starting out.
我建议首先使用 TCP 而不是 UDP。UDP 不会像 TCP 那样自动重新传送丢失的数据包,因此它会在等式中添加另一个元素,这可能会使您在刚开始时感到困惑。
Building a socket client is relatively easy using the TCPClient class available in the .Net library. TCPListener is easy enough to use for a single client but if you're hoping to develop some server type application (IE: Handling multiple connections.) the real hurdle you'll have to overcome is understanding multithreading.
使用 .Net 库中提供的 TCPClient 类构建套接字客户端相对容易。TCPListener 很容易用于单个客户端,但如果您希望开发一些服务器类型的应用程序(即:处理多个连接)。您必须克服的真正障碍是理解多线程。
Once you've played around with single connection sockets I suggest you read up on multithreading.
一旦你玩过单连接套接字,我建议你阅读多线程。