Skip to content

Instantly share code, notes, and snippets.

@varavut
Last active January 26, 2019 07:22
Show Gist options
  • Save varavut/215159c8ac4a734b816c3821800bf19e to your computer and use it in GitHub Desktop.
Save varavut/215159c8ac4a734b816c3821800bf19e to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace DnsCall
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
EndPoint endPoint = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);
string hexMessage = "AAAA01000001000000000000076578616d706c6503636f6d0000010001";
hexMessage = "AA AA 01 00 00 01 00 00 00 00 00 00 0A 6A 6F 68 61 6E 6E 6F 74 65 73 03 63 6F 6D 00 00 01 00 01".Replace(" ", "");
byte[] bytes = StringToByteArray(hexMessage);
socket.SendTo(bytes, endPoint);
byte[] buffer = new byte[255];
int len = socket.ReceiveFrom(buffer, ref endPoint);
byte[] responseBytes = new byte[len];
Array.Copy(buffer, responseBytes, len);
string response = BitConverter.ToString(responseBytes);
Debug.WriteLine(response);
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment