如何在套接字编程中获取接收到的数据包的 tcp 标头? [英] how to get the tcp header of a received packet in socket programming?

查看:27
本文介绍了如何在套接字编程中获取接收到的数据包的 tcp 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取每个接收到的 TCP 数据包的 TCP 头(发往特定程序,即端口)似乎不可能使用 recv() 或 recvfrom() 来使用常规套接字获取 TCP 标头.

I want to get the TCP header of each received TCP packet (destined for a specific program,namely, a port) it seems not possible to use recv() or recvfrom() to get the TCP header using regular socket.

所以,我想使用原始套接字使用原始套接字,我可以使用 recvfrom() 接收所有发往主机的 IP 数据包如果主机收到很多数据包,我需要检查每个数据包的端口号,看看是否需要它.那么这可能是非常耗费时间和 CPU 的?

So, I want to use raw socket with raw socket, I can use recvfrom() to receive all IP packets destined to the host if the host receives a lot packets, I need to check the port number of each packet so see whether it is the needed. then it could be very time and CPU comsuming?

while()
{
recvfrom();
check_port_number(); 
}

有什么好的方法可以获取接收到的 tcp 数据包的 tcp 标头吗?我正在使用 linux c,所以解决方案应该是基于 linux c 的.谢谢!

are there any good ways to get the tcp header of a received tcp packet? I'm working with linux c, so THE solution should be linux c-based. thanks!

推荐答案

你想使用 libpcap API,而不是套接字 API.libpcap 让您可以直接从网络上读取数据包.

You want to use the libpcap API, not the socket API. libpcap lets you read packets right off the wire.

另一个可以用来轻松解析数据包的库是 libnet.

Another library you can use to ease parsing the packets is libnet.

这篇关于如何在套接字编程中获取接收到的数据包的 tcp 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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