如何在Linux中创建原始套接字而不缓冲接收数据包?可能吗?

时间:2022-11-23 07:35:46

I use Linux, and I create specific application. I use raw socket. And, when I open it and recv(...), I get packets, wich were went earler, and, how I guess, were buffered in kernel, or network card driver. But I don't need them. I need only packets, wich went after I opened socket. Ofcource, I can drop this packets, but I don't know how many packets I need drop, because each time quantity of packets is different. How create this socket? Is it possible?

我使用Linux,并创建特定的应用程序。我使用原始套接字。而且,当我打开它并recv(...)时,我收到了数据包,这些数据包已经过了,而且,我猜测,是在内核或网卡驱动程序中缓存的。但我不需要它们。我只需要数据包,我打开套接字后就去了。 Ofcource,我可以丢弃这些数据包,但我不知道需要丢弃多少数据包,因为每次数据包的数量都不同。如何创建这个套接字?可能吗?

1 个解决方案

#1


3  

Depends on how you've negotiated the host/port to communicate on, and do you have control over whatever is sending these packets?

取决于您如何协商主机/端口进行通信,您是否可以控制发送这些数据包的内容?

You could:

你可以:

1) Immediately after opening the socket, do a recv() loop (with flags=MSG_DONTWAIT) and ignore every packet assuming that it was stale, ending the loop as soon as recv() returns <=0 bytes (it should set errno to EWOULDBLOCK to indicate that there was nothing left to read too, otherwise the cause could be another socket-related issue)

1)打开套接字后,立即执行一个recv()循环(带有flags = MSG_DONTWAIT)并忽略每个数据包,假设它是陈旧的,一旦recv()返回<= 0字节就结束循环(它应该将errno设置为EWOULDBLOCK表示没有什么东西可以读取,否则原因可能是另一个与套接字相关的问题)

2) Negotiate a new port each time

2)每次协商一个新的端口

3) Add a custom header to your packets (e.g. first N bits) to indicate e.g. sequence number, or a special "new connection" code, or a timestamp. This usage really depends on what you're doing on both ends of this raw socket.

3)向数据包添加自定义标头(例如,前N位)以指示例如序列号,或特殊的“新连接”代码或时间戳。这种用法实际上取决于你在这个原始套接字的两端做了什么。

#1


3  

Depends on how you've negotiated the host/port to communicate on, and do you have control over whatever is sending these packets?

取决于您如何协商主机/端口进行通信,您是否可以控制发送这些数据包的内容?

You could:

你可以:

1) Immediately after opening the socket, do a recv() loop (with flags=MSG_DONTWAIT) and ignore every packet assuming that it was stale, ending the loop as soon as recv() returns <=0 bytes (it should set errno to EWOULDBLOCK to indicate that there was nothing left to read too, otherwise the cause could be another socket-related issue)

1)打开套接字后,立即执行一个recv()循环(带有flags = MSG_DONTWAIT)并忽略每个数据包,假设它是陈旧的,一旦recv()返回<= 0字节就结束循环(它应该将errno设置为EWOULDBLOCK表示没有什么东西可以读取,否则原因可能是另一个与套接字相关的问题)

2) Negotiate a new port each time

2)每次协商一个新的端口

3) Add a custom header to your packets (e.g. first N bits) to indicate e.g. sequence number, or a special "new connection" code, or a timestamp. This usage really depends on what you're doing on both ends of this raw socket.

3)向数据包添加自定义标头(例如,前N位)以指示例如序列号,或特殊的“新连接”代码或时间戳。这种用法实际上取决于你在这个原始套接字的两端做了什么。