using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.Threading; namespace ConsoleApplication3 { public static class Data { public const int RCV_TIMEOUT = 1000; public const int MaxRetry = 2; public const string BroadCastIP = "255.255.255.255"; public const int UDPPort = 8080; } class Program { enum Response { Error, NotFound, Found } static void Main(string[] args) { while (true) { Thread.Sleep(1000); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.EnableBroadcast = true; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Data.RCV_TIMEOUT); IPEndPoint ipendpoint = new IPEndPoint(System.Net.IPAddress.Parse(Data.BroadCastIP), Data.UDPPort); byte[] message = Encoding.ASCII.GetBytes("#Where are you"); SendSearchMessage(socket, message, ipendpoint); socket.Close(); } } private static void SendSearchMessage(Socket socket, byte[] message, EndPoint rmtdevice) { DateTime dtStart = DateTime.Now; try { socket.EnableBroadcast = true; socket.SendTo(message, message.Length, SocketFlags.None, rmtdevice); Console.WriteLine("Send."); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)