using System; using System.Net; using System.Net.Sockets; using System.Text;public struct Hello { int id; string name; byte[] Serialize() { ...序列化成二进制; }; void DeSerialize(byte[]) { ...反序列化; }; }public class UDPListener { private const int listenPort = 11000;
using System.Net;
using System.Net.Sockets;
using System.Text;public struct Hello
{
int id;
string name;
byte[] Serialize()
{
...序列化成二进制;
};
void DeSerialize(byte[])
{
...反序列化;
};
}public class UDPListener
{
private const int listenPort = 11000;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);
Hello a = new Hello; try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);
a.DeSerialize(bytes);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
} public static int Main()
{
StartListener(); return 0;
}
}