为了在多线程程序中处理来自多个客户端的数据而进行的探测 [英] Probelm in order to handle data from multiple client in a mutithreaded program

查看:61
本文介绍了为了在多线程程序中处理来自多个客户端的数据而进行的探测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我面对一个探针,以便在多线程Windows程序中处理来自多个客户端的数据,其中服务器为每个客户端提供不同的线程.

这里的问题是:服务器总是在新创建的线程(客户端)中读取(接收)数据,而不管客户端是否已发送消息..

在服务器,客户端1和客户端2都存在并且客户端2最近已加入的情况下.在这种情况下,客户端1服务器发送的任何数据都将视为从客户端2发送的数据并在客户端2线程中进行处理.

对此将有所帮助..

在此先谢谢您..

Dear all,

I am facing a probelm in order to handle data from multiple client in a mutithreaded windows program where server serves each client with different threads.

Probem here is: server always reads(receive) data in the newly create thread(client), irespective of the client tha has sent the message..

In a scenario where Server, Client1 & Client 2 are there and client 2 has joined recently. In this case any data sent by Client 1 server treats as data is sent from Client 2 & handles in the Client 2 thread.

Help on this will be appriciated..

Thanks in advance..

推荐答案

好,您的逻辑中有几个错误.最大的一个是您将new_fd的地址作为参数传递给处理线程.线程*不会*将该变量移到更安全的位置,但会继续通过指针(即*client_fd)使用它.这意味着您将继续在主服务器线程中访问实际变量new_fd.当第二个客户端出现时,您用第二个客户端的套接字信息覆盖new_fd并创建一个第二个线程来处理它.现在,两个线程都指向同一个new_fd变量,该变量包含第二个客户端的SOCKET,现在第一个客户端的SOCKET丢失了.

您需要在线程启动期间捕获任何可更改"参数.将其移动到线程作用域本地的变量中.当然,这带来了一个计时问题,尽管不太可能,但是客户端连接可能会变得如此之快,以至于您在主线程中处理多个连接,为每个线程创建线程,但是这些线程没有机会在主线程覆盖数据之前,启动并捕获可变"数据.由于"SOCKET"是单个"l值",因此您可以将其传递给线程,而不是传递给SOCKET的指针.这将消除l值类型参数的计时问题.

最后,您有一个变量"x",它将用作线程ID数组的索引. "x"从10开始,并在存储每个条目后递减.但是数组只有10个长,因此您创建的第一个线程超出了数组边界(0..9).
Ok, you have several errors in your logic. This biggest one is that you are passing the address of new_fd as the argument to the processing thread. The thread does *not* move that variable to a safer place but continues to use it through the pointer (i.e., *client_fd). That means you are continuing to access the actual variable new_fd in the main server thread. When the 2nd client comes along, you overwrite new_fd with the socket info the for 2nd client and create a 2nd thread to process it. Now both threads are pointing to the same new_fd variable which contains the SOCKET for the 2nd client, the 1st client''s SOCKET is now lost.

You need to capture any "changable" parameter during the start of the thread. Move it to a variable local in scope to the thread. Of course, this opens up a timing issue in that, while unlikely, it is possible for the client connections to come so fast that you process multiple ones in the main thread, creating threads for each, but the threads do not get a chance to start and capture the "changable" data before the main thread overwrites it. Since "SOCKET" is a single "l-value", you can pass it to the thread rather than a pointer to the SOCKET. That would remove the timing issue for l-value type arguments.

Lastly, you have this variable ''x'' which you are using as an index into an array of thread ids. ''x'' starts at 10 and is decremented after storing each entry. Yet the array is only 10 long so the very first thread you creates exceeds the array boundaries (0..9).


它们是否都在同一套接字上? ...如果是这样,这可能就是您遇到这个问题的原因.您是否尝试过使用一个登录请求套接字,在那里进行握手,然后将客户端发送到他们自己的套接字?

另外,您可以在应用程序层通过在一个线程上处理所有传入数据来处理此问题,在该线程中,您要找出单个数据包的源,然后将任务分配给与该客户端打交道的线程.
Are they all on the same socket? ...if so, that''s probably why you''re having this problem. Have you tried having a login-request socket, doing a hand-shake there, then sending clients to their own socket?

Alternatively, you can handle this at the application layer by handling all incoming data on one thread, in which you figure out the source of the individual packet, then task to the thread that''s dealing with that client.


这篇关于为了在多线程程序中处理来自多个客户端的数据而进行的探测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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