C语言 生成和发送数据包的 C 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3468353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C code to generate and send a packet
提问by M.S Balagopal
I would like to know about the inbuilt library functions in C to build a complete packet(along with frame) and to send it over a network... Can any one upload the C code which does the above stuff... :)
我想知道 C 中的内置库函数来构建一个完整的数据包(连同框架)并通过网络发送它......任何人都可以上传执行上述操作的 C 代码...... :)
回答by bdk
Here is a very simple code example for two programs that talk over a UDP socket: One code listing creates and sends a packet and one receives it.
下面是一个非常简单的代码示例,用于两个通过 UDP 套接字进行通信的程序:一个代码列表创建并发送一个数据包,另一个接收它。
http://www.abc.se/~m6695/udp.html
http://www.abc.se/~m6695/udp.html
Note that these Network functions aren't part of the C Language itself which has no networking support, but they are standard (POSIX, I think) and available in similar forms on most modern C implementations.
请注意,这些网络函数不是没有网络支持的 C 语言本身的一部分,但它们是标准的(我认为是 POSIX)并且在大多数现代 C 实现中以类似的形式提供。
Note that with the standard functions, you only specify the packet payload, address, port, and some flags, you don't get to control the exact contents of the ethernet frame, IP headers, etc, those are created for you by the Operating System. If you need that level of control over the low level packet, I believe you can use libpcap/winpcap for that purpose, or some operating systems may have 'raw' sockets which let you do this.
请注意,使用标准功能,您只需指定数据包有效负载、地址、端口和一些标志,您无法控制以太网帧、IP 标头等的确切内容,这些是由操作员为您创建的系统。如果您需要对低级数据包进行这种级别的控制,我相信您可以为此目的使用 libpcap/winpcap,或者某些操作系统可能具有“原始”套接字,可以让您执行此操作。

