How to send SYN packet using C# or Java
I want to send SYN packet to a remote host, now the problem is that if I use any of the classes of .NET framework then I cannot to do this; e.g. TCP Client takes Host ip and port number and establishes connection, same is the case with ClientSocket class in java.
I want 开发者_高级运维to control the connection establishment my self; I mean I want to send the connection request (SYN packet) then wait for the connection reply and then send the packet. I know that it might not be easy without external libraries, so if any one can guide me how to do it in either C# or Java. .
Rather than commenting to both answers so far ... raw sockets have been restricted in Windows since XP (countermeasure to malware attacks). Read the following:
http://msdn.microsoft.com/en-us/library/ms740548(v=vs.85).aspx
Once that's done you can certainly build a raw socket following their rules based on Mat's answer for Java or Ritch's for C#.
It will be heavily restricted access, and very system dependent either way. I'm unaware of a method to complete the 3 way handshake and transmit data across TCP/UDP (though I'd be interested in hearing one).
I thought I should make one quick amendment to this answer. If you don't use winsock and instead use a different TCP stack, you can probably get what you want (but this is still not directly availalbe within the OS). Look at this as an example offering: http://www.komodia.com/tcpip-library-v5-1/ or this previous SO thread What is a popular, multi-platform, free and open source socket library
What you're looking for is called raw sockets. I don't know about C#, but there are libraries in Java that let you do that, e.g. RockSaw. You cannot do it using pure Java as far as I know.
Be warned that on some operating systems (Linux at least), you need administrator privileges to be able to open raw sockets. And there are a bunch of restrictions on Windows. (See that page I linked.)
Use the Socket class with a SocketType of raw, and the correct protocol type. I think you want TCP.
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
精彩评论