端口到线程映射 [英] Port to Thread Mapping

查看:138
本文介绍了端口到线程映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要问题端口到线程映射的帮助,例如,一个进程有2个线程(T1,T2),一个线程(T1)正在使用端口x,另一个线程(T2)正在使用端口y.现在的问题是,如何找到哪个线程正在使用哪个端口.基于示例,T1→T2. x,T2→在这里我只有进程ID和端口号.解决方案可以在用户模式或内核中,但是我正在寻找优化的方法.

这是我的疑问-要访问任何端口,我需要将套接字绑定到任何唯一的端口.现在,应用程序创建一个套接字对象,获取句柄并向/从端口发送/接收数据.当此线程使用此端口时,没有人可以访问此端口,没有人可以获取此套接字对象的句柄.这意味着在特定时间只有1个线程可以具有套接字句柄并使用特定端口.所以现在在我的示例中2个线程正在使用相同的场景,我的问题是如何识别哪个线程正在使用哪个端口?

我正在考虑解析进程句柄表并检查对象属性,但是我不知道如何用代码编写它.

出于好奇,如何将数据包从端口传输到线程?任何文章或引荐来源将不胜感激.

除了上述疑问之外,我还有更多内容-
1.如何通过OS网络部分进行管理?
2.为什么我需要创建一个套接字?为什么我不能直接从任何端口发送或接收数据?
3.线程读取的数据包如何属于多线程进程?
4.一个线程是否可以在属于同一进程的另一个线程正在使用的同一端口上执行任何操作?

谢谢,
Subrat Sarkar

Hi,

I need help for problem port to thread mapping, for example, a process have 2 threads (T1, T2)and one thread(T1) is using port x and another(T2) is using port y. Now here is the problem, how to find that which thread is using which port. Based on example, T1 -> x, T2 -> y here. I have Process ID and port number only. Solution can be in user mode or kernel but I am looking for optimized approach.

Here is my doubt - To access any port, I need to bind a socket to any unique port. Now application creates a socket object, get handle and send/receive data to/from port. While this thread is using this port none can get access to this port and no-one can get handle for this socket object. That means at a particular time only 1 thread can have socket handle and use specific port. So now in my example 2 threads are using the same scenario, My question is how to identify which thread is using which port ?

I am thinking to parse process handle table and check object attribute but I don''t know how to write it in code.

For my curiosity, how is a packet transferred from the port to thread ? Any article or referrer would be appreciated.

Apart from above doubt I have some more -
1. How is it managed by OS network part ?
2. Why do I need to create a socket ? Why can''t I directly send or receive data to/from any port ?
3. How is a data packet read by thread belongs to multi-Threaded process ?
4. Is it possible for a thread can perform any operation on same port which is being used by another thread belongs to the same process?

Thanks,
Subrat Sarkar

推荐答案

任何线程都可以使用进程内的任何套接字.我可以轻松地将数据从2个线程互换地传递给套接字.我不知道当前Windows版本的内部信息,但我想没有为套接字存储这样的数据.可能只有一个锁(或可能是无锁的数据结构)可以在试图同时访问同一套接字的多个线程之间进行同步.
在高性能网络引擎中,当事件发生时(一个可用的传入数据,或发送缓冲区中的可用空间,传入的连接,...),其中一个套接字经常使用线程池来为套接字提供服务,并且某些东西具有每次使用随机线程作为服务方时,找出套接字处理哪个线程只是毫无意义的,通常是随机的.
Any threads can use any of the sockets inside a process. I can easily pass data to a socket from 2 threads interchangeably. I don''t know the internals of current windows versions but I guess there is no such data stored for sockets. Probably there is only a lock (or maybe lockless data structure) to synchronize between multiple threads that try to access the same socket at the same time and that''s it.

In high performance network engines often a thread pool is used to serve sockets when an event occurs (incoming data available, or free space is available in the send buffer, incoming connection, ...) with one of the sockets and something has to be done with the sockets, each time a random thread is used as a servant so finding out which thread handles the socket just makes no sense, its often random.
SubratSarkar写道:
SubratSarkar wrote:

出于好奇,如何将数据包从端口传输到线程?任何文章或引荐来源将不胜感激.

For my curiosity, how is a packet transferred from the port to thread ? Any article or referrer would be appreciated.


当数据包进入您的以太网卡时,将解析其标头.如果目标地址不是您的机器,则将对数据包进行路由(如果您的机器是以这种方式配置的).如果目标地址是您的机器,则根据目标端口号,传入数据将存储在端口所标识的操作系统内的套接字对象的接收缓冲区(接收缓冲区的大小可通过setsockopt/SO_RCVBUF进行配置)中. > 许多进程可以具有一个打开的句柄,该句柄引用OS中的同一套接字对象,并且应用程序可以从套接字的接收缓冲区中将数据读出到应用程序的地址空间的这些进程的任何线程.我本人从未使用过多个进程中的套接字对象,但是有一个函数可以复制套接字句柄:


When the packet comes in on your ethernet card its header is parsed. If the target address isn''t your machine then the packet is routed (if your machine is configured that way). If the target address is your machine then according to the target port number the incoming data is stored in the receive buffer (size of receive buffer is configurable with setsockopt/SO_RCVBUF) of the socket object inside the OS identified by the port.
Many processes can have an open handle that reference the same socket object in the OS, and any thread of these processes that application can read out data from the receive buffer of the socket to the address space of the app. I myself never used a socket object from more than one processes but there is a function to duplicate socket handles: WSADuplicateSocket[^].

Summary: The address/port pair in the incoming packet identifies a machine(address) and a socket object inside (port) the networking part of the OS. Any application can have an open handle to that socket object (the handle is basically a process-local "reference counting pointer" to an object inside the OS) and any thread inside those applications can manipulate the socket object. When all applications close all handles to the socket object it might still remain alive for some time if it has data to send in its send buffer (search for socket linger to learn more about this) or unfinished tasks regarding the TCP protocol (shutdown, etc...).


这篇关于端口到线程映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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