简体   繁体   English

哪个.Net组件应该用于tcp / ip套接字通信?

[英]Which .Net component should be used for tcp/ip sockets communication?

We need to talk to another system that wants to communicate to us via raw tcp/ip socket communication. 我们需要与另一个希望通过原始tcp / ip套接字通信与我们通信的系统进行通信。 What would be the appropriate .Net object, if any, to use for this? 什么是适当的.Net对象,如果有的话,用于此?

If you know of a 3rd party component, open source component, etc., that you think is better to use, please feel free to mention that as well. 如果您知道您认为最好使用的第三方组件,开源组件等,请随时提及。

I would use TcpClient. 我会使用TcpClient。 It sits on top of Socket and is an abstraction upon Socket. 它位于Socket之上,是Socket的抽象。 TcpClient has GetStream which returns a Stream object. TcpClient有GetStream,它返回一个Stream对象。 Then to read the stream you can simply use a stream reader. 然后,要阅读流,您只需使用流阅读器。

For example: 例如:

tcpClient = new TcpClient(host, port)
                            {
                                ReceiveTimeout = readTimeout
                            };
            stream = tcpClient.GetStream();
            streamReader = new StreamReader(stream, Encoding.ASCII);
            streamWriter = new StreamWriter(stream, Encoding.ASCII)
                               {
                                   AutoFlush = true
                               };

            return this;

Which then allows (snippet): 然后允许(片段):

s treamReader.Read(myReadBuffer, 0, myReadBuffer.Length) s treamReader.Read(myReadBuffer, 0, myReadBuffer.Length)

Our company uses Dart Socket.net which basically wraps system.net.sockets.socket into really easy to use methods/components as well as having features of their own. 我们公司使用Dart Socket.net,它基本上将system.net.sockets.socket包装成非常易于使用的方法/组件以及具有自己的功能。 I recommend them for windows form applications. 我推荐它们用于Windows窗体应用程序。 They have lots of examples that come with their demos and have an active support forum that you can post questions and receive support/suggestions from. 他们有许多示例与他们的演示一起提供,并有一个积极的支持论坛,您可以发布问题并获得支持/建议。

Check them out at Dart.com http://www.dart.com/ptsknet.aspx 请访问Dart.com http://www.dart.com/ptsknet.aspx查看它们

The TcpClient is the most common, though I prefer the Socket class myself. TcpClient是最常见的,虽然我更喜欢Socket类。 Either will work. 要么工作。

Socket & TCPClient are both pretty easy to manage and efficient. Socket和TCPClient都非常易于管理和高效。 They'll definitely do what you need. 他们肯定会做你需要的。 There are a variety of good reasons to use the async modes of either. 使用异步模式的原因有多种。

Be warned that there are some threading issues. 请注意,存在一些线程问题。 For instance, some of the async methods will terminate when the calling thread exits. 例如,一些异步方法将在调用线程退出时终止。 There are a lot of very inefficient/bad .NET multi-threaded server examples out there too, so be careful. 还有很多非常低效/糟糕的.NET多线程服务器示例,所以要小心。 One last thing to watch out for is the exception handling. 最后要注意的是异常处理。 Often you just get a "SocketException" back and have to inspect it to figure out what really happened. 通常你只是得到一个“SocketException”,并且必须检查它以找出真正发生的事情。 It would be great if there were a few more derived SocketException classes that could be handled independently. 如果有一些可以独立处理的派生SocketException类,那将会很棒。

But don't let me discourage you, once you get your code implemented and working it'll continue to "just work" from then on. 但是,不要让我沮丧,一旦你的代码得到实施和工作,它将继续“正常工作”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM