Broadcast something and it should be picked up by that code;
Working example:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SocketSample
{
class Program
{
private static void Server()
{
try
{
Socket sok = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Raw, ProtocolType.IP);
sok.Bind(new IPEndPoint(IPAddress.Parse("10.201.170.106"), 6223));
sok.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
byte[] byOut = new byte[4] { 1, 0, 0, 0 };
sok.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
int c = 0;
while (++c < 40)
{
byte[] buffer = new byte[4096];
int read = sok.Receive(buffer);
for (int i = 0; i < buffer.Length; ++i)
Console.Write((char)buffer[i]);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
private static void Client()
{
try
{
byte[] data = Encoding.ASCII.GetBytes("The quick red fox jumps over the lazy brown dog");
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 8);
sendSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
EndPoint sendEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.21"), 62508);
sendSocket.SendTo(data, data.Length, SocketFlags.None, sendEndPoint);
sendSocket.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
Thread server = new Thread(Server);
server.Start();
Thread.Sleep(250);
Thread client = new Thread(Client);
client.Start();
Console.ReadKey();
}
}
}
You probably will have to run it as administrator due to the way the socket is opened.
Hope this helps,
Fredrik Bornander