.NET 双向套接字实时通信协议
我需要维护服务器和多个客户端之间的连接,以便客户端可以发送命令,服务器可以触发事件。服务器基本上是一个音乐播放器,客户端发送“Play()”、“Pause()”、“GetPlaylists()”等命令。服务器端需要能够告诉客户端诸如“SongEnded”或“PlayerPaused”。而且,还需要能够来回发送一些数据(如当前歌曲、专辑图像、播放列表等)。我当然可以自己创建一个套接字并创建自己的协议来处理所有上述场景,但很可能有人已经在我之前完成了这件事,所以我真正想要的是一个实时框架 - .NET 服务器和客户端之间的通信。例如,我查看了 xml-rpc,但不确定应该如何处理“OnClientSend”。另外,如果我没记错的话,xml-rpc 是类似于 REST 的。我也研究过 wcf,但由于我没有使用它的经验,所以我不知道从哪里开始,以及如何在简单的控制台应用程序中托管服务器。
重要提示:客户端不能是 .NET。
重要:这需要能够连接到 java (android)。
重要提示:主要平台是 Windows(服务器和客户端)和 Android(客户端)。
重要提示:不进行任何音频流。但是,需要发送图像。
任何解决方案的想法将不胜感激。另外,如果您有一个好的框架的链接,或者如何使用 .NET 中已经存在的组件的描述,我会非常高兴。
[编辑] 问题是,当通过套接字发送数据时,无法保证(根本!)服务器会同时读取您发送的包。我可能会发送 50 个字节,然后发送 100 个字节,然后再次发送 50 个字节,但服务器可能会将其读取为 200 字节块,或者先发送 100 个字节,然后发送 100 个字节等,这意味着我需要创建一个缓冲区,读取消息,直到我确定(这是问题)我收到了完整的消息(仅此而已)。
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". 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. I've looked at xml-rpc for instance, but not sure how I should handle the "OnClientSend" with that. Also, if I'm not mistaken, xml-rpc is made to be REST-like. 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.
Important: The client needs to be able to not be .NET.
Important: This needs to be possible to connect to java (android).
Important: Primary platforms are windows (server and client), and Android (client).
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.
[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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ZeroMQ 看起来很适合您的问题。看来你自己也实现了类似的东西。
充当并发框架的 Supersocket 库。
跨 inproc、IPC、TCP 和多播传送消息。
以扇出、发布订阅、管道和请求-答复模式连接 N 到 N。
对于集群产品和超级计算来说足够快。
用于可扩展多核消息传递应用程序的异步 I/O。
大型且活跃的开源社区。
20 多种语言,包括 C、C++、Java、.NET、Python。
大多数操作系统,包括 Linux、Windows、OS X。
LGPL 自由软件,由 iMatix Corporation 提供商业支持。
ZeroMQ looks a good fit for your problem. Seems you've implemented something similar yourself.
The Supersocket library that acts as a concurrency framework.
Carries messages across inproc, IPC, TCP, and multicast.
Connect N-to-N in fanout, pubsub, pipeline, and request-reply patterns.
Fast enough for clustered products and supercomputing.
Asynchronous I/O for scalable multicore message-passing applications.
Large and active open source community.
20+ languages including C, C++, Java, .NET, Python.
Most OSes including Linux, Windows, OS X.
LGPL free software, commercial support by iMatix Corporation.
您应该使用 WCF: http://msdn.microsoft.com/en-我们/netframework/aa663324.aspx
You should go for WCF: http://msdn.microsoft.com/en-us/netframework/aa663324.aspx
您还可以查看 XMPP 和 WebSocket。 XMPP 不仅限于消息传递,您始终可以根据自己的目的扩展它。 WebSockets 发展顺利,因为它是 HTML5 的一部分。
You can also look at XMPP and WebSockets. XMPP is not only limited for messaging you can always extend it for your own purpose. WebSockets is coming up well as it is part of HTML5.
我最终创建了自己的简单协议,我可以轻松地用多种语言实现该协议。我通过在套接字两侧添加额外的缓冲区层来实现所需的结果,然后发送每条消息,然后发送一个字节序列,告诉另一端这是消息的结尾。我还在消息中添加了 id 以启用特殊的“$Return”消息。
这些消息只是使用 xmlserializer 序列化的类。类是从 xsd 生成的。
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.
The messages are simply serialized classes using xmlserializer. Classes are generated from xsd.