简体   繁体   English

用C#发送TCP数据包

[英]Send TCP packet in C#

I want to send a TCP packet (with a custom header) in C#. 我想在C#中发送TCP数据包(带有自定义标头)。 The building of such packets is no problem, and I have the data in a byte array. 这样的数据包的构建是没有问题的,并且我将数据存储在字节数组中。 But how can I send this packet over a socket? 但是如何通过套接字发送此数据包?

I tried something like this: 我试过这样的事情:

using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP))
{
    TcpPacket tcpPacket = new TcpPacket();
    // fill tcpPacket with data
    sock.Bind(new IPEndPoint(MYADDRESS, MYPORT));
    byte[] data = tcpPacket.GetBytes();
    sock.SendTo(data, new IPEndPoint(DESTADDRESS, DESTPORT));
}

This runs without any exception but sniffing the network shows that nothing is send. 这毫无例外地运行,但是嗅探网络表明没有任何发送。 What is the solution? 解决办法是什么?

I use Windows 7 Professional, and I don't want the system to create the full TCP connection all alone. 我使用Windows 7 Professional,并且我不希望系统单独创建完整的TCP连接。

PS: I don't want to use some other library. PS:我不想使用其他库。

PS: Building IP packets is not a problem, either. PS:构建IP数据包也不是问题。

For sending your own crafted TCP packet on Windows 7, you will need a driver like WinPcap . 为了在Windows 7上发送您自己制作的TCP数据包,您将需要WinPcap之类的驱动程序。 If you use WinPcap, you can use one of the many .NET wrappers or code your own. 如果使用WinPcap,则可以使用许多.NET包装器之一或自己编写代码。 Sending a raw frame only with objects provided by the Windows API (like sockets) will not work. 仅使用Windows API提供的对象(例如套接字)发送原始帧将不起作用。

Just look in TCP/IP Raw Sockets . 只需查看TCP / IP Raw Sockets即可

The only alternative would be to create your own network monitoring driver, or to buy a commercial version of WinPcap which does not require installation but integrates seamlessly into your program. 唯一的选择是创建您自己的网络监控驱动程序,或购买商业版本的WinPcap,该版本不需要安装但可以无缝集成到您的程序中。

On Windows 7, Windows Vista, Windows XP with Service Pack 2 (SP2), and Windows XP with Service Pack 3 (SP3), the ability to send traffic over raw sockets has been restricted in several ways: 在Windows 7,Windows Vista,带有Service Pack 2(SP2)的Windows XP和带有Service Pack 3(SP3)的Windows XP上,通过原始套接字发送流量的能力受到多种方式的限制:

TCP data cannot be sent over raw sockets. TCP数据无法通过原始套接字发送。

For the case you change your mind. 对于这种情况,您会改变主意。 Maybe you can find something you need in the library eExNetworkLibrary . 也许您可以在eExNetworkLibrary库中找到所需的东西。

It includes a WinPcap wrapper and a lot of methods and objects to craft and analyze packets. 它包括一个WinPcap包装器以及许多用于处理和分析数据包的方法和对象。 May it will be useful. 可能会有用。

Are you attempting to send a TCP packet or a UDP packet? 您是否要发送TCP数据包或UDP数据包? If you want to send a TCP packet you need to Connect() to your the remote end point before you attempt to send the packet. 如果要发送TCP数据包,则在尝试发送数据包之前,需要Connect()到远程端点。

For TCP you need to connect to the remote endpoint. 对于TCP,您需要连接到远程端点。

It is better if you use the TcpClient Class to create your socket, see TcpClient.Client Property . 如果使用TcpClient类创建套接字,则更好,请参见TcpClient.Client属性

Check out the sample in Socket Send and Receive [C#] . Socket Send and Receive [C#]中检出示例。

您正在绑定套接字,但是应该使用Connect() (到另一个端点)。

For making custom packets and sending on the network you should take a look at Pcap.Net . 为了制作自定义数据包并在网络上发送,您应该查看Pcap.Net Check out the sample code presented in an answer to Stack Overflow question "IP address spoofing using SharpPcap on C#" . 请查看“堆栈溢出”问题“在C#上使用SharpPcap进行IP地址欺骗”的答案中提供的示例代码。

You should use SocketType.Stream : 您应该使用SocketType.Stream

SocketType.Raw: Supports access to the underlying transport protocol. SocketType.Raw:支持对基础传输协议的访问。 Using the SocketTypeRaw, you can communicate using protocols like Internet Control Message Protocol (Icmp) and Internet Group Management Protocol (Igmp). 使用SocketTypeRaw,可以使用Internet控制消息协议(Icmp)和Internet组管理协议(Igmp)之类的协议进行通信。 Your application must provide a complete IP header when sending. 发送时,您的应用程序必须提供完整的IP标头。 Received datagrams return with the IP header and options intact. 收到的数据报将返回IP标头和完整的选项。

SocketType.Stream: Supports reliable, two-way, connection-based byte streams without the duplication of data and without preservation of boundaries. SocketType.Stream:支持可靠的双向双向基于连接的字节流,而无需重复数据,也无需保留边界。 A Socket of this type communicates with a single peer and requires a remote host connection before communication can begin. 这种类型的套接字与单个对等方通信,并且需要远程主机连接才能开始通信。 Stream uses the Transmission Control Protocol (Tcp) ProtocolType and the InterNetworkAddressFamily. Stream使用传输控制协议(Tcp)ProtocolType和InterNetworkAddressFamily。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM