0

I'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?

2
  • I said I was wondering how to send packets over TCP in C# ?? Commented Jun 26, 2011 at 10:30
  • What are your grounds for believing that this doesn't work? What result were you expecting and how does it compare to what actually happened? Commented Jun 26, 2011 at 10:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok how to install it, and do u know why my data is too long ?? I copied it from wireshark.
Are you sure he even wants to craft packet headers and whatnot? It kind of looks to me that this guy just wants to send plain, normal data via standard TCP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.