Skip to content

Instantly share code, notes, and snippets.

@alexbakker
Created August 29, 2015 11:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexbakker/05627dcf6f8165cf681a to your computer and use it in GitHub Desktop.
Save alexbakker/05627dcf6f8165cf681a to your computer and use it in GitHub Desktop.
Bootstrapinfo example
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace BootstrapCheck
{
class Program
{
static int tries = 5;
static void Main(string[] args)
{
if (args.Length != 2)
return;
IPAddress address;
if (!IPAddress.TryParse(args[0], out address))
{
Console.WriteLine("First argument should be a valid IP address");
return;
}
ushort port;
if (!ushort.TryParse(args[1], out port))
{
Console.WriteLine("Second argument should be a valid port");
return;
}
var endpoint = (EndPoint)new IPEndPoint(address, port);
var socket = new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
socket.ReceiveTimeout = 1000;
byte[] request = new byte[78];
request[0] = 240;
for (int i = 0; i < tries; i++)
{
socket.SendTo(request, endpoint);
Console.WriteLine("Sent {0} byte request to {1}", request.Length, endpoint);
byte[] buffer = new byte[2048];
int received = 0;
try { received = socket.ReceiveFrom(buffer, ref endpoint); }
catch (SocketException ex)
{
Console.WriteLine("Socket error: {0}", ex.SocketErrorCode);
continue;
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
continue;
}
byte[] data = new byte[received];
Array.Copy(buffer, 0, data, 0, received);
int version = IPAddress.HostToNetworkOrder((int)BitConverter.ToUInt32(data, 1));
string motd = Encoding.UTF8.GetString(data, 5, data.Length - 5);
Console.WriteLine("Bootstrap info {{ Source: {0}, version: {1}, motd: {2} }}", endpoint.ToString(), version, motd);
break;
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment