简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM