C# 套接字发送与 BeginSend 性能

发布于 2024-11-08 18:51:37 字数 688 浏览 0 评论 0原文


所以我明白Send是同步的,Be​​ginSend是异步的。我听说套接字发送方法的数据传输速度比 BeginSend 更快(注意:我不是在谈论 CPU 性能,而只是在谈论发送和接收数据包延迟)。谁能告诉我这是真的还是假的?我所指的文章是我听说同步数据传输的地方,如下所示...... 如果这是真的,有没有办法解决?或者这就是使用异步数据传输时的情况?

非常感谢你的帮助。

文章正文:

这两种通信形式都是传输数据的方式。区别在于传输数据的格式。 异步通信是 PC 通信中最广泛使用的通信方法,通常用于电子邮件应用程序、Internet 访问和异步 PC 到 PC 通信。通过异步通信,数据一次传输一个字节,每个字节包含 1 个起始位、8 个数据位和 1 个停止位,总共 10 位。对于异步通信,会产生大量开销,因为发送的每个字节都包含两个额外的位(起始位和停止位),因此会严重降低性能。

同步通信是更有效的通信方法。 CQ 的连接解决方​​案通过同步通信方法进行通信。 通过同步通信,数据作为大数据块的帧而不是庞大的单个字节进行传输。同步的优点之一是可以轻松地将控制信息插入到每个块的开头和结尾,以确保恒定的定时或同步。同步的另一个优点是它比异步更高效。例如,56 Kbps 拨号同步线路每秒可以传送 7000 字节 (56000/8),而 56 Kbps 拨号异步线路每秒只能传送 5600 字节 (56000/10)。当传输大量信息时,这意味着速度和性能的显着提高。

So I understand that Send is Synchronous and BeginSend is Asynchronous. I've heard that the Sockets Send Method is faster for data transfer then BeginSend(Note: I am not talking about CPU Performance but only Send and Recieve packet latency). Can anyone tell me if this is true or not? The article I am referring to where I heard Synchronous Data Transfer goes like this down below....
If it is true, is there a way around it? Or is that just how it is going to be when using Asynchronous Data Transfer?

Thanks very much for you help.

Article Text:

Both of these forms of communication are a means of transmitting data. The difference is in the format that the data is transmitted.
Asynchronous communications is the method of communications most widely used for PC communication and is commonly used for e-mail applications, Internet access, and asynchronous PC-to-PC communications. Through asynchronous communications, data is transmitted one byte at a time with each byte containing one start bit, eight data bits, and one stop bit, thus yielding a total of ten bits. With asynchronous communications, there is a high amount of overhead because every byte sent contains two extra bits (the start and stop bits) and therefore a substantial loss of performance.

Synchronous communications is the more efficient method of communications. CQ's connectivity solutions communicate through the synchronous method of communications.
Through synchronous communications, data is transmitted as frames of large data blocks rather than bulky individual bytes. One advantage of synchronous is that control information is easily inserted at the beginning and end of each block to ensure constant timing, or synchronization. Another advantage of synchronous is that it is more efficient than asynchronous. For example, a 56 Kbps dial-up synchronous line can carry 7000 bytes per second (56000/8) compared to a 56 Kbps dial-up asynchronous line which can only carry 5600 bytes per second (56000/10). When transmitting large amounts of information, this translates into a significant increase in speed and performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞别 2024-11-15 18:51:37

我听说套接字发送方法的数据传输速度比 BeginSend 更快

事实并非如此。

至于文章正文。你在谈论两件截然不同的事情。计算机程序中的异步网络连接和异步方法。

I've heard that the Sockets Send Method is faster for data transfer then BeginSend

Not true.

As for the article text. You are talking about two very different things. An asynchronous network connection and asynchronous methods in a computer program.

别在捏我脸啦 2024-11-15 18:51:37

这当然不是真的。本文讨论了网络连接类型上下文中的同步和异步。然而,它与如何使用套接字发送数据无关。在.Net中,同步和异步只是意味着方法如何阻塞(同步)和不阻塞(异步),与数据传输速度无关。使用的任何同步和异步方法的延迟都是相同的。

It is certainly not true. The article talks about Synchronous and Asynchronous in the context of Network Connection Types. However it has nothing to do with how data is sent using Sockets. In .Net, Synchronous and Asynchronous simply means how methods will block(Synchronous) and not-block(Asynchronous) and has nothing to do with data transfer speeds. The latency is the same across any Synchronous and Asynchronous method used.

拿命拼未来 2024-11-15 18:51:37

我想说的最重要的事情是,同步套接字方法更容易编程,并且可以在单个包装函数中完成。 (我假设您将从 Socket 类继承并添加自定义错误处理、日志记录等以包装发送和接收)

在异步的情况下,您将需要至少两个函数和另一个类或结构(StateObject)跟踪您打算传输的内容。

您当然可以使用匿名委托技术来掩盖回调方法的存在,但这并不能改变您的代码会更难以理解的事实
(并维护)

我无法准确地计算函数的时间,但我发现对于较小的字节大小(<4K),同步似乎更快,而对于较大的块(>8K),异步似乎更好。也许,当您使用 ASync 接收更大的缓冲区时,Async func 会继续前进,并在您咀嚼先前接收到的块时为您提供更多字节。

The most crucial thing I'd say is that Synchronous Socket methods are lot easier to program and can be accomplished in a single wrapper function. (I am assuming that you will inherit from Socket class and add your custom error-handling, logging etc to wrap around Send and Receive)

In case of Asynchronous, you will need at least two functions and another class or struct (a StateObject) to keep track of what you intended to transmit.

You may certainly mask existence of a callback method using anonymous delegate technique but that does not change the fact that your code will be little bit more complicated to understand
(and maintain)

I have not been able to accurately time functions but I've seen that for smaller byte sizes (<4K), synchronous seem to be faster and for larger blocks (> 8K), asynchronous seem to be better. Perhaps, when you use ASync to receive larger buffers, Async func has gone ahead and gotten few more bytes fer you while you were chomping up previously received chunk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文