can anybody tell me how to create a TCP packet using C# ????
Looks like you are looking for the TCP Client class.
Look at MSDN and read through the System.Net.Sockets namespace documentation for more information.
You don't create TCP packets. TCP presents a stream-based abstraction; you read and write streams of bytes to/from the network socket. The fact that the network very probably treats these as packets at some point is not something you need to care about at the application level.
If you do need to care, you can use a tool such as GNU netcat to send traffic.
May I advise you to this library: http://www.eex-dev.net/fileadmin/user_upload/apidoc/NetworkLibrary/index.html?
Here is the code for sending a custom TCP packet:
//Create a list for the interfaces
List<EthernetInterface> wpcInterfaces = new List<EthernetInterface>();
//Get all local interfaces
WinPcapInterface[] arWpc = EthernetInterface.GetAllPcapInterfaces();
//Create a router
Router rRouter = new Router();
//Foreach WinPcapInterface of this host
foreach (WinPcapInterface wpc in arWpc)
{
//Create a new interface handler and start it
EthernetInterface ipInterface = new EthernetInterface(wpc);
ipInterface.Start();
//Then add it to the router and to our list
wpcInterfaces.Add(ipInterface);
rRouter.AddInterface(ipInterface);
}
//Create a TCP frame
TCPFrame tcpFrame = new TCPFrame();
tcpFrame.DestinationPort = 80;
tcpFrame.SourcePort = 12345;
tcpFrame.AcknowledgementFlagSet = true;
//Create an IP frame and put the TCP frame into it
IPv4Frame ipFrame = new IPv4Frame();
ipFrame.DestinationAddress = IPAddress.Parse("192.168.0.1");
ipFrame.SourceAddress = IPAddress.Parse("192.168.1.254");
ipFrame.EncapsulatedFrame = tcpFrame;
//Send the frame
rRouter.PushTraffic(tcpFrame);
//Cleanup resources
rRouter.Cleanup();
//Start the cleanup process for all interfaces
foreach (EthernetInterface ipInterface in wpcInterfaces)
{
ipInterface.Cleanup();
}
//Stop all handlers
rRouter.Stop();
//Stop all interfaces
foreach (EthernetInterface ipInterface in wpcInterfaces)
{
ipInterface.Stop();
}
Hopefully it helps. :)
Have a look at the System.Net.Sockets namespace. In particular, at the System.Net.Sockets.Socket class.
精彩评论