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 ;
}
}

[1494 byte] By [bilalso] at [2008-2-14]
# 1

The following code should do the trick. I am on a guest machine so I have not had a chance to compile or run this code, but the following code snippet should give you the general idea:



class StateObject
{
public byte[] buffer;
public Socket sock;
}
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);
StateObject so = new StateObject();
so.buffer = bytes;
so.sock = mcastSocket;

// this call will not block
mcastSocket.BeginReceiveFrom(bytes, 0, bytes.Length, SocketFlags.Multicast, ref remoteEP,
new AsyncCallback(MyCallback), so);

//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 ;
}
}

public void MyCallback(IAsyncResult ar)
{
StateObject so = (StateObject)ar.AsyncState;
Socket mcastSocket = so.sock;
IPEndPoint ipPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEP = (EndPoint) ipPoint;

int read = EndReceiveFrom(ar, ref remoteEP);
// remoteEP should now contain who sent the datagram

if (read > 0){
// consume the bytes in so.buffer (variable read holds how many bytes there are of data in the buffer)
// post another receive
mcastSocket.BeginReceiveFrom(so.buffer,0,so.buffer.Length,SocketFlags.Multicast,so);
}
else
{
// no data was read
}
}

MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
One small correction,
this line
// this call will not block
mcastSocket.ReceiveFrom(bytes, 0, bytes.Length, SocketFlags.Multicast, ref remoteEP,
new AsyncCallback(MyCallback), so);

should be

// this call will not block
mcastSocket.BeginReceiveFrom(bytes, 0, bytes.Length, SocketFlags.Multicast, ref remoteEP,
new AsyncCallback(MyCallback), so);

mahjayar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3

mahjayar wrote:
One small correction,
this line
// this call will not block
mcastSocket.ReceiveFrom(bytes, 0, bytes.Length, SocketFlags.Multicast, ref remoteEP,
new AsyncCallback(MyCallback), so);

should be

// this call will not block
mcastSocket.BeginReceiveFrom(bytes, 0, bytes.Length, SocketFlags.Multicast, ref remoteEP,
new AsyncCallback(MyCallback), so);

this line crashes with errorcode 10045, but with SocketFlags.None its fine

//exception thrown

_socket.BeginReceiveFrom( _receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.Multicast, ref _ep, _asyncReceive, null );

//no exception thrown

_socket.BeginReceiveFrom( _receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, ref _ep, _asyncReceive, null );

Valdair at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified