C#的客户端 - 服务器协议/模式问题 [英] C# client-server protocol/model question

查看:237
本文介绍了C#的客户端 - 服务器协议/模式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过概念为客户端 - 服务器套接字应用程序我写在C#(客户端和服务器)的模式工作。我的服务器将需要一次处理一次许多客户,最好多个请求从客户端。我已经制定了我的沟通的容器中,我会在每封邮件将包含(除其他事项外)的消息的长度的开始发送一个固定长度的头。我有在c#套接字编程的一些经验,所以我很轻松地使用异步插座。

I am trying to conceptually work through a model for a client-server socket application I am writing in c# (both client and server). My server will need to handle many clients at once, and preferably multiple requests from a client at once. I have worked out a container for my communication where I will send a fixed length header at the beginning of each message which will contain (among other things) the length of the message. I have some experience with socket programming in c#, so I am comfortable using Asynchronous sockets.

我有,在概念上,是我需要同时在客户端和服务器能够随时接收邮件的麻烦主要的事情。客户端将建立连接,并保持登录(如IM客户端),它需要将两个接收在任意时间数据,并在任意时间请求。我还希望,作为我的协议的一部分,以接收到由每个请求(无论是从服务器到客户机或客户机到服务器)的响应。

The main thing I am having trouble with, conceptually, is I need both the client and server to be able to receive messages at any time. The client will establish a connection, and remain "logged in" (like an IM client), and it will need to both receive data at arbitrary times and make requests at arbitrary times. I had also wanted, as part of my protocol, to receive a response to each request made (be it from the server to client, or client to server).

我想能够尽可能使用单一套接字。我相信我可以让使用两个插座,一个用于使服务器 - >客户端的请求,一个用于客户端 - >服务器请求这个工作,但我不想应付两个端口/连接额外的并发症。但是,使用一个单一的插座,我不知道如何管理发送请求并得到响应时,他们可能会交错。

I would like to be able to use a single socket if possible. I believe I could make this work using two sockets, one for making server->client requests and one for client->server requests, but I don't want the extra complications of dealing with two ports/connections. However, using a single socket, I am not sure how to manage sending requests and getting responses when they could be interleaved.

我无法找到一个类似的服务器的任何实例或客户在我的搜索。由于任何人谁提供任何的IDE。

I cannot find any examples of a similar server or client in my searching. Thanks to anyone who offers any ides.

推荐答案

如果你使用TCP,那么你一定要有每一个插座客户端在服务器端,以及侦听连接的插座。对于UDP你可以做到这一切与一个插座。无论如何,请执行下列操作无论是针对一个UDP套接字,或每个TCP客户端套接字:

If you're using TCP, then you will necessarily have to have one socket per client on the server side, as well as the socket to listen for connections. For UDP you can do this all with one socket. Regardless, do the following for either your one UDP socket, or for each TCP client socket:

有一个 BeginReceive 在任何时候去,当你需要用到的事件发生 BeginWrite (即用户按下一个按钮或消息进来,你需要做些什么)。

Have a BeginReceive going at all times and BeginWrite when you the necessary event occurs (ie the user presses a button or a message came in that you need to do something about).

编辑:在回答你的评论,我会处理这将是包含在你的头的请求ID的方式。每当发送一个请求(从任一端)包括用于该消息的唯一值。使用词典<的requestId,RequestState> (与类型相应的替代),看看是否传入消息涉及现有的预申请。此外,您可以指定与高位集中的所有ID都起源于客户端和清除高位请求ID是从服务器,以避免冲突:

In response to your comment, the way I'd handle it would be to include a request ID in your header. Whenever sending a request (from either end) include a unique value for that message. Use a Dictionary<RequestId, RequestState> (with appropriate substitutions for the types) and see if an incoming message relates to a pre-existing request. Furthermore, you could specify that all IDs with the high bit set are requests originating at the client and IDs with the high bit cleared are from the server to avoid collisions:

Server                      Client
Request 0x00 -------------> Starts processing

Starts processing <-------  (unrelated) Request 0x80

Request 0x00 complete <---- Sends response to 0x00

Sends response to 0x80 ---> Request 0x80 complete

这是AOL的OSCAR协议使用(可能慢)状态要求系统。

This is the system that AOL's OSCAR protocol uses for (possibly slow) stateful requests.

EDIT2:嗯,好......你真的想它阻止?有些事情,你可以做的是有一个单独的线程处理发送 / 接收来电。该线程将通过一个线程安全的发送消息队列主线程通信,而类似的消息收到伪队列。我把后者伪队列的原因是,你要把这些消息的乱序从队列的能力。主线程将放在发送队列中的消息,然后在接收队列阻塞。每次套接字线程更新接收队列,主线程会醒来并检查它想要的信息是还没有。如果是,它将把它(不按顺序),并完成所需的操作。如果消息现在还没有,它刚刚在队列块一次。当操作终于完成了它刚刚恢复正常运行,并从接收队列,以便获得消息并处理它们或做它做任何其他。

Hm, ok... you really want to block on it? Something you could do is have a separate thread handle the Send/Receive calls. This thread would communicate with the main thread via a thread-safe "send message" queue and a similar "message received" psuedo-queue. The reason I call the latter a psuedo-queue is that you would want the ability to take messages out of order from the queue. The main thread would put a message on the send queue, then block on the receive queue. Each time the socket thread updates the receive queue, the main thread would wake up and check if the message it wants is there yet. If it is, it would take it (out of order) and finish the desired operation. If the message is not there yet, it'd just block on the queue again. When the operation is finally completed it'd just resume normal operation and get messages in order from the receive queue and process them or do whatever else it does.

这是否化妆任何意义吗?我很抱歉,如果它有点别扭......

Does that make any sense? I'm sorry if it's kinda confusing...

这篇关于C#的客户端 - 服务器协议/模式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆