来自同一端口号的多个客户端 [英] Multple clients from same port Number

查看:123
本文介绍了来自同一端口号的多个客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个视频聊天网站。



1.每个客户端使用套接字向服务器发送她/他的捕获视频。

2.服务器将收到的数据转发给除发件人之外的其他客户。

3.然后,每个接收数据的客户都会通过图像查看器显示它。



每个客户端在单个端口上发送图像(例如:4524)。

接收方应识别来自端口的每个数据包,并让数据包形成相同的客户端匹配图像之一观众。



是否可以识别来自端口的每个数据包是否来自同一个客户端?



或者显示来自多个客户的视频imga的任何其他想法?

解决方案

也许我并没有完全理解你的想法,但我觉得你很困惑。无论如何,我清楚地知道如何与多个客户合作以正确识别它们。



通常,只有一个IP端口就足够了。你似乎证实了这一点。然后,端口是服务端的属性,而不是客户端的属性。客户端仅使用尝试连接的服务的已知端口。如果是这样,识别来自端口的每个数据包是什么意思。



但是,您的问题很容易解决。服务端的唯一标识是客户端的远程套接字,即作为服务代码对象的套接字,代表客户端的远程套接字。



您可以使用TPC,因此会话。只要双方保持连接活动,远程套接字就代表服务端的客户端套接字。



您可以使用套接字级别,但使用.NET我更喜欢更高级别:在服务端使用 System.Net.Sockets.TcpListener System.Net.Sockets.TcpClient 在客户端。



操作的模式应如下所示:在服务端,您需要两个独立的线程:一个接受新的客户端连接(让我们称之为监听线程),另一个写入和读取数据到/从 NetworkStream 跟随你在循环中开发的一些应用程序级协议与所有可用的远程(客户端)套接字(让我们称之为通信)线)。这些套接字是在通信线程中创建的,它在无限循环中调用方法 System.Net.Sockets.TcpListener.AcceptTcpClient 。此方法是阻塞的,因此线程大多数时间都处于等待状态。此方法返回表示服务端上新创建的客户端套接字的对象(相同类型: TcpClient )。现在,您应该支持收集此类 TcpClient 实例(更确切地说,一些数据项包括对此类实例的引用,请参阅belo);这个集合由两个线程使用。此集合的项类型应关联某些特定于客户端的数据(我不知道您是否要使用身份验证,但是 - 无论涉及什么)。这是关联是您需要的客户身份。不要忘记使用适当的锁定原语使集合操作成为线程安全的。



如果客户端因任何原因断开连接,您的通信线程将抛出异常。应该抓住这个例外;和异常处理程序用于从集合中删除客户端记录。通过这种方式,您不需要任何特殊的仪式来进行优雅的断开连接:一切都会正常工作,甚至客户端机器上的电源关闭。这是我的想法:优雅的连接是风向标的发明:无论如何,某些断开连接都是不优雅的(所有类型的网络故障),为什么还要烦恼?



一些额外的信息:对于线程我建议使用线程包装器;您可以在此处找到示例代码和说明:如何将ref参数传递给线程 [ ^ ]。



您可能希望为忙碌逻辑和内部提供额外的线程 - 线程沟通;考虑我在提示/技巧文章中提供的阻塞队列和线程间调用:用于线程通信和线程间调用的简单阻塞队列 [ ^ ]。



-SA

I am developing a video chat site.

1. Each clients sends her/his capturing video using socket to server.
2. And the server forward the received data to the rest of clients except the sender.
3. Then each clients who receive the data show it through the image viewer.

each clients send their images on single port (ex:4524).
The receiver should identify each packets from the port and let the packets form same client match one of image viewers.

Is it possible to identify each packets from a port whether it is from same client ?

Or any other idea that shows video imgaes from multiple clients ?

解决方案

Maybe I did not completely understand your idea, but I feel you're confusing something. Anyway, I have a clear idea how to work with multiple clients to identify them properly.

Generally, just one IP port is quite enough. You seemingly confirm it. Then, the port is the attribute of the service side, not client's. A client only uses the known port of the service whem trying to connect. If so, what does it mean "to identify each packets from a port".

However, your problem is easy to solve. The unique identification at the service side is the remote socket of the client, that is, a socket as an object of the service code which represent the remote socket of the client.

You can use TPC, hence sessions. A remote socket represents a client's socket on the service side as long as both sides keep the connection alive.

You can use socket level, but with .NET I would prefer a bit higher level: using System.Net.Sockets.TcpListener on service side and System.Net.Sockets.TcpClient on client side.

The schema of the operation should be like this: on the service side, you need two separate threads: one accepts new client connections (let's call it listening thread), another one writes and reads data to/from NetworkStream following some application-level protocol you develop in cycle with all available remote (client's) sockets (let's call it communication thread). Those sockets are created in the the communication thread which calls the method System.Net.Sockets.TcpListener.AcceptTcpClient in infinite cycle. This method is blocking, so the thread will be in wait state most of the time. This method returns the object representing a newly created client socket on the service side (of the same type: TcpClient). Now, you should support collection of such TcpClient instances (more exactly, some data items each including a reference to such instance, see belo); this collection is used by both thread. The item type of this collection should associate some client-specific data (I don't know if you want to use authentication, but — whatever involved). This is association is the client identity you need. Don't forget to use appropriate locking primitives to make collection operation thread-safe.

In case a client breaks connection by whatever reason, your communication thread will throw exception. This exception should be caught; and the exception handler used to remove the client record from the collection. In this way, you don't need any special ceremony for graceful disconnection: everything will work, even the power off on the client machine. This is my idea: graceful connections is the invention in vane: some disconnection will be non-graceful anyway (all kind of network failures), so why bothering?

Some extra information: for threads I recommend to use a thread wrapper; the sample code and explanation you will find here: How to pass ref parameter to the thread[^].

You may want to have additional thread(s) for "busyness logic" and inter-thread communication; consider blocking queue and inter-thread invocation I offer in my Tips/Tricks article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


这篇关于来自同一端口号的多个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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