views:

175

answers:

1

I am using the function below inside a thread to receive the byte[] via socket. Since my machine has two network adaptors it is receiveing the byte[] two times. I want to skip the subsequent same byte[] received.

What should I do to achive this?

public void Receiver()
{

        strMultpileBatchString = "";
        string mcastGroup = ReceiverIP;
        string port = ReceiverPort;

        //Declare the socket object.
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        //Initialize the end point of the receiver socket.
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(port));
        s.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
        s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
        s.Bind(ipep);
        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0);
        IPAddress ip = IPAddress.Parse(mcastGroup);
        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip));


        while (true)
        {
            byte[] b = new byte[BytesSize];
        }
}
+3  A: 
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(port));

I think this is the cause of the problem IPAddress.Any, try to specify specific IP (one of two network cards)

From MSDN

"Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress..::.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces"

Ahmed Said