如何分离不同客户端发送的数据包? [英] How to seperate packets sent by different clients?

查看:70
本文介绍了如何分离不同客户端发送的数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个服务器应用程序,它接受多个客户端。我正在使用TcpListener类并使用异步AcceptClient方法我为每个连接的客户端创建一个单独的线程。并且这些线程分别从自己的TsClClient回调获取的TcpClient套接字中读取。他们不断读取数据包并排队。是否可以通过客户端B的线程捕获客户端A发送的数据包?我没有实现任何客户端识别机制。我只依赖于AcceptClient方法产生的单独的TcpClient对象。够了吗 ?因为我感觉数据包混乱严重... :(

解决方案

来自一个客户端的数据包不可能被传递到另一个TcpClient。


在服务器方案中实际发生的是在给定端口上建立侦听套接字。当客户端连接时,服务器从机器获取另一个端口并使用当侦听套接字继续侦听原始端口时,与客户端通信。因此,每个客户端连接最终都有自己唯一的端口。


如果你的数据包混淆了,它可能是由线程如何排队造成的。


-Steve


PS每个客户端一个线程是一种常见的方法,但只有在有意义的情况下才有意义线程实际上工作。如果他们只是从套接字读取并将读取排队到一个单独的工作线程,那么实际上你的效率低​​于仅使用BeginRead。


Hi all, I have a server application and it accepts multiple clients. I'm using the TcpListener class and using async AcceptClient method I create a seperate thread for each client that connects. And these threads seperately reads from their own TcpClient sockets obtained by the AcceptClient callback. They constantly read packets and queue them. Is it possible if a packet sent by Client A can be captured by Client B's thread ? I haven't implemented any client identification mechanism. I just rely on the seperate TcpClient objects yielded by the AcceptClient method. Is that enough ? Cause I sense that the packets are mixed up badly... :(

解决方案

It's not possible for a packet coming from one client be delivered to a different TcpClient.

What actually happens in a server scenario is that a listening socket is established on a given port. When a client connects, the server takes another port form the machine and uses that to communicate with the client, while the listening socket continues listening on the original port. So, each client connection ends up with its own unique port.

If your packets are getting mixed up, it's probably being caused by how the threads queue them.

        -Steve

P.S. One thread per client is a common approach, but only makes sense if the threads actually do work. If they're just reading from the socket and queueing the reads to a separate worker thread, then you're actually being less efficient than just using BeginRead.


这篇关于如何分离不同客户端发送的数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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