簡體   English   中英

如何使用C#跳過從套接字收到的后續byte []

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

我在線程內部使用下面的函數通過套接字接收byte []。 由於我的機器有兩個網絡適配器,因此它兩次接收到byte []。 我想跳過隨后收到的相同byte []。

我應該怎么做才能做到這一點?

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

我認為這是導致IPAddress問題的原因。任何嘗試嘗試指定特定的IP(兩個網卡之一)

MSDN

“在調用Bind之前,必須首先創建要從中進行數據通信的本地IPEndPoint。如果您不在乎分配了哪個本地地址,則可以使用IPAddress .. ::。Any作為address參數創建IPEndPoint,並且基礎服務提供商將分配最合適的網絡地址。如果您具有多個網絡接口,這可能有助於簡化您的應用程序。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM