异步服务器套接字多个客户端 [英] Asynchronous server socket multiple clients

查看:359
本文介绍了异步服务器套接字多个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与公布在MSDN上以下code:

I have been working with the following code published on msdn:

<一个href="http://msdn.microsoft.com/en-us/library/fx6588te.aspx">http://msdn.microsoft.com/en-us/library/fx6588te.aspx

据我所知,服务器应用程序不会被阻止,而该应用程序正在等待新的客户。

I understand that the server application is not blocked whilst the application is waiting for new clients.

不过可以在此应用程序(甚至插座)为此事处理多个并发请求?

However can this application (or even sockets) for that matter handle multiple concurrent requests?

  • 如果客户A和B连接的同时,会发​​生什么?

  • What would happen if client A and B connected at the same time?

如果客户端A连接和其要求的处理需要5秒,如果客户端B连接一秒钟后它必须等待客户端A完成之前,其处理可以启动?

If client A connects and the handling of its request takes 5 seconds, if client B connects a second later must it wait for client A to finish before its processing can start?

或将客户A和客户B的请求,可以同时处理?

Or will client A and client B's requests be handled concurrently?

我已经通过把Thread.sleep代码做这个了一些测试(N)命令在监听套接字code接收/发送数据之间。然后我就可以发送多个请求到插座,他们似乎要处理。然而,插座总是处理它们的一样的线程ID - 这使我相信,它不是实际发生的事情兼

I have done some testing with this by putting Thread.Sleep(n) commands in between the receive/send data in the socket listener code. I can then send multiple requests to the socket and they appear to be handled. However the socket always handles them on the same thread id - which makes me believe that it isnt actually happening concurrently.

特别给出的描述微软提供的这个程序只是犯规块,同时等待新的连接 - 这是否意味着它的可以的处理并发连接

Especially given the description by microsoft that this app simply doesnt block whilst awaiting for new connections - does that mean it can handle concurrent connections?

推荐答案

[更新2014年]:看来这个例子已被修改,因为这个答案被张贴,作为的this线程。在MSDN的例子现在可以处理多个传入连接正常。总之,一般的方法描述这里是正确的,或许它可以提供进一步的澄清。

[Update 2014]: It seems that the example has been modified since this answer was posted, as noted in this thread. The MSDN example now handles multiple incoming connections properly. Anyway, the general approach described here is correct and perhaps it can provide additional clarification.

在做socket通信,你基本上有一个单一的监听器插座,所有传入连接,和多个处理插座为每个连接的客户端。

When doing socket communication, you basically have a single listener socket for all incoming connections, and multiple handler sockets for each connected client.

当你开始听端口,将创建传入连接的回调方法的套接字(这是引用的 你提到的例子)。那是一个和唯一的监听套接字作为该端口号:

When you start listening to a port, you create a socket with a callback method for incoming connections (this is referencing the example you mentioned). That's the one-and-only listener socket for that port number:

listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

这行告诉听众调用 AcceptCallback 方法,每当一个新的客户端连接(新连接的回调)。这种方法是应尽快开展工作,因为它阻止其他传入的连接的人。

This line tells the listener to invoke the AcceptCallback method whenever a new client is connected (new connection callback). That method is the one which should do its work quickly, since it blocks other incoming connections.

这也就是为什么 AcceptCallback 必须立即创建一个专用的处理程序插座有自己的背景数据回调方法( ReadCallback ):

That is also why AcceptCallback must immediately create a dedicated "handler" socket with its own background data callback method (ReadCallback):

// inside AcceptCallback, we switch to the handler socket for communication
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
    new AsyncCallback(ReadCallback), state); // fired on a background thread

从这一刻起, ReadCallback 方法被调用每当接收到新连接的客户端的一些数据。

From that moment on, ReadCallback method is invoked whenever some data is received by your newly connected client.

此外,在返回前, AcceptCallback 需要调用 listener.BeginAccept 再次,继续听新传入的连接

Also, before returning, AcceptCallback needs to call listener.BeginAccept again, to continue listening to new incoming connections:

// this is the same server socket we opened previously, which will now 
// continue waiting for other client connections: it doesn't care about
// the actual data transmission between individual clients
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

这部分是从MSDN示例中省略,这意味着它只能接收一个连接。

This part is omitted from the MSDN example, meaning it can only receive a single connection.

只要您从客户端得到数据包, ReadCallback 方法将被调用。因此,该数据回调方法里面,你需要阅读和处理接收到的数据,然后调用相同的 BeginReceive 方法再次(同样,与 ReadCallback 作为其数据的回调方法)。

As soon as you get a packet of data from your client, ReadCallback method will be invoked. So, inside this data callback method, you need to read and process the received data, and then invoke the same BeginReceive method again (again, with ReadCallback as its data callback method).

通过MSDN例子的问题是,它只允许一个客户端的连接( listener.BeginAccept 只调用一次)。为了让多张并发连接,您需要使用创建一个接收端口 handler.BeginReceive ,然后调用 listener.BeginAccept 开始收听新的客户。

The problem with MSDN example is that it allows connection of only a single client (listener.BeginAccept is called only once). To allow mulitple concurrent connections, you need to create a receive socket using handler.BeginReceive, and then call listener.BeginAccept to start listening to new clients.

这篇关于异步服务器套接字多个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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