asynchronous sockets
I have written a simple socket program that joins and reads a multicast group. But it reads the multicast synchronously and if theres nothing coming form the multicast group, the program just hands. How can I make it synchronous? How can I use delegates here so that the code becomes non-blocking.
Heres the code
public void ReceiveBroadcastMessages()
{
IPAddress mcastAddress;
int mcastPort;
Socket mcastSocket;
MulticastOption mcastOption;
mcastAddress = IPAddress.Parse("15.15.15.15");
mcastPort = 56000;
byte[] bytes = new byte[1024];
IPEndPoint groupEP = new IPEndPoint(mcastAddress, mcastPort);
EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any,0);
string temp = "";
int i = 0;
try
{
mcastSocket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPAddress localIPAddr = IPAddress.Parse("20.20.20.20");
EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, mcastPort);
mcastSocket.Bind(localEP);
mcastOption = new MulticastOption(mcastAddress, localIPAddr);
mcastSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,mcastOption);
i = mcastSocket.ReceiveFrom(bytes,ref remoteEP); //Blocking code
temp = groupEP.ToString() + System.Text.Encoding.ASCII.GetString(bytes,0,bytes.Length);
mcastSocket.Close();
}
catch (Exception e)
{
string a = e.Message ;
}
}

