Anuya
Anuya

Reputation: 8350

How to skip the subsequent byte[] received from socket using c#

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

Upvotes: 0

Views: 532

Answers (1)

Ahmed
Ahmed

Reputation: 7248

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"

Upvotes: 3

Related Questions