你如何在 C# 中获取原始 TCP 数据包? [英] How do you get raw TCP packet in C#?
问题描述
我想接收原始 TCP 数据包,然后以相同的工作量将其发回.
I want to received raw TCP packet and then send it back with same workload.
它应该看起来像这样:
void OnPacketReceived(TcpPacket p)
{
byte [] body = p.GetBody();
}
注意:我需要 TCP 数据包而不是以太网帧.
NOTE : I need the TCP packet and not the Ethernet frame.
推荐答案
如果将套接字实现为原始套接字,则可以访问整个数据包(实际上必须自己处理有关数据包的所有内容).
If you implement the socket as a raw socket, you have access to the whole packet (and in fact must handle everything about the packet yourself).
在创建套接字时使用 SocketType.Raw 和 ProtocolType.Raw.
Use SocketType.Raw and ProtocolType.Raw when creating the socket.
请注意,如果您以这种方式实现套接字,您将不得不处理 TCP 的细节.
Just be aware that you're going to have to handle the details of TCP if you implement your socket this way.
作为参考,这里是有关 Socket 类的 MSDN 文档:http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
For reference, here is the MSDN documentation about the Socket class: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
这篇关于你如何在 C# 中获取原始 TCP 数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!