I have a problem with the code below. What he does is send the current date / time using UDP broadcast and listen to this message. My current use of this code is local, I use "Send and Receive" in the same application on the same computer. I have not tried this between two computers.
public class Agent
{
public static int Port = 33333;
public delegate void OnMessageHandler(string message);
Socket socketSend;
Socket socketReceive;
bool receiving = false;
Thread receiveThread;
public event OnMessageHandler OnMessage;
public Agent()
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketSend.EnableBroadcast = true;
socketSend.Connect(new IPEndPoint(IPAddress.Broadcast, Port));
socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketReceive.EnableBroadcast = true;
socketReceive.Bind(new IPEndPoint(IPAddress.Any, Port));
}
public void Start()
{
Console.WriteLine("Agent started!");
receiving = true;
receiveThread = new Thread(new ThreadStart(Receive));
receiveThread.IsBackground = true;
receiveThread.Start();
}
public void Stop()
{
receiving = false;
socketSend.Close();
socketReceive.Close();
receiveThread.Join();
Console.WriteLine("Agent ended.");
}
public void Send(string message)
{
Console.WriteLine("Sending : {0}", message);
byte[] bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, Encoding.Unicode.GetBytes(message));
socketSend.Send(bytes);
Console.WriteLine("Message sent.");
}
protected void Receive()
{
Console.WriteLine("Thread started!");
while (receiving)
{
try
{
if (socketReceive.Available > 0)
{
byte[] bytes = new byte[socketReceive.Available];
Console.WriteLine("Waiting for receive...");
int bytesReceived = socketReceive.Receive(bytes, SocketFlags.None);
Console.WriteLine("Bytes received : {0}", bytesReceived);
string message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("Received message : {0}", message);
OnMessage(message);
}
else
{
Console.Write(".");
Thread.Sleep(200);
}
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.Interrupted)
{
break;
}
else
{
throw;
}
}
}
Console.WriteLine("Thread ended.");
}
}
, ( ), . , / 19 , > 0, 38. 19 , 19 . , , .
:
Agent started!
Thread started!
..........Sending : 29/07/2009 12:05:04
Message sent.
Waiting for receive...
Bytes received : 19
Received message : 29/07/2009 12:05:04
Waiting for receive...
Bytes received : 19
Received message : 29/07/2009 12:05:04
......Thread ended.
Agent ended.