简体   繁体   English

.NET双向套接字实时通信协议

[英].NET two-way socket realtime communication protocol

I need to maintain a connection between a server and multiple clients so that the clients can send commands, and the server trigger events. 我需要维护服务器和多个客户端之间的连接,以便客户端可以发送命令,服务器触发事件​​。 The server is basically a music-player, and the clients send commands like "Play()", "Pause()", "GetPlaylists()", etc. The server on it's side need to be able to tell the clients things like "SongEnded" or "PlayerPaused". 服务器基本上是一个音乐播放器,客户端发送“Play()”,“Pause()”,“GetPlaylists()”等命令。它旁边的服务器需要能够告诉客户这样的事情“SongEnded”或“PlayerPaused”。 And also, it needs to be possible to send some data to and forth (like current song, album image, playlists, etc.). 而且,它需要能够来回发送一些数据(如当前歌曲,专辑图像,播放列表等)。 I could of cause go ahead and create a socket myself and create my own protocol for handling all of the above scenarios, but chances are that someone has already done this before me, so what I really want is a framework made for real-time-communication between server and client for .NET. 我可以继续自己创建一个套接字并创建自己的协议来处理所有上述场景,但很可能有人已经在我之前完成了这个,所以我真正想要的是一个实时的框架 - .NET的服务器和客户端之间的通信。 I've looked at xml-rpc for instance, but not sure how I should handle the "OnClientSend" with that. 我看过xml-rpc,但不知道我应该如何处理“OnClientSend”。 Also, if I'm not mistaken, xml-rpc is made to be REST-like. 另外,如果我没弄错的话,xml-rpc会变成类似REST的。 I've also looked at wcf, but since I have no experience with it I don't know where to start, and how to host the server in a simple console-app. 我也看了wcf,但由于我没有使用它的经验,我不知道从哪里开始,以及如何在一个简单的控制台应用程序中托管服务器。

Important: The client needs to be able to not be .NET. 重要提示:客户端需要能够不是.NET。
Important: This needs to be possible to connect to java (android). 重要提示:这需要可以连接到java(android)。
Important: Primary platforms are windows (server and client), and Android (client). 重要:主要平台是Windows(服务器和客户端)和Android(客户端)。
Important: No streaming of audio is made. 重要提示:没有音频流。 However, images needs to be sent. 但是,需要发送图像。

Any ideas for a solution would be appreciated. 任何解决方案的想法将不胜感激。 Also, if you got links to a good framework, or descriptions of how to use components already existing inside .NET I'd be really happy. 此外,如果您获得了良好框架的链接,或者如何使用.NET中已存在的组件的描述,我会非常高兴。

[Edit] The problem is that when sending data over sockets there is no guarantee (at all!) that the packages you sent will be read by the server at the same time. [编辑]问题是,当通过套接字发送数据时,无法保证(根本不会!)您发送的包将同时被服务器读取。 I might send 50, then 100, then 50 bytes again, but the server might read that as a 200byte chunk, or first 100 then 100 etc, which means I need to create a buffer, read in messages until I know for certain (this is the problem) that I've received a whole message (and nothing more). 我可能再次发送50,然后是100,然后再发送50个字节,但是服务器可能会将其读取为200字节块,或者先读取100个然后是100等,这意味着我需要创建一个缓冲区,读取消息直到我确定知道(这是问题)我收到了一条完整的信息(仅此而已)。

ZeroMQ looks a good fit for your problem. ZeroMQ看起来非常适合您的问题。 Seems you've implemented something similar yourself. 似乎你自己实现了类似的东西。

  • The Supersocket library that acts as a concurrency framework. 作为并发框架的Supersocket库。

  • Carries messages across inproc, IPC, TCP, and multicast. 通过inproc,IPC,TCP和多播传递消息。

  • Connect N-to-N in fanout, pubsub, pipeline, and request-reply patterns. 在扇出,pubsub,管道和请求 - 回复模式中连接N到N.

  • Fast enough for clustered products and supercomputing. 足够快,适用于集群产品和超级计算。

  • Asynchronous I/O for scalable multicore message-passing applications. 用于可伸缩多核消息传递应用程序的异步I / O.

  • Large and active open source community. 庞大而活跃的开源社区。

  • 20+ languages including C, C++, Java, .NET, Python. 20多种语言,包括C,C ++,Java,.NET,Python。

  • Most OSes including Linux, Windows, OS X. 大多数操作系统包括Linux,Windows,OS X.

  • LGPL free software, commercial support by iMatix Corporation. LGPL免费软件,iMatix公司的商业支持。

You can also look at XMPP and WebSockets . 您还可以查看XMPPWebSockets XMPP is not only limited for messaging you can always extend it for your own purpose. XMPP不仅限于发送消息,您可以随时扩展它以用于您自己的目的。 WebSockets is coming up well as it is part of HTML5. WebSockets即将推出,因为它是HTML5的一部分。

I ended up creating my own simple protocol that I easily can implement in several languages. 我最终创建了自己的简单协议,我可以轻松地用多种语言实现。 I achieved the wanted result by adding an extra layer of buffers on both sides of the socket, then sending each message followed by a byte-sequence that tells the other side that this was the end of the message. 我通过在套接字的两侧添加额外的缓冲层来实现所需的结果,然后发送每个消息,后跟一个字节序列,告诉另一方这是消息的结束。 Also I added id's to the message to enable the spesial "$Return" message. 我还在消息中添加了id以启用特殊的“$ Return”消息。

The messages are simply serialized classes using xmlserializer. 消息只是使用xmlserializer的序列化类。 Classes are generated from xsd. 类是从xsd生成的。

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

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