2016-11-27 13 views
2

UDPを使用するモノラルC#アプリケーションを作成しています。私はパケットがNUnitで正しく受け取られるようにテストスイートを書いていました。UDPをローカルでテストするC#

UdpComクラスを使用した最小限の作業例を作成しました。これはただちに耳を傾け、コールバックメソッドで受け取ったバイトをプリントアウトすることになっています。

マイ最小限UdpComクラスは次のようになります。

using System; 
using System.Net; 
using System.Net.Sockets; 


namespace UdpPractice 
{ 
    public class UdpState 
    { 
     public UdpClient client; 
     public IPEndPoint endpoint; 
     public UdpState(UdpClient c, IPEndPoint iep) 
     { 
      this.client = c; 
      this.endpoint = iep; 
     } 
    } 
    public class UdpCom 
    { 
     public UdpState uState; 
     public UdpClient uClient; 
     public IPAddress address; 
     public int port; 
     public IPEndPoint uEndpoint; 

     public UdpCom (IPAddress address, int port) 
     { 
      uEndpoint = new IPEndPoint (address, port); 
      uClient = new UdpClient (uEndpoint); 
      uState = new UdpState (uClient, uEndpoint); 
      uClient.BeginReceive (new AsyncCallback(RxCallback), uState); 
     } 

     public void RxCallback(IAsyncResult result) 
     { 
      UdpState rState = (UdpState)(result.AsyncState); 
      UdpClient rClient = rState.client; 
      IPEndPoint rEndPoint = rState.endpoint; 
      byte[] rxBytes = rClient.EndReceive (result, ref rEndPoint); 
      Console.WriteLine ("Received Bytes ___________________________"); 
      Console.WriteLine (rxBytes.ToString()); 

      rClient.BeginReceive (new AsyncCallback(RxCallback), rState); 
     } 
    } 
} 

私の簡単なテストは、このクラスをインスタンス化して、それをバイトのダミーパケットを送信します。結果を今はテストしていません。私のRxCallbackメソッドにブレークポイントを設定するだけです。

これは私のNUnitのテストです:

using NUnit.Framework; 
using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using UdpPractice; 

namespace UdpTest 
{ 
    [TestFixture()] 
    public class Test 
    { 
     [Test()] 
     public void TestCase() 
     { 
      // Setup Receiver 
      IPAddress address = new IPAddress (new byte[] { 127, 0, 0, 1 }); 
      int port = 14580; 
      UdpCom com = new UdpCom (address, port); 
      com.uClient.Connect (com.uEndpoint); 
      // Set up sender 
      UdpClient sender = new UdpClient(new IPEndPoint (address, port)); 
      sender.Connect (address, port); 

      // Dummy Data 
      byte [] dummyData = new byte[] {1, 2, 3, 4, 4, 5}; 

      sender.Send (dummyData, dummyData.Length); 
      Thread.Sleep (100); 

      // Close 
      sender.Close(); 
      com.uClient.Close(); 
     } 
    } 
} 

私の問題は私が、私はこの例外を取得していますバイトを取得しようとしているライン上のRxCallback方法でいる時ということです。

System.ObjectDisposedException has been thrown 
Cannot access a disposed object. 
Object name: System.Net.Sockets.UdpClient 

これらのバイトをUdpComクラスのキューに格納したいと思っていますが、最初にそれらのバイトにアクセスする際に問題があります。

私はUDPの基本的な概念をいくつか立てるのに失敗しているかもしれませんが、それはかなり前のようです。私は、次のされています

udpの例は以下のとおりです。

EDIT ので、私は地元を使用していない、次のように私は私のクラスとテストを更新ループバック。しかし、私はまだ同じ例外を取得しています。

using System; 
using System.Net; 
using System.Net.Sockets; 


namespace UdpPractice 
{ 
    public class UdpState 
    { 
     public UdpClient client; 
     public IPEndPoint endpoint; 
     public UdpState(UdpClient c, IPEndPoint iep) 
     { 
      this.client = c; 
      this.endpoint = iep; 
     } 
    } 
    public class UdpCom 
    { 
     public UdpState uState; 
     public UdpClient uClient; 
     public IPAddress address; 
     public int port; 
     public IPEndPoint uEndpoint; 

     public UdpCom (IPAddress address, int port) 
     { 
      uEndpoint = new IPEndPoint (address, port); 
      uClient = new UdpClient (uEndpoint); 
      uState = new UdpState (uClient, uEndpoint); 
      uClient.BeginReceive (new AsyncCallback(RxCallback), uState); 
     } 

     public void RxCallback(IAsyncResult result) 
     { 
      UdpState rState = (UdpState)(result.AsyncState); 
      UdpClient rClient = rState.client; 
      IPEndPoint rEndPoint = rState.endpoint; 
      byte[] rxBytes = rClient.EndReceive (result, ref rEndPoint); 
      Console.WriteLine ("Received Bytes ___________________________"); 
      Console.WriteLine (rxBytes.ToString()); 

      rClient.BeginReceive (new AsyncCallback(RxCallback), rState); 
     } 
    } 
} 

とテストは次のとおりです: UdpComは今ある

using NUnit.Framework; 
using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using UdpPractice; 

namespace UdpTest 
{ 
    [TestFixture()] 
    public class Test 
    { 
     [Test()] 
     public void TestCase() 
     { 
      // Setup Receiver 
      IPAddress address = new IPAddress (new byte[] { 192, 168, 1, 161 }); 
      int port = 14580; 
      UdpCom com = new UdpCom (IPAddress.Any, port); 
      // com.uClient.Connect (com.uEndpoint); 
      // Set up sender 
      UdpClient sender = new UdpClient(new IPEndPoint (address, port)); 
      sender.Connect (address, port); 

      // Dummy Data 
      byte [] dummyData = new byte[] {1, 2, 3, 4, 4, 5}; 

      sender.Send (dummyData, dummyData.Length); 
      Thread.Sleep (100); 

      // Close 
      sender.Close(); 
      com.uClient.Close(); 
     } 
    } 
} 
+0

あなたが使用できる別のリソース:http://stackoverflow.com/a/19787486/5233410 – Nkosi

+1

同じIPアドレス、宛先IP、ポート番号の3つのプロパティを持つ接続を1つだけ持つことができます。ローカルに接続するときは、クライアントとサーバーが同じ3つのプロパティを使用していないことを確認する必要があります。解決策は、サーバーをIPAnyでリッスンし、クライアントをPCのIPアドレスに接続させることです。通常はLocalHostであるPC 127.0.0.1のLoopBackアドレスを使用しています。 Netライブラリでは、クライアントがループバックIPを使用することを許可しておらず、例外が発生します。 – jdweng

+0

私は 'com.uClient.Connect(com.uEndpoint);'行を取り出して、 'com'クライアントを' UdpCom com = new UdpCom(IPAddress.Any、port);と送信者UdpClient 192.168.1.161に接続してください。同じ例外が発生しています。変更を反映するために質問を更新しました。 – Jesse

答えて

0

私はUDPで通信する方法と基本的な誤解で苦しんでいたと思います。

UdpCom:

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Collections.Generic; 

namespace UdpPractice 
{ 
    public class UdpState 
    { 
     public UdpClient client; 
     public IPEndPoint endpoint; 
     public UdpState(UdpClient c, IPEndPoint iep) 
     { 
      this.client = c; 
      this.endpoint = iep; 
     } 
    } 
    public class UdpCom 
    { 
     public UdpState uState; 
     public IPAddress address; 
     public int port; 
     public IPEndPoint uEndpoint; 
     public bool receiveFlag; 
     public List<byte[]> rxBytesBuffer; 

     public UdpCom (IPAddress address, int port) 
     { 
      uEndpoint = new IPEndPoint (address, port); 
      // uClient = new UdpClient (uEndpoint); 
      uState = new UdpState (new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port)), uEndpoint); 
      receiveFlag = false; 
      uState.client.BeginReceive (new AsyncCallback(RxCallback), uState); 
      rxBytesBuffer = new List<byte[]>(); 
     } 

     public void RxCallback(IAsyncResult result) 
     { 
      UdpState rState = (UdpState)result.AsyncState; 
      UdpClient rClient = ((UdpState)result.AsyncState).client; 
      IPEndPoint rEndPoint = ((UdpState)result.AsyncState).endpoint; 
      byte[] rxBytes = rClient.EndReceive (result, ref rEndPoint); 
      rxBytesBuffer.Add (rxBytes); 
      Console.WriteLine ("Received Bytes ___________________________"); 
      Console.WriteLine (rxBytes.ToString()); 
      receiveFlag = true; 
      rClient.BeginReceive (new AsyncCallback(RxCallback), result.AsyncState); 
     } 
    } 
} 

テスト:

using NUnit.Framework; 
using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using UdpPractice; 

namespace UdpTest 
{ 
    [TestFixture()] 
    public class Test 
    { 
     [Test()] 
     public void TestCase() 
     { 
      // Setup Listener 
      int port = 14580; 
      IPEndPoint locep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); 
      // Server (Listener) 
      UdpCom com = new UdpCom (IPAddress.Any, port); 
      // Set up sender 
      UdpClient sender = new UdpClient(); 
      // Dummy Data 
      byte [] dummyData = new byte[] {1, 2, 3, 4, 4, 5}; 
      sender.Send (dummyData, dummyData.Length, locep); 
      sender.Send (dummyData, dummyData.Length, locep); 
      Thread.Sleep (100); 
      Assert.AreEqual(true, com.receiveFlag); 
     } 
    } 
} 

私はリスナーがIPAddress.Anyに耳を傾けるにするために必要な、その後、私は主にEndPoint引数でSend方法を使用していました。受信フラグがセットされていなかったので、そこに遅延を追加しなければなりませんでした。

関連する問題