2

これは私が使用している私のコードです。これは私がインターネットから入手したコードです。彼らはそれがうまく機能していると言っていました。これに対するコメントも良いのですが、なぜうまくいかないのかわかりません。もう1つ、このアプリケーションを管理者モードではなくユーザーモードとして使用しています。

private void btnStart_Click(object sender, EventArgs e)
    {
        if (cmbInterfaces.Text == "")
        {
            MessageBox.Show("Select an Interface to capture the packets.", "MJsniffer", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        try
        {
            if (!bContinueCapturing)        
            {
                //Start capturing the packets...

                btnStart.Text = "&Stop";

                bContinueCapturing = true;

                //For sniffing the socket to capture the packets has to be a raw socket, with the
                //address family being of type internetwork, and protocol being IP
                Console.WriteLine("1");
                mainSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Raw, ProtocolType.IP);
                Console.WriteLine("2");
                //Bind the socket to the selected IP address
                mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));
                Console.WriteLine("3");
                //Set the socket  options
                mainSocket.SetSocketOption(SocketOptionLevel.IP,            //Applies only to IP packets
                                           SocketOptionName.HeaderIncluded, //Set the include the header
                                           true);                           //option to true
                Console.WriteLine("4");
                byte[] byTrue = new byte[4] {1, 0, 0, 0};
                byte[] byOut = new byte[4]{1, 0, 0, 0}; //Capture outgoing packets

                //Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
                mainSocket.IOControl(IOControlCode.ReceiveAll,              //Equivalent to SIO_RCVALL constant
                                                                            //of Winsock 2
                                     byTrue,                                    
                                     byOut);

                //Start receiving the packets asynchronously
                mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                    new AsyncCallback(OnReceive), null);
            }
            else
            {
                btnStart.Text = "&Start";
                bContinueCapturing = false;
                //To stop capturing the packets close the socket
                mainSocket.Close ();
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine("5");
            MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
            Console.WriteLine("6");
            MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
4

2 に答える 2

4

そして、もう1つ、このアプリケーションを管理者モードではなくユーザーモードとして使用しています。

これは機能しません。以下はWin32API用に記述されていますが、.NETが呼び出すものであるため、同じことが当てはまります

タイプのソケットを使用するには、SOCK_RAW管理者権限が必要です。rawソケットを使用するWinsockアプリケーションを実行しているユーザーは、ローカルコンピューターのAd​​ministratorsグループのメンバーである必要があります。そうでない場合、rawソケット呼び出しはエラーコード.で失敗しますWSAEACCES。Windows Vista以降では、rawソケットへのアクセスはソケットの作成時に適用されます。以前のバージョンのWindowsでは、rawソケットへのアクセスは他のソケット操作中に強制されます。

私の強調

于 2013-03-13T07:59:46.687 に答える
0

SocketException.SocketErrorCode を確認して、質問を更新していただけますか?
10013 を受け取ると仮定します。これらはコードの説明です。ほとんどの場合、他のアプリケーションがすでにソケットにアクセスしているか、権限がありません。

于 2013-03-13T07:37:59.350 に答える