如何用 C# 为多个客户端创建一个易于编程的服务器?
我想类似的问题已经被问过,但我找不到任何问题。请随时向我指出现有的解决方案。
我将解释我的情况。我想创建一个服务器应用程序。有许多客户端(目前只有几十个,但应该扩展到 1000+)连接到服务器(在单台机器上运行)。
每个客户端定期发送少量数据到服务器进行处理(处理速度很快)。服务器还可以定期向每个客户端发送少量数据。响应时间应该很短(<100 ms),但不需要实时或类似的东西。
我的第一个想法来自于我还在用 VB6 编程时:创建一个服务器套接字来侦听传入的请求,然后为每个可能的客户端创建一个客户端套接字(单线程)。我怀疑这是否能很好地扩展。沟通也很难落实。
所以我想我应该创建一个侦听器线程来接受新的客户端连接,并创建一个不同的线程来实际读取客户端传入的数据。由于会有很多客户端,我不想为每个客户端创建一个线程。相反,我更喜欢使用单个线程来读取循环中的所有传入数据,然后直接处理这些数据或创建工作项以供不同的线程处理。我想这种方法可以很好地扩展。欢迎对这个想法提出任何意见。
我担心的剩下的问题是沟通的便捷性。上述解决方案似乎需要手动协议,可能通过 TCP 发送 ASCII 命令。虽然这可行,但我认为现在应该有更好的方法。
某些接口/代理方式似乎是合理的。我之前使用过 Java RMI。根据我的理解,.NET Remoting 具有类似的目的。对于我所描述的场景(许多客户端)来说,远程处理是一个可行的解决方案吗?还有我不知道的更好的方法吗?
编辑:
- 这不是在局域网中,而是在互联网中,如果这很重要的话。
- 如果可能的话,它也应该在Linux下运行。
I suppose similar questions were already asked, but I was unable to find any. Please feel free to point me to an existing solutions.
I'll explain my scenario. I'd like to create a server application. There are many clients (currently only a few dozens, but it should scale up to 1000+) that connect to the server (which is running on a single machine).
Each client periodically sends a small amount of data to the server to process (processing is quick). The server can also send small amounts of data to each client on a regular basis. The response time should be low (<100 ms), but realtime or anything like that is not required.
My first idea was back from when I was still programming in VB6: Create a server socket to listen to incoming requests, then create a client socket for each possible client (singlethreaded). I doubt this scales well. It is also difficult to implement the communication.
So I figured I'd create a listener thread to accept new client connections and a different thread to actually read the incoming data by the clients. Since there are going to be many clients, I don't want to create a thread for each client. Instead, I'd prefer to use a single thread to read all incoming data in a loop, then either processing these data directly or creating work items for a different thread to process. I guess this approach would scale well enough. Any comments on this idea are most welcome.
The remaining problem I'm worried about is easy of communication. The above solution seems to require a manual protocol, possibly sending ASCII commands via TCP. While this would work, I think there should be a better way nowadays.
Some interface/proxyish way seems reasonable. I worked a bit with Java RMI before. From my point of understanding, .NET Remoting serves a similar purpose. Is Remoting a feasible solution to the scenario I described (many clients)? Is there an even better way I don't know of yet?
Edit:
- This is not in LAN, but internet, if that matters.
- If possible, it should also run under Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 AresnMkrt 指出的,您应该尝试 WCF。
只需按原样使用(使用 netTcpBinding,但不要忘记关闭安全性)并创建 Tracer Bullet - 测量性能是否满足您的要求。
如果没有,您可以尝试调整 WCF - WCF 具有很强的可扩展性,您可以修改消息序列化以根据需要发送 ASCII 消息。
As AresnMkrt pointed out, you should try WCF.
Just take it as is (with netTcpBinding, but don't forget to switch security off) and create a Tracer Bullet - measure if performance meets your requirements.
If not, you can try to tune WCF - WCF is very extensible, and you can modify message serialization to send ASCII messages as you want.
您确定需要二进制协议吗?相反,您确定需要发明一个全新的协议,使用 JSON/XML 的普通 RESTful 服务就足够了吗? WCF 在这方面可以为您提供很多帮助。
Are you sure you need a binary protocol? Rather, are you sure you need to invent a whole new protocol where plain RESTful service with JSON/XML will suffice? WCF can help you in this regard a lot.