发送TCP数据包在C# [英] Send TCP packet in C#

查看:164
本文介绍了发送TCP数据包在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中发送一个TCP包(一个自定义标题)。这种数据包的建设是没有问题的,我有一个字节数组中的数据。 ?但我怎么能在一个插座发送这个数据包

I want to send a TCP packet (with a custom header) in C#. 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?

我想是这样的:

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?

我使用的是Windows  7专业版,我不希望系统独自创建完整的TCP连接。

I use Windows 7 Professional, and I don't want the system to create the full TCP connection all alone.

PS:我不想使用一些其他的库。

PS: I don't want to use some other library.

PS:建设IP数据包是没有问题的,无论是

PS: Building IP packets is not a problem, either.

推荐答案

有关Windows 7的发送您自己制作的TCP数据包,您将需要一个像的WinPcap 。如果你使用的WinPcap,您可以使用许多.NET包装的一个或编写自己的。发送原始帧只能用Windows的API(如插座)提供的对象将无法工作。

For sending your own crafted TCP packet on Windows 7, you will need a driver like WinPcap. If you use WinPcap, you can use one of the many .NET wrappers or code your own. Sending a raw frame only with objects provided by the Windows API (like sockets) will not work.

只要看看中的 TCP / IP原始套接字

Just look in TCP/IP Raw Sockets.

,唯一的选择是创建自己的网络监控司机,还是买WinPcap的商业版本,它不需要安装,但是可以无缝集成到您的程序。

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.

在Windows 7中,Windows Vista中,Windows XP中带有Service Pack 2(SP2)和Windows XP的Service Pack 3(SP3)发送流量原始套接字上的能力已经在几个方面受到限制:

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:

TCP数据无法通过原始套接字发送

TCP data cannot be sent over raw sockets.

有关你改变主意的情况。也许你可以找到你在图书馆里需要的东西 eExNetworkLibrary

For the case you change your mind. Maybe you can find something you need in the library eExNetworkLibrary.

它包括一个WinPcap的包装和大量的方法和对象手艺和分析数据包。也许这将是有益的。

It includes a WinPcap wrapper and a lot of methods and objects to craft and analyze packets. May it will be useful.

这篇关于发送TCP数据包在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆