I开发者_StackOverflow中文版've read some on the internet, and I'm wondering how to send packets over TCP in C#. This is what I've done so far:
TcpClient client = new TcpClient("myipaddresssecret", 7172);
Byte[] data = "140049081141799b622d422fcd918d9fb1c55bc3f422";
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
client.Close();
(got the data from Wireshark)
So the question is: How to send TCP packets over internet in C#, or WHAT is wrong with my code?
For sending your own crafted TCP packet on Windows, 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.
Just look here: http://msdn.microsoft.com/en-us/library/ms740548(VS.85).aspx
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.
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 data cannot be sent over raw sockets.
For the case you change your mind, maybe you can find something you need in this library: http://www.eex-dev.net/fileadmin/user_upload/apidoc/NetworkLibrary/index.html
It includes a WinPcap wrapper and a lot of methods and objects to craft and analyze packets. May it be useful.
精彩评论