显示传递对象的异步套接字示例?

发布于 2024-10-16 03:25:46 字数 317 浏览 4 评论 0原文

我需要创建一个服务器进程,可以将高频数据(每秒 1000 次更新)推送到大约 50 个客户端。我认为最好的方法是使用 SocketAsyncEventArgs 类型的异步套接字。

客户->服务器连接将长时间运行至少几天到无限期。我计划让服务器进程进行监听,客户端连接,服务器开始将数据推送到客户端。

有人可以指出我或向我展示如何执行此操作的示例吗?我找不到任何显示服务器进程将对象推送到客户端的示例。

编辑:这是通过千兆位 LAN 进行的。使用具有 16 核和 24GB RAM 的 Windows 服务器,

谢谢

I need to create a server process which can push high frequency data (1000 updates per second) to around 50 client. I'm thinking the best way you do this is using async sockets with the SocketAsyncEventArgs type.

The client -> server connections will be long running at least several days to indefinite. I plan to have a server process listening and the clients connect and the server starts pushing the data to the clients.

Can someone point me to or show me an example of how to do this? I can't find any example showing a server process pushing an object to a client.

EDIT: This is over a gigibit LAN. Using windows server with 16 cores and 24gb ram

thanks

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-10-23 03:25:46

首先,您还需要提出一些要求。你的服务器很强大,但如果你不做必须做的事情,它就会严重失败。

  1. 客户可以在没有一些数据的情况下生存吗?我的意思是,数据流是否需要以正确的顺序到达另一端,没有任何丢失?

  2. “数据”有多大?几个字节还是?

  3. 事实:Windows 上的调度间隔为 10 毫秒。

  4. 事实:无论您何时发送,客户端都会收到它,具体取决于很多因素 - 网络配置、中间路由器的数量、客户端处理器负载等等。所以你在这里需要某种时间戳

根据这一切,你可以设计一个优先级队列,其中一个线程为其提供服务并为每个客户端发送 UDP 数据报。此外,由于 (4) 有效,您可以将一些数据“聚集”在一起,并每秒对 100 个数据进行 10 次更新。

如果你想实现其他目标,那么这里就需要局域网和大量优质的网络设备。

First, some more requirements from your side is required. You have server with lots of muscle, but it will fail miserably if you don't do what has to be done.

  1. can the client live without some of the data? I mean, does the stream of the data need to reach other side in proper order, without any drops?

  2. how big is 'the data'? few bytes or?

  3. fact: scheduling interval on windows is 10 msec.

  4. fact: no matter WHEN you send, clients will receive it depending on lots of stuff - network config, number of routers in-between, client processor load, and so on. so you need some kind of timestamping here

Depending on all this, you could design a priority queue with one thread servicing it and sending out UDP datagrams for each client. Also, since (4) is in effect, you can 'clump' some of your data together and have 10 updates per second of 100 data.

If you want to achieve something else, then LAN will be required here with lots of quality network equipment.

短叹 2024-10-23 03:25:46

如果您想使用 .NET 套接字创建此服务器-客户端项目,那么这是所需内容的一个很好的概述:

由于服务器将同时将数据传输到多个客户端,因此您需要使用异步 Socket .Beginxxx 方法或 SocketAsyncEventArgs 类。

您将有客户端连接到您的服务器。服务器将接受这些连接,然后将新连接的客户端添加到内部客户端列表中。

您将在服务器内运行一个线程,该线程定期向客户端列表中的所有套接字发送通知。如果在向套接字发送数据时发生任何异常/错误,则该客户端将从列表中删除。

由于服务器是多线程应用程序,因此您必须确保对客户端列表的访问是同步的。

您无需担心缓冲发送数据,因为 TCP 堆栈会处理这件事。如果您根本不想缓冲数据(即让套接字立即发送数据),请设置Socket.NoDelay 为 true。

看起来您不需要来自客户端的任何数据,但如果需要,则必须确保您的服务器在使用 Socket.BeginXXX< 时具有 Socket.BeginReceive 循环/code> 模式或 Socket.ReceiveAsync 方法(如果使用 SocketAsyncEventArgs)。

一旦服务器和客户端之间建立了数据连接和传输,您就需要担心客户端和服务器之间对象的序列化和反序列化。

服务器上发生的序列化很容易,因为您可以使用 BinaryFormatter 或其他编码器对您的对象进行编码并将数据转储到套接字上。

另一方面,发生在客户端的反序列化可能非常复杂,因为一个对象可以跨越多个数据包,并且一个数据包中可以有多个对象。您本质上需要一种方法来识别流中对象的开头和结尾,以便您可以提取对象数据并将其反序列化。

实现此目的的一种方法是将数据嵌入众所周知的协议(例如 HTTP)中,并使用该格式发送。不幸的是,这也意味着您必须在客户端编写 HTTP 解析器。这不是一件容易的事。

另一种方法是利用现有的编码方案,例如 Google 的协议缓冲区。这种方法需要学习如何使用协议缓冲区堆栈。

您还可以将数据嵌入到 XML 结构中,然后在客户端安装流到 XML 的解码器。这可能是最简单的方法,但效率最低。

如您所见,这不是一个简单的项目,但您可以从 Socket.BeginSend 示例开始 此处此处,以及< a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx" rel="nofollow">SocketAsyncEventArgs 示例位于此处

其他提示:< /strong>

您可以让客户端与服务器保持两个 连接以实现冗余目的,从而提高通信的可靠性。原因是 TCP 连接需要一段时间才能建立,因此如果一个连接失败,当客户端尝试重新连接失败的连接时,您仍然可以从另一个连接接收数据。

您可以研究使用 TCPClient 类客户端实现,因为它主要是从网络连接读取流。

If you want to use .NET Sockets to create this server-client project, then this is a good outline of what's needed:

Since the server will be transferring data to several clients simultaneously, you'll need to use the asynchronous Socket.Beginxxx methods or the SocketAsyncEventArgs class.

You'll have clients connect to your server. The server will accept those connections and then add the newly connected client to an internal clients list.

You'll have a thread running within the server, that periodically sends notifications to all sockets in the clients list. If any exceptions/errors occurs while sending data to a socket, then that client is removed from the list.

You'll have to make sure that access to the clients list is synchronized since the server is a multithreaded application.

You don't need to worry about buffering your send data since the TCP stack takes care of that. If you do not want to buffer your data at all (i.e. have the socket send data immediately), then set Socket.NoDelay to true.

It doesn't seem like you need any data from your clients, but if you do, you'd have to make sure your server has a Socket.BeginReceive loop if using Socket.BeginXXX pattern or Socket.ReceiveAsync method if using SocketAsyncEventArgs.

Once you have the connection and transmission of data between server and client going, you then need to worry about serialization and deserialization of objects between client and server.

Serialization which occurs on the server is easy, since you can use the BinaryFormatter or other encoders to encode your object and dump the data onto the socket.

Deserialization on the other hand, which occurs on the client, can be pretty complex because an object can span multiple packets and you can have multiple objects in one packet. You essentially need a way to identify the beginning and end of an object within the stream, so that you can pluck out the object data and deserialize it.

One way to do this is to embed your data in a well known protocol, like HTTP, and send it using that format. Unfortunately, this also means you'd have to write a HTTP parser at the client. Not an easy task.

Another way is to leverage an existing encoding scheme like Google's protocol buffers. This approach would require learning how to use the protocol buffers stack.

You can also embed the data in an XML structure and then have a stream-to-XML decoder on your client side. This is probably the easiest approach but the least efficient.

As you can see, this is not an easy project, but you can get started with the Socket.BeginSend examples here and here, and the SocketAsyncEventArgs example here

Other Tips:

You can improve the reliability of your communication by having the client maintain two connections to the server for redundancy purposes. The reason being that TCP connections take a while to establish, so if one fails, you can still receive data from the other one while the client attempts to reconnect the failed connection.

You can look into using TCPClient class for the client implementation, since it's mostly reading a stream from a network connection.

烟火散人牵绊 2024-10-23 03:25:46

约会或西29号怎么样?这将节省重新发明轮子的麻烦。不知道 amqp 或 Zeromq 他们也可能工作得很好......

What about rendezvous or 29 west? It would save reinventing the wheel. Dunno about amqp or zeromq they might work fine too....

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