使用套接字和 C# 服务使用 PHP Web 界面进行线程化(PHP 套接字)
嗨希望有人可以帮助我。
- 我的 .net 应用程序连接(通过 TCP) 到我们将调用的服务 “超级服务”(读取和发送数据)
- 我的应用程序有一个开放的套接字供 PHP 连接到(允许人们从网络界面使用他们的用户名和密码连接到“超级服务”)
- 当有人通过 PHP 连接时我想要 产生一个线程(这是一个新的 连接到超级服务)
我最初想到这样做,以便处理 PHP 连接的线程与连接到“超级服务”的线程相同,这将使生活变得非常简单。
但是一旦页面加载,PHP 将关闭套接字,并且我仍然需要为 Web 界面的用户保持与“超级服务”的连接。
所以我想到按以下方式执行此操作:
- 1)PHP 连接到打开的套接字 我的应用程序并发送用户名/密码
- 2) 我的应用程序生成一个新线程 连接到“超级服务”(使用 提供的用户名/通行证)并创建 一个新的 Guid
- 3) 生成的线程然后被放置 放入字典(Guid,线程)中并且 将 Guid 报告回 PHP 为 保存在会话中。
- 4) PHP 再次连接到我的应用程序,但是 带有 GUID 和消息。
- 5)我的应用程序找到关联的ad 到该用户/客户端(使用 guid)
到那里我可以做。
但是接下来能够向/从该 thead 发送和获取信息(然后发送/获取超级服务)的步骤就是我陷入困境的地方......
或者还有更好的方法吗?
再次提前致谢!
注意:问题被重写以希望更有意义。
Hi Hope someone can help me.
- My .net app that connects (via tcp)
to a service we shall call
"super-service" (reads and sends data) - My app has an open socket for PHP to
connect to (To allow people from a web interface to connect to "super-service" using their username and passwords) - When someone connects via PHP i want
to spawn a thread (which is a new
connection to super-service)
I originally thought of doing it so the thread that handles the PHP connection is the same thread that connects to "super-service" which would make life very simple.
But PHP will close the socket once the page has loaded and i need to maintain a connection to "super-service" still for that user of the web interface.
So I thought of doing it the following way:
- 1) PHP Connects to an open socket on
my app and sends username / password - 2) My app spawns a new thread that
connects to "super-service" (using
username / pass supplied) and creates
a new Guid - 3) That spawned thread then get put
into a Dictionary(Guid, Thread) and
reports the Guid back to PHP to be
saved in a session. - 4) PHP connects again to my app but
with a GUID and a message. - 5) my app finds the thead associated
to that user/client (using the guid)
Upto there i can do.
But the next steps of being able to send and get info to/from that thead (which then sends/gets to super-service)is where i'm stuck...
Alternatively is there a better way?
Thanks again in advance!
NOTE: question rewritten to hopefully make more sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法真正向线程发送消息,但您可以存储另一个线程可能接收的消息。如果您想这样做,请查看
Monitor.Wait()
和Monitor.PulseAll()
。您必须将数据存储在目标线程可以访问的地方,并让目标线程等待并检查新消息。或者,您可以只将连接到“超级服务”的套接字存储在该字典中,而不是尝试控制线程。
You can't really send a message to a thread, but you can store a message that another thread may pick up. If you want to go that way, have a look at
Monitor.Wait()
andMonitor.PulseAll()
. You'd have to store data somewhere the target thread can access it, and have the target threads wait and check for new messages.Alternatively you could just store the sockets that are connected to the "super service" in that dictionary instead of trying to control the threads.