How do you get raw TCP packet in C#?

て烟熏妆下的殇ゞ 提交于 2019-12-05 20:47:53

问题


I want to received raw TCP packet and then send it back with same workload.

It should look something like this:

void OnPacketReceived(TcpPacket p)
{
    byte [] body = p.GetBody();
}

NOTE : I need the TCP packet and not the Ethernet frame.


回答1:


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).

Use SocketType.Raw and ProtocolType.Raw when creating the socket.

Just be aware that you're going to have to handle the details of TCP if you implement your socket this way.

For reference, here is the MSDN documentation about the Socket class: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx




回答2:


You can use the pcapDotNet library.

https://pcapdotnet.codeplex.com/




回答3:


you need to use Packet sniffer where you can put filters of your choice and on the basis of that can respond also.

.Net wrapper around WinPcap can prove to be helpful for you.



来源:https://stackoverflow.com/questions/11034870/how-do-you-get-raw-tcp-packet-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!