开发者

Send TCP packet in C#

开发者 https://www.devze.com 2023-01-29 11:48 出处:网络
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?

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?

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.

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

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


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.

Just look in 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.

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 the library eExNetworkLibrary.

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


Are you attempting to send a TCP packet or a UDP packet? 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.


You are Binding your socket but you should Connect() (to the other endpoint).


For TCP you need to connect to the remote endpoint.

It is better if you use the TcpClient Class to create your socket, see TcpClient.Client Property.

Check out the sample in Socket Send and Receive [C#].


For making custom packets and sending on the network you should take a look at Pcap.Net. Check out the sample code presented in an answer to Stack Overflow question "IP address spoofing using SharpPcap on C#".


You should use SocketType.Stream:

SocketType.Raw: Supports access to the underlying transport protocol. Using the SocketTypeRaw, you can communicate using protocols like Internet Control Message Protocol (Icmp) and Internet Group Management Protocol (Igmp). Your application must provide a complete IP header when sending. Received datagrams return with the IP header and options intact.

SocketType.Stream: Supports reliable, two-way, connection-based byte streams without the duplication of data and without preservation of boundaries. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消