How do you get raw TCP packet in C#?

2020-02-28 05:25发布

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.

3条回答
【Aperson】
2楼-- · 2020-02-28 05:50

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.

查看更多
Animai°情兽
3楼-- · 2020-02-28 05:51

You can use the pcapDotNet library.

https://pcapdotnet.codeplex.com/

查看更多
倾城 Initia
4楼-- · 2020-02-28 05:55

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

查看更多
登录 后发表回答