Send specific packets with HttpWebRequest
Hey. Is it possible to send a packet from a C# application without using sockets? I'd like to use WebClient or HttpWebRequest in ord开发者_如何学运维er to send specifically formatted packets to a server. Examples I've seen tend to use UDP client. Thanks
It depends on what you mean by "Specifically Formatted". HttpWebRequest is a .Net wrapper around the HTTP protocol which is not UDP in nature, so you can not customize the packets it sends other than modifying object data like headers, etc.
You should look at the IPEndPoint class, this is designed for sending data to an network endpoint by IP and port address. Here is a simple example, see the link for more details and a longer example with error checking.
byte[] data = new byte[1024];
string payload = "<Enter Your Payload Here>";
IPEndPoint ep = new IPEndPoint("127.0.0.1", 1234); //IP + Port
Socket remoteServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
data = Encoding.ASCII.GetBytes(payload);
remoteServer.SendTo(data, data.Length, SocketFlags.None, ep);
精彩评论